Lesson 5 of 11
Chapter 18 . LangGraph . Lesson 5 of 11

Parallel branches and reducers

Independent nodes run at the same time. A reducer defines how their results merge into one key.

01Real-world example

Illustration: API, UI and performance suites run in parallel, and their results merge into one release report.
API, UI and performance suites run in parallel, and their results merge into one release report.

02The graph

Parallel branches and reducers as a graph
api, ui and perf run together. operator.add joins their results into one list.

03How the code does it

  • Three edges from START fan out: api, ui and perf run in the same step.
  • Three edges into summarize fan in: it waits for all three before it runs.
  • Annotated[list[str], operator.add] tells LangGraph to concatenate parallel writes. Without a reducer two writers in one step is an error.
  • summarize has no type hint on purpose: LangGraph reads a node's type hint as its input schema.

Key points

  • Many edges from START run in parallel
  • Many writers, one key? Add a reducer
  • No reducer means InvalidUpdateError

04Run it

terminal
cd chapter_18_LangGraph/src/chapters
python 005_Parallel_Reducer.py
output
  api: 42/42 passed
  perf: p95 380ms (budget 500ms)
  ui: 118/120 passed
  3 suites reported

Same graph WITHOUT the reducer:
  InvalidUpdateError: At key 'results': Can receive only one value per step. Use an Annotated key to handle multiple values.