LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Graphs
  • Functional API
  • Pregel
  • Checkpointing
  • Storage
  • Caching
  • Types
  • Runtime
  • Config
  • Errors
  • Constants
  • Channels
  • Agents
LangGraph CLI
LangGraph SDK
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewGraphsFunctional APIPregelCheckpointingStorageCachingTypesRuntimeConfigErrorsConstantsChannelsAgents
LangGraph CLI
LangGraph SDK
LangGraph Supervisor
LangGraph Swarm
Language
Theme
PythonlanggraphgraphstateStateGraphadd_node
Methodā—Since v0.1

add_node

Add a new node to the StateGraph.

Copy
add_node(
  self,
  node: str | StateNode[NodeInputT, ContextT],
  action: StateNode[NodeInputT, ContextT] | None = None,
  *,
  defer: bool = False,
  metadata: dict[str, Any] | None = None,
  input_schema: type[NodeInputT] | None = None,
  retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
  cache_policy: CachePolicy | None = None,
  destinations: dict[str, str] | tuple[str, ...] | None = None,
  **kwargs: Unpack[DeprecatedKwargs] = {}
) -> Self

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}

Parameters

NameTypeDescription
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.

actionStateNode[NodeInputT, ContextT] | None
Default:None

The action associated with the node.

Will be used as the node function or runnable if node is a string (node name).

deferbool
Default:False

Whether to defer the execution of the node until the run is about to end.

metadatadict[str, Any] | None
Default:None

The metadata associated with the node.

input_schematype[NodeInputT] | None
Default:None

The input schema for the node. (Default: the graph's state schema)

retry_policyRetryPolicy | Sequence[RetryPolicy] | None
Default:None

The retry policy for the node.

If a sequence is provided, the first matching policy will be applied.

cache_policyCachePolicy | None
Default:None

The cache policy for the node.

destinationsdict[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 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.

Warning

This is only used for graph rendering and doesn't have any effect on the graph execution.

View source on GitHub