Passing State Between Nodes

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

  1. Node A returns {"messages": ["Message 1"]}
  2. Node B returns {"messages": ["Message 2"]}
  3. Result State: messages = ["Message 1", "Message 2"]

This allows multiple nodes to contribute to the conversation history without destroying previous context.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn