add_node(
self,
node: str | StateNode[NodeInputT, ContextT],
| Name | Type | Description |
|---|---|---|
node* | str | StateNode[NodeInputT, ContextT] | 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. |
action | StateNode[NodeInputT, ContextT] | None | Default: None |
defer | bool | Default: False |
metadata | dict[str, Any] | None | Default: None |
input_schema | type[NodeInputT] | None | Default: None |
retry_policy | RetryPolicy | Sequence[RetryPolicy] | None | Default: None |
cache_policy | CachePolicy | None | Default: None |
error_handler | StateNode[Any, ContextT] | None | Default: None |
destinations | dict[str, str] | tuple[str, ...] | None | Default: None |
timeout | float | timedelta | TimeoutPolicy | None | Default: None |
Add a new node to the StateGraph.
Example:
from typing_extensions import TypedDict
from langchain_core.runnables import RunnableConfig
from langgraph.graph import START, StateGraph
class State(TypedDict):
x: int
def my_node(state: State, config: RunnableConfig) -> State:
return {"x": state["x"] + 1}
builder = StateGraph(State)
builder.add_node(my_node) # node name will be 'my_node'
builder.add_edge(START, "my_node")
graph = builder.compile()
graph.invoke({"x": 1})
# {'x': 2}
Customize the name::
builder = StateGraph(State)
builder.add_node("my_fair_node", my_node)
builder.add_edge(START, "my_fair_node")
graph = builder.compile()
graph.invoke({"x": 1})
# {'x': 2}The action associated with the node.
Will be used as the node function or runnable if node is a string (node name).
Whether to defer the execution of the node until the run is about to end.
The metadata associated with the node.
The input schema for the node. (Default: the graph's state schema)
The retry policy for the node.
If a sequence is provided, the first matching policy will be applied.
The cache policy for the node.
Optional node-level error handler callable for this 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 will be used as the target node names and the values will be used as the labels for the edges.
If a tuple is provided, the values will be used as the target node names.
This is only used for graph rendering and doesn't have any effect on the graph execution.
Timeout for each node attempt. A number or timedelta is
a hard wall-clock cap and is not refreshed. Use TimeoutPolicy
to configure both a wall-clock run_timeout and an
idle_timeout refreshed by progress signals. When exceeded, a
NodeTimeoutError is raised
and the retry policy (if any) decides whether to retry. Timeouts
are supported only for async nodes; sync nodes cannot be safely
cancelled in-process.