Lesson 9 / 29
Fan-Out With Send (Map-Reduce)
Run the same node in parallel on many items and merge the results.
One task per item
When the number of parallel tasks is only known at run time (summarise each of N documents, check each of N claims), a conditional edge can return a list of Send(node, payload) objects. Each Send starts that node with its own private input, in parallel, and the results flow into shared state through a reducer (typically list-append). A later node then reduces them (merge, rank, answer). This map-reduce pattern avoids writing loops by hand and runs independent work concurrently, but remember rate limits and cost: N items means N model calls.
Summarise three documents in parallel, run
I ran this offline with langgraph 1.2.12 and langchain-core 1.6.6 in a Python virtual environment. No API key or model is needed because plain Python functions stand in for the model, so the output is repeatable. Three Send tasks run the summarise node, one per document. The results are collected with a list-append reducer; they are sorted before printing because parallel tasks can finish in any order.
import operator
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import Send
class State(TypedDict):
docs: list
summaries: Annotated[list, operator.add]
def fan_out(state):
return [Send("summarise", {"doc": d}) for d in state["docs"]] # one parallel task per document
def summarise(payload):
doc = payload["doc"]
return {"summaries": [f"{doc.split()[0]}...({len(doc.split())} words)"]}
g = StateGraph(State)
g.add_node("summarise", summarise)
g.add_conditional_edges(START, fan_out, ["summarise"])
g.add_edge("summarise", END)
out = g.compile().invoke({"docs": ["Leave policy has 24 days", "Travel policy caps hotels", "Security policy requires 2FA now"], "summaries": []})
print(sorted(out["summaries"]))
Output:
['Leave...(5 words)', 'Security...(5 words)', 'Travel...(4 words)']
Do not rely on parallel order
Results from parallel branches can arrive in any order. Sort or key them if order matters.
Quick check: When is Send most useful?
- To disable reducers
- For a fixed two-step chain
- When the number of parallel tasks is known only at run time
- To compile the graph
Answer
When the number of parallel tasks is known only at run time — Send creates one task per item dynamically.