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
type[StateT]
required
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.type[ContextT] | None
default:"None"
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.type[InputT] | None
default:"None"
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.type[OutputT] | None
default:"None"
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
str | StateNode
required
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.StateNode | None
default:"None"
The action associated with the node. Required if
node is a string.bool
default:"False"
Whether to defer the execution of the node until the run is about to end.
dict[str, Any] | None
default:"None"
Metadata associated with the node.
type[NodeInputT] | None
default:"None"
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.
RetryPolicy | Sequence[RetryPolicy] | None
default:"None"
Retry policy for the node. If a sequence is provided, the first matching policy will be applied.
CachePolicy | None
default:"None"
Cache policy for the node.
dict[str, str] | tuple[str, ...] | None
default:"None"
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
Self
The instance of the StateGraph, allowing for method chaining.
Usage Example
add_edge
Parameters
str | list[str]
required
The key(s) of the start node(s) of the edge.
str
required
The key of the end node of the edge.
Returns
Self
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
str
required
The starting node. This conditional edge will run when exiting this node.
Callable | Runnable
required
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.dict[Hashable, str] | list[str] | None
default:"None"
Optional mapping of paths to node names. If omitted, the paths returned by
path should be node names.Returns
Self
The instance of the graph, allowing for method chaining.
Usage Example
add_sequence
Parameters
Sequence
required
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
Self
The instance of the StateGraph, allowing for method chaining.
Usage Example
set_entry_point
add_edge(START, key).
Parameters
str
required
The key of the node to set as the entry point.
Returns
Self
The instance of the graph, allowing for method chaining.
set_conditional_entry_point
add_conditional_edges(START, path, path_map).
Parameters
Callable | Runnable
required
The callable that determines the next node or nodes. If it returns
END, the graph will stop execution.dict[Hashable, str] | list[str] | None
default:"None"
Optional mapping of paths to node names.
Returns
Self
The instance of the graph, allowing for method chaining.
set_finish_point
add_edge(key, END).
Parameters
str
required
The key of the node to set as the finish point.
Returns
Self
The instance of the graph, allowing for method chaining.
compile
Runnable interface and can be invoked, streamed, batched, and run asynchronously.
Parameters
Checkpointer
default:"None"
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.BaseCache | None
default:"None"
Cache to use for storing node results.
BaseStore | None
default:"None"
Memory store to use for SharedValues.
All | list[str] | None
default:"None"
An optional list of node names to interrupt before. Use
"*" to interrupt before all nodes.All | list[str] | None
default:"None"
An optional list of node names to interrupt after. Use
"*" to interrupt after all nodes.bool
default:"False"
A flag indicating whether to enable debug mode.
str | None
default:"None"
The name to use for the compiled graph. Defaults to
"LangGraph".Returns
CompiledStateGraph
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