AI Hints
Home›LangGraph Tutorial›Send and Map-Reduce
LangGraph · Lesson 64 of 120

Send

Dynamic fan-out.

⏱ 2 min read▶ Video at 1:08:01🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Parallel edges are fixed when you build the graph. But what if you don't know how many branches you need? A basket might hold two books, or twenty. Send creates one branch per item, while the graph runs.

Send
From the video: Send
  • Fixed parallel edges
  • Unknown number of items
  • Send: one branch each

One branch per book

The fan out function returns a list of sends. Each send names a node, and gives it its own small input, here one title. The check node runs once per send, in parallel, and adds its result to a shared list. Run it. Three titles create three branches, and every result is collected.

send.pypython
import operator
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START
from langgraph.types import Send

STOCK = {"Dune": 3, "Circe": 0, "Emma": 5}

class State(TypedDict):
    titles: list[str]
    results: Annotated[list[str], operator.add]

def fan_out(s):
    return [Send("check", {"title": t}) for t in s["titles"]]

def check(item):
    return {"results": [f"{item['title']}: {STOCK.get(item['title'], 0)}"]}

b = StateGraph(State); b.add_node(check); b.add_conditional_edges(START, fan_out, ["check"])
print(b.compile().invoke({"titles": ["Dune", "Circe", "Emma"], "results": []})["results"])
Output
['Dune: 3', 'Circe: 0', 'Emma: 5']

How Send works

Send takes a node name, and the input for that one run. The node receives that input, not the whole state. Its return value still goes through the parent state's reducers. And you can send to the same node as many times as you need.

keyvalue
Send(node, input)one branch
Node getsits own input
Resultsthrough reducers
Countdecided at run time

Summary

Send creates one parallel branch per item, decided while the graph runs. Results are combined by a reducer. Next, the full map reduce pattern.

Key takeaways
  • Return a list of Sends
  • Each gets its own input
  • Reducer collects
Next up: Map-Reduce with Send — many in parallel