StateGraph class is the primary interface for building stateful, multi-actor applications with LangGraph. It provides a declarative API for defining nodes, edges, and state schemas.
Defined in: langgraph/graph/state.py:113
Overview
StateGraph is a graph whose nodes communicate by reading and writing to a shared state. The signature of each node is State -> Partial<State>. Each state key can optionally be annotated with a reducer function that will be used to aggregate the values of that key received from multiple nodes.
Warning: StateGraph is a builder class and cannot be used directly for execution. You must first call .compile() to create a CompiledStateGraph object that supports methods like invoke(), stream(), astream(), and ainvoke().
Constructor
Parameters
The schema class that defines the state. Can be a
TypedDict, Pydantic model, or dataclass.Each field can be annotated with a reducer function using Annotated[type, reducer] syntax.The schema class that defines the runtime context.Use this to expose immutable context data to your nodes, like
user_id, db_conn, etc. Context is accessible via the Runtime object injected into nodes.The schema class that defines the input to the graph. If not provided, defaults to
state_schema.Use this to accept a subset of the state schema as input.The schema class that defines the output from the graph. If not provided, defaults to
state_schema.Use this to return a subset of the state schema as output.Usage Example
Methods
add_node
Parameters
The function or runnable this node will run.If a string is provided, it will be used as the node name, and
action will be used as the function or runnable. Otherwise, the name is inferred from the function/runnable name.The action associated with the node. Required if
node is a string.Whether to defer the execution of the node until the run is about to end.
Metadata associated with the node.
The input schema for the node. If not provided, defaults to the graph’s state schema.Use this to provide a subset of the state to the node.
Retry policy for the node. If a sequence is provided, the first matching policy will be applied.
Cache policy for the node.
Destinations that indicate where a node can route to. Useful for edgeless graphs with nodes that return
Command objects.If a dict is provided, the keys are target node names and values are edge labels. If a tuple is provided, the values are target node names.Note: This is only used for graph rendering and doesn’t affect graph execution.Returns
The instance of the StateGraph, allowing for method chaining.
Usage Example
add_edge
Parameters
The key(s) of the start node(s) of the edge.
The key of the end node of the edge.
Returns
The instance of the StateGraph, allowing for method chaining.
Raises
- ValueError: If the start key is
ENDor if the start/end key is not present in the graph.
Usage Example
add_conditional_edges
Parameters
The starting node. This conditional edge will run when exiting this node.
The callable that determines the next node or nodes.If not specifying
path_map, it should return one or more node names. If it returns END, the graph will stop execution.Optional mapping of paths to node names. If omitted, the paths returned by
path should be node names.Returns
The instance of the graph, allowing for method chaining.
Usage Example
add_sequence
Parameters
A sequence of
StateNode (callables that accept a state arg) or (name, StateNode) tuples.If no names are provided, the name will be inferred from the node object. Each node will be executed in the order provided.Returns
The instance of the StateGraph, allowing for method chaining.
Usage Example
set_entry_point
add_edge(START, key).
Parameters
The key of the node to set as the entry point.
Returns
The instance of the graph, allowing for method chaining.
set_conditional_entry_point
add_conditional_edges(START, path, path_map).
Parameters
The callable that determines the next node or nodes. If it returns
END, the graph will stop execution.Optional mapping of paths to node names.
Returns
The instance of the graph, allowing for method chaining.
set_finish_point
add_edge(key, END).
Parameters
The key of the node to set as the finish point.
Returns
The instance of the graph, allowing for method chaining.
compile
Runnable interface and can be invoked, streamed, batched, and run asynchronously.
Parameters
A checkpoint saver object or flag.If provided, this
Checkpointer serves as a fully versioned “short-term memory” for the graph, allowing it to be paused, resumed, and replayed from any point.- If
None, it may inherit the parent graph’s checkpointer when used as a subgraph. - If
False, it will not use or inherit any checkpointer.
thread_id in the config when invoking the graph.Cache to use for storing node results.
Memory store to use for SharedValues.
An optional list of node names to interrupt before. Use
"*" to interrupt before all nodes.An optional list of node names to interrupt after. Use
"*" to interrupt after all nodes.A flag indicating whether to enable debug mode.
The name to use for the compiled graph. Defaults to
"LangGraph".Returns
The compiled StateGraph that can be invoked and streamed.
Usage Example
CompiledStateGraph
The result of callingStateGraph.compile() is a CompiledStateGraph instance, which extends Pregel and implements the LangChain Runnable interface.
Defined in: langgraph/graph/state.py:1180
Methods
TheCompiledStateGraph inherits all methods from Pregel, including:
invoke()- Synchronously invoke the graphainvoke()- Asynchronously invoke the graphstream()- Synchronously stream graph executionastream()- Asynchronously stream graph executionget_state()- Get current graph stateupdate_state()- Update graph stateget_graph()- Get graph structure