
Passing State Between Nodes
How data flows through the graph.
Passing State Between Nodes
In a linear script, you pass data via variables. In LangGraph, you pass data via the State Reducer.
The Reducer Pattern
When a node returns a value, it doesn't overwrite the state; it sends an update to the reducer.
from typing import Annotated
from langgraph.graph.message import add_messages
# State Definition with Reducer
class State(TypedDict):
# 'add_messages' ensures new messages are APPENDED, not overwriting old ones
messages: Annotated[list, add_messages]
Example Flow
- Node A returns
{"messages": ["Message 1"]} - Node B returns
{"messages": ["Message 2"]} - Result State:
messages = ["Message 1", "Message 2"]
This allows multiple nodes to contribute to the conversation history without destroying previous context.