By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An audit run fans out to three independent checks — inventory, pricing and compliance — and each one appends its finding to a shared list. The branches run in the same superstep, so a plain list[str] channel would raise InvalidUpdateError when the second writer arrives. Give the channel an append reducer and the writes become legal; the fan-in node then runs once after all branches finish and turns the concatenation into something deterministic. In this lab you build that fan-out/fan-in shape and test the three properties a merged list must have: order-independence, duplicate handling and an idempotent merge.
The reducer is a merge function, not a place for business logic. operator.add on lists is associative, which is what the runtime needs to combine writes consistently. What the reducer cannot promise is arrival order, and it cannot promise that two branches did not report the same finding. Those two concerns belong in the fan-in node, which reads the merged list and writes a normalised result to a separate channel.
The graph below has three branches from START, each with an edge to merge. That converging edge is the fan-in barrier, so merge runs once with all three findings present. It writes the sorted, deduplicated list to ordered_findings, a plain channel, rather than writing back into findings. Writing back into the append channel would append the normalised list to itself and quietly double the data on the next run.
from operator import add
from typing import Annotated, TypedDict
from langgraph.graph import END, START, StateGraph
class AuditState(TypedDict):
order_id: str
findings: Annotated[list[str], add]
ordered_findings: list[str]
def check_inventory(state: AuditState) -> dict:
return {"findings": ["inventory: only 2 of 3 items in stock"]}
def check_pricing(state: AuditState) -> dict:
return {"findings": ["pricing: order total matches quote"]}
def check_compliance(state: AuditState) -> dict:
return {"findings": ["compliance: refund window closed"]}
def merge(state: AuditState) -> dict:
"""Fan-in: normalise what the parallel writers appended."""
# Arrival order is not promised, so sort. Duplicates are possible, so set.
return {"ordered_findings": sorted(set(state["findings"]))}
builder = StateGraph(AuditState)
builder.add_node("check_inventory", check_inventory)
builder.add_node("check_pricing", check_pricing)
builder.add_node("check_compliance", check_compliance)
builder.add_node("merge", merge)
builder.add_edge(START, "check_inventory")
builder.add_edge(START, "check_pricing")
builder.add_edge(START, "check_compliance")
builder.add_edge("check_inventory", "merge")
builder.add_edge("check_pricing", "merge")
builder.add_edge("check_compliance", "merge")
builder.add_edge("merge", END)
graph = builder.compile()
result = graph.invoke(
{"order_id": "A17", "findings": [], "ordered_findings": []}
)
print(len(result["findings"]))
print(result["ordered_findings"])Printing len(result['findings']) is deliberate: it shows the reducer kept all three writes, while ordered_findings shows the deterministic view the rest of the graph should consume. If those two disagree, the fan-in is wrong; if the append channel has fewer than three entries, a reducer conflict happened before the merge ever ran.
A merge helper is easy to test in isolation, and the properties are worth naming explicitly. Order-independence says two different arrival orders produce the same result. Duplicate handling says the same finding reported twice appears once. Idempotency says running the merge on its own output changes nothing, which matters because a replay or a retried node can call it again.
import pytest
def merge_findings(findings: list[str]) -> list[str]:
"""Order-independent, duplicate-free, and safe to call twice."""
return sorted(set(findings))
def test_merge_is_order_independent():
left = ["b: pricing checked", "a: inventory checked"]
right = ["a: inventory checked", "b: pricing checked"]
assert merge_findings(left) == merge_findings(right)
def test_merge_collapses_duplicates():
assert merge_findings(
["a: inventory checked", "a: inventory checked", "b: pricing checked"]
) == ["a: inventory checked", "b: pricing checked"]
def test_merge_is_idempotent():
once = merge_findings(["b: pricing checked", "a: inventory checked"])
assert merge_findings(once) == once
def test_every_branch_finding_survives():
branches = [
["inventory: partial"],
["pricing: excluded"],
["compliance: review"],
]
merged = merge_findings(
[finding for branch in branches for finding in branch]
)
assert merged == ["compliance: review", "inventory: partial",
"pricing: excluded"]| Concern | Wrong move | Right move |
|---|---|---|
| Concurrent writes to one key | Leave the channel unreduced and hope the branches do not collide | Annotate the channel with a reducer such as operator.add |
| Arrival order | Rely on the order branches were registered | Sort or aggregate in the fan-in node; branch order is not a contract |
| Duplicates | Mutate the reducer's left list in place to dedupe | Keep the reducer append-only and deduplicate in the fan-in node |
| Replay or retry | Have the fan-in append to the channel it just read | Write the normalised result to a separate channel; keep the merge pure |
InvalidUpdateError.Key takeaways
Parallel branches writing one channel need an explicit reducer; without one the runtime raises InvalidUpdateError rather than guessing.
Edges converging on a node make it a fan-in barrier: it runs once after all branches, with every write merged.
The reducer answers concurrent writes; the fan-in answers order and duplicates. Keep those responsibilities separate.
Normalise into a separate channel instead of appending back into the reduced channel, or a replay can double the data.
Test the helper directly for order-independence, duplicate collapse and idempotency, then test the graph end to end for one entry per branch.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.