Skip to content

Instantly share code, notes, and snippets.

@razhangwei
Created February 10, 2025 03:16
Show Gist options
  • Save razhangwei/631a799e6bd69b26ce9c118c624cd80d to your computer and use it in GitHub Desktop.
Save razhangwei/631a799e6bd69b26ce9c118c624cd80d to your computer and use it in GitHub Desktop.
LangGraph cheatsheet

This is an excellent LangGraph cheat sheet! It provides a comprehensive and practical overview of LangGraph's key features and usage. Here's a slightly refined version with added clarity and formatting for better readability:


LangGraph Cheat Sheet

1. Import Necessary Modules

from typing import TypedDict, Dict, Any, Callable
from langchain_core.runnables import Runnable
from langgraph.graph import StateGraph, END

2. Define the State

  • Use a TypedDict to define the structure of your state.
  • This is the shared data passed between nodes.
class MyGraphState(TypedDict):
    input_data: str
    intermediate_result: str
    final_output: str

3. Create Nodes

  • Nodes are functions or Runnables that process the state.
  • They take the current state as input and return updates to the state.

Example Node Function:

def node_a(state: MyGraphState) -> Dict[str, Any]:
    new_result = f"Processed: {state['input_data']}"
    return {"intermediate_result": new_result}

Using LangChain Runnables as Nodes:

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
model = ChatOpenAI()
runnable_node = prompt | model  # Combine prompt and model as a Runnable

4. Create the Graph

  • Instantiate a StateGraph with your state definition.
builder = StateGraph(MyGraphState)

5. Add Nodes to the Graph

builder.add_node("node_a", node_a)
builder.add_node("runnable_node", runnable_node)

6. Define Edges

  • Direct edges connect nodes in sequence.
builder.add_edge("node_a", "runnable_node")

7. Conditional Edges

  • Use conditional edges for branching logic.
def should_continue(state: MyGraphState):
    if len(state["intermediate_result"]) > 10:
        return "runnable_node"  # Route to runnable_node
    else:
        return END  # End the graph

builder.add_conditional_edges(
    "node_a",
    should_continue,
    {"runnable_node": "runnable_node", END: END}
)

8. Set the Entry Point

  • Define where execution starts in the graph.
builder.set_entry_point("node_a")

9. Compile the Graph

  • Compile the graph into a RunnableGraph.
graph = builder.compile()

10. Invoke the Graph

  • Pass an initial state to execute the graph.
inputs = {"input_data": "Short input"}
result = graph.invoke(inputs)
print(result)

11. Parallel Execution (Send/Gather)

  • Use Send() to dispatch tasks to multiple nodes in parallel and Gather() to collect results.

Example:

from langgraph.graph import Send

# Example parallel execution (not fully shown here)
# builder.add_node("parallel_task", Send([...]))

12. Checkpoints

  • Save and restore the state of the graph at specific points (useful for long-running workflows).

Example:

from langgraph.graph import CheckpointAt

checkpoint_config = CheckpointAt(
    nodes=["node_a", "runnable_node"],  # Nodes to checkpoint
    config={"save": True, "load": True}
)

# Add checkpointing logic if needed (refer to LangGraph docs for exact usage).

13. Key Classes and Functions

Class/Function Description
StateGraph Main class for creating a graph
add_node() Adds a node to the graph
add_edge() Adds a direct edge between two nodes
add_conditional_edges() Adds conditional edges based on a routing function
set_entry_point() Sets the starting node of the graph
compile() Compiles the graph into a RunnableGraph
invoke() Executes the graph with an initial state
Send() Dispatches tasks to multiple nodes in parallel
END Special node that signifies the end of a branch

14. Tips and Best Practices

  1. Define State Clearly: A well-defined state structure simplifies reasoning about data flow.
  2. Descriptive Node Names: Use meaningful names for better readability.
  3. Modular Nodes: Each node should perform one specific task.
  4. Debugging Tools: Use LangSmith for debugging and monitoring your graph execution.
  5. Leverage Runnables: Combine LangChain Runnables for powerful, reusable components.

This cheat sheet provides you with all essential steps, best practices, and examples for building LangGraph workflows efficiently! Let me know if you'd like further clarifications or advanced examples tailored to your needs! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment