
Immutable vs Mutable State
Managing state safety in concurrent environments.
Immutable vs Mutable State
LangGraph usually treats state as Immutable snapshots.
Why Immutability Matters
# BAD: Modifying global state in place
def bad_node(state):
state["user"]["age"] = 26 # MUTATION!
# If other nodes are reading 'state' right now, they might see partial updates.
# GOOD: Returning an update
def good_node(state):
# Retrieve current value
current_age = state.get("user", {}).get("age", 25)
# Return structure to be merged
return {"user": {"age": current_age + 1}}
Concurrency Safety
If Node A and Node B run in parallel, they check out the same version of the state (Snapshot 1). They compute their updates independently. The runtime then merges them safely into Snapshot 2. Direct mutation breaks this safety guarantee.