All Agent Engineering

Merge parallel branch state in LangGraph with reducers

Choose a reducer per key written by parallel nodes and align it with Send fan-out inputs and the reader node.

AINavWritten / Updated
On this page

At a glance

When parallel nodes write the same state key, define how their updates will merge. The documented default replacement describes updating an old value; it does not establish last-writer-wins for concurrent writes. This guide defines a state table, reducers and Send worker contracts: configure explicit merging for parallel aggregation or redesign the graph to avoid simultaneous writes.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • LangGraph Merge parallel branch state in LangGraph with reducers

Execution locations and responsibilities

  • LangGraph graph runtime and application host Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. List the same-key updates from parallel nodes → 2. Choose a merge rule for each shared key → 3. Verify left/right semantics of the reducer → 4. Match Send inputs to worker return keys → 5. Handle clearing, retries, and duplicate routing → 6. Check what the aggregating node reads. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. List the same-key updates from parallel nodes2. Choose a merge rule for each shared key3. Verify left/right semantics of the reducer4. Match Send inputs to worker return keys5. Handle clearing, retries, and duplicate routing6. Check what the aggregating node reads

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • Each state key has an optional reducer; nodes return partial updates applied through that key’s channel. Default replacement describes updating an old value, not which concurrent result survives. Shared keys requiring aggregation need an explicit merge reducer, value type and merge meaning, or a graph redesign avoiding simultaneous writes.

When not to use

  • This note does not cover Command resume, interrupts, or parent-key updates from subgraphs; Command appears only because mixing it with static edges can duplicate paths.
  • The documentation does not state the merge order of parallel updates, so no reducer here may rely on order.
  • The full usage of Overwrite is on a separate documentation page that was not supplied, so its parameters are not inferred.

Prerequisites and input

  • An existing or designed StateGraph state schema in which every key has a known type.
  • A way to say which nodes run in the same super-step and which keys each of them returns.

Engineering checks

State layer: keys and their reducers — Each state key has an optional reducer; nodes return partial updates applied through that key’s channel. Default replacement describes updating an old value, not which concurrent result survives. Shared keys requiring aggregation need an explicit merge reducer, value type and merge meaning, or a graph redesign avoiding simultaneous writes.

Scheduling layer: super-steps and Send — Nodes that run at the same time share one super-step. Send objects are returned from a conditional edge; each names a target node and the input for that invocation, and the invocations run in parallel. When they write to shared keys, reducers merge the updates, for example by concatenating lists. Normal edges cannot create invocations for an item count known only at runtime.

Reset layer: clearing after merging — For a key with a merging reducer, returning an empty value does not clear earlier data. Error buffers or counters that must reset between retries should use Overwrite, which bypasses the reducer. The documentation points to a separate page for details, and the supplied material does not include them.

1. List the same-key updates from parallel nodes

Where: the application code that holds the state schema. Input: the return-value description of every parallel node. Action: build a table of key, writing node, value type, and whether several branches can write it at once. Completion check: every multi-writer key has a row. Stop condition: stop if you cannot tell which keys a node writes, and clarify that node's contract first.

2. Choose a merge rule for each shared key

Where: the state schema. Input: the parallel-write table. Shared keys retaining multiple branch results bind an explicit reducer with Annotated; operator.add is the documented list-append example. If only one result is needed, route the graph to avoid simultaneous writes instead of assuming default replacement is a concurrency rule. Completion: every shared key has a reducer or a concrete plan avoiding simultaneous writes. Stop to separate keys or update purposes when aggregation and replacement have not been distinguished.

3. Verify left/right semantics of the reducer

Where: each custom reducer function. Input: the current state value (left) and the node update (right). Action: confirm it is a binary function returning a new value of the same type. The documentation does not state the order in which parallel updates are merged, so do not let the result depend on order; sort in the reader node if needed. Completion check: walk through two arrival orders on paper and get the same meaning. Stop condition: stop and revise if the two orders differ in meaning.

4. Match Send inputs to worker return keys

Where: the conditional router and worker. Input: the runtime item list. Return a Send for each item with its call-specific state. For parallel shared aggregation, workers return only keys registered in the state table with an explicit merge reducer. Completion: each returned aggregate key matches the table and its reducer. Stop when a worker needs an unregistered or unconfigured shared aggregate key.

5. Handle clearing, retries, and duplicate routing

Where: update design and routing. Input: aggregate keys needing replacement or clearing. Keep the reducer for normal accumulation and separately record one-off updates that need to bypass it. The documented Overwrite bypasses a reducer; it does not replace the permanent schema reducer. Read its dedicated reference before coding reset behavior and stop reset implementation without that evidence. Avoid mixing static edges and dynamic routing from the same node, which can run both paths. Completion: normal aggregation, one-off reset and routing choices are distinguished. Stop unconfirmed reset syntax or routing; retain only the aggregation design conclusion without claiming a reset run.

6. Check what the aggregating node reads

Where: the reader node after the fan-out. Input: the merged state you derived on paper. Action: confirm it depends only on the merged whole, not on item count or order. Completion check: the expected value for each key is written down. Stop condition: stop if an expectation contradicts the step 3 walk-through.

Task example

Unexecuted walkthrough: Send distributes three documents to workers, each returning one finding in a list. The state table configures Annotated with operator.add for that shared parallel key; expect three retained findings and sort by item ID downstream without relying on merge order. With no explicit reducer, stop and configure it or avoid simultaneous writes instead of predicting which update survives. If findings must be cleared, record a separate bypassing update while retaining normal accumulation; check dedicated Overwrite documentation before coding it.

Completion checks

  • Every shared parallel key has an explicit merge reducer or routing that avoids simultaneous writes; no last-writer outcome is inferred from default replacement.
  • The reducer gives the same meaning under two arrival orders on paper.
  • Send inputs and worker return keys map one-to-one onto the state table.
  • Retain the normal accumulation reducer and register reset updates separately; stop reset implementation until exact Overwrite usage is established.

Verification scope

This note relies only on the supplied LangGraph Graph API documentation, which was read. LangGraph was not installed, no login was made, no API was executed, and no output was produced or verified; the example is hypothetical.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. LangGraph · LangGraph documentation: langgraph-api

Found an error? Tell us →