How to run a LangGraph graph
Quick answer
To run a
LangGraph graph, define your state type and node functions, add nodes and edges to a StateGraph, compile it with graph.compile(), then invoke the compiled app with app.invoke() passing the initial state. This executes the graph and returns the final state.PREREQUISITES
Python 3.8+pip install langgraphBasic Python typing knowledge
Setup
Install the langgraph package via pip and prepare your environment.
pip install langgraph output
Collecting langgraph Downloading langgraph-0.1.0-py3-none-any.whl (10 kB) Installing collected packages: langgraph Successfully installed langgraph-0.1.0
Step by step
Define a typed state, create node functions, build the graph, compile it, and invoke it with an initial state.
from langgraph.graph import StateGraph, END
from typing import TypedDict
# Define the state type
class State(TypedDict):
messages: list[str]
# Define a node function that appends a response
def my_node(state: State) -> State:
return {"messages": state["messages"] + ["response from my_node"]}
# Create the graph and add the node
graph = StateGraph(State)
graph.add_node("my_node", my_node)
graph.set_entry_point("my_node")
graph.add_edge("my_node", END)
# Compile the graph into an app
app = graph.compile()
# Invoke the app with initial state
result = app.invoke({"messages": ["Hello"]})
print(result) output
{'messages': ['Hello', 'response from my_node']} Common variations
You can define multiple nodes, add edges between them, and use different state types. The app.invoke() method runs synchronously. For async or streaming execution, integrate with async frameworks or customize node functions accordingly.
from langgraph.graph import StateGraph, END
from typing import TypedDict
class State(TypedDict):
messages: list[str]
# Node functions
def node1(state: State) -> State:
return {"messages": state["messages"] + ["node1 response"]}
def node2(state: State) -> State:
return {"messages": state["messages"] + ["node2 response"]}
# Build graph
graph = StateGraph(State)
graph.add_node("node1", node1)
graph.add_node("node2", node2)
graph.set_entry_point("node1")
graph.add_edge("node1", "node2")
graph.add_edge("node2", END)
app = graph.compile()
result = app.invoke({"messages": []})
print(result) output
{'messages': ['node1 response', 'node2 response']} Troubleshooting
- If you see
TypeError, ensure your node functions accept and return the correctTypedDictstate type. - If
app.invoke()hangs, verify that your graph has anENDedge to terminate execution. - Use
graph.set_entry_point()to specify the starting node; missing this causes runtime errors.
Key Takeaways
- Use
StateGraphwith typed states and node functions to build LangGraph graphs. - Compile the graph with
graph.compile()before invoking it. - Invoke the compiled app with
app.invoke()passing the initial state dictionary. - Always add an
ENDedge to terminate graph execution properly. - Define clear entry points with
graph.set_entry_point()to avoid runtime errors.