Principle:Langchain ai Langgraph Node Registration
| Metadata | Value |
|---|---|
| Type | Principle |
| Library | langgraph |
| Source | libs/langgraph/langgraph/graph/state.py
|
| Workflow | Building_a_Stateful_Graph |
Overview
Node registration is the process of adding processing functions (or runnables) to a StateGraph builder. Each node represents a discrete unit of computation that reads from the shared state and returns a partial state update. Nodes are the vertices in the directed graph that LangGraph compiles into an executable Pregel application.
Description
Nodes as Pure Functions
In LangGraph, every node follows the contract:
State -> Partial<State>
A node receives the current state (or a subset defined by its input schema) and returns a dictionary containing only the fields it wants to update. Fields not included in the return value are left unchanged. If a field has a reducer, the returned value is merged with the existing value through the reducer function rather than overwriting it.
This design encourages pure, side-effect-free functions that are easy to test in isolation: given a state dict, the function returns a predictable partial update.
Node Naming
Nodes can be registered with an explicit string name or the name can be inferred:
- Explicit:
graph.add_node("my_node", my_function) - Inferred:
graph.add_node(my_function)-- usesmy_function.__name__ - Runnable: If the action is a LangChain
Runnable, its.get_name()is used.
Names must be unique within the graph and cannot use the reserved names START ("__start__") or END ("__end__"). Names also cannot contain the internal separator characters : or |.
Deferred Nodes
A node can be marked as deferred (defer=True). Deferred nodes are not executed during normal superstep progression; instead, they are held back until the graph run is about to finish. This is useful for cleanup tasks, summary generation, or any post-processing that should run after the main computation completes.
Node Input Schema
By default, each node receives the full state schema. However, a node can declare a narrower input_schema so it only sees a subset of the state. LangGraph also attempts to infer the input schema from the function's type hints on its first parameter.
Sequences
The add_sequence method is a convenience for registering a chain of nodes that execute one after another. It calls add_node and add_edge for each consecutive pair, reducing boilerplate for linear pipelines.
Retry and Cache Policies
Nodes can be annotated with:
- Retry policy -- Defines how and when to retry a node if it raises an exception.
- Cache policy -- Defines caching behavior so repeated calls with the same input can return cached results.
Destinations
The destinations parameter is a rendering hint that documents which nodes a Command-returning node can route to. It does not affect execution but improves graph visualization.
Usage
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
x: int
def increment(state: State) -> dict:
return {"x": state["x"] + 1}
def double(state: State) -> dict:
return {"x": state["x"] * 2}
builder = StateGraph(State)
# Register with inferred name
builder.add_node(increment)
# Register with explicit name
builder.add_node("doubler", double)
# Register a sequence (linear pipeline)
builder.add_sequence([increment, double])
Theoretical Basis
- Actor model -- Each node can be viewed as an actor that receives a message (the state), processes it, and emits a response (the partial update). The Pregel engine coordinates execution across actors.
- Functional composition -- Nodes as pure functions
State -> Partial<State>support functional composition. Each node is independently testable, and the graph topology defines how these functions compose. - Deferred execution -- The concept of deferred nodes borrows from cleanup handlers and finalizers in systems programming, ensuring that certain operations run only after the main workflow completes.