State Schemas, Reducers and Messages
State is the shared memory every node reads and the only thing nodes change. Each key is a channel with its own reducer, so you need to know how updates merge, how messages are matched by id, what each schema type validates, and what belongs in state rather than in config or runtime context.
Key points
- 1
A node receives the current state and returns a partial update (a dict with only the keys it changes), and LangGraph merges each returned key into its channel. A returned key that is not in the schema has no channel and is silently dropped.
- 2
A key without a reducer is overwritten by the latest value.
Annotated[list, operator.add](or any(old, new) -> mergedfunction) combines updates instead, and the reducer also applies to values passed in theinvokeinput. - 3
add_messagesappends new messages, gives an id to any message that lacks one, replaces a stored message whose id matches, and converts dicts,(role, content)tuples and bare strings into message objects. - 4
To delete messages, return
RemoveMessage(id=...)throughadd_messages.RemoveMessage(id=REMOVE_ALL_MESSAGES)clears the whole list. With plainoperator.add, aRemoveMessagejust gets appended. - 5
MessagesStateis a ready-made schema with one key,messages, annotated withadd_messages. Subclass it to add more keys. - 6
Overwrite(value)fromlanggraph.typesbypasses the reducer for one write and replaces the whole value. TwoOverwritewrites to the same key in one super-step raiseInvalidUpdateError. - 7
If two nodes in the same super-step write to a key that has no reducer, you get
InvalidUpdateErrorbecause LangGraph cannot pick a winner. Give the key a reducer or have the branches write to different keys. - 8
TypedDictschemas get no runtime type checks. Adataclassadds field defaults. A PydanticBaseModelvalidates and coerces node inputs, but node return values are not validated and graph output is a plain dict, not a model instance. - 9
StateGraph(State, input_schema=In, output_schema=Out)filters whatinvokeaccepts and returns. A node's type hint (def n(state: Private)) decides which channels it receives. Keys outside the output schema still show up instream_mode="values". - 10
Per-run dependencies such as a user id or a database client belong in runtime context. Declare
context_schema=Ctx, callinvoke(..., context={...}), and readruntime.contextin a node that has aruntime: Runtime[Ctx]parameter. Context is never checkpointed. - 11
With a checkpointer, every call on the same
thread_idstarts from the saved state. Reducer keys therefore keep accumulating across calls, and that includes the values you pass as input. - 12
In JS (
@langchain/langgraph1.x), the docs define state withStateSchemaand Zod fields. Keys that need a reducer useReducedValue, and the message list usesMessagesValue.
Common traps
Using
operator.addfor messages. Edits that reuse an id get duplicated instead of replaced, andRemoveMessagedoes nothing useful.Expecting a
TypedDictschema to catch a misspelt key or a wrong type. The misspelt write is silently dropped, and value types are never checked.Putting clients, locks or secrets in state. Checkpoints would have to serialise them, and they leak into storage. Put them in runtime context.
Forgetting that the reducer also runs on the input. Re-sending the full history to a thread that has a checkpointer duplicates messages unless the ids match.
Assuming a Pydantic state validates node outputs or makes
invokereturn a model instance. It does neither.Parallel branches that write the same plain key fail with
InvalidUpdateErroronly when they land in the same super-step, so the bug can show up after an unrelated edge change.
Read the source
Further reading
Suggest a resourceNo extra reading for State Schemas, Reducers and Messages yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on State Schemas, Reducers and Messages
Ten questions, with the answer and explanation after each one.