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

Practice: Review Summaries

Send in action.

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

Let's practise map reduce with a model. A book has several customer reviews. We'll summarise each one in parallel, then ask the model for one overall verdict.

Practice: Review Summaries
From the video: Practice: Review Summaries
  • Several reviews
  • Summarise each in parallel
  • One overall verdict

Summarising reviews

Fan out sends each review to the summarise node. Each branch asks the model for a few words, in parallel. The verdict node waits for every summary, and asks for one sentence. Run it. We see a short summary per review, then a single verdict that weighs them together.

review_summaries.pypython
import operator
from typing import Annotated, TypedDict
from langchain.chat_models import init_chat_model
from langgraph.graph import StateGraph, START
from langgraph.types import Send
from reviews import REVIEWS
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
class State(TypedDict):
    reviews: list[str]
    notes: Annotated[list[str], operator.add]
    verdict: str

def fan_out(s): return [Send("summarise", {"review": r}) for r in s["reviews"]]
def summarise(x): return {"notes": [model.invoke(f"Summarise in 5 words: {x['review']}").text.strip()]}
def verdict(s): return {"verdict": model.invoke(f"One-sentence verdict from these notes: {s['notes']}").text}
b = StateGraph(State); b.add_node(summarise); b.add_node(verdict)
b.add_conditional_edges(START, fan_out, ["summarise"]); b.add_edge("summarise", "verdict")
out = b.compile().invoke({"reviews": REVIEWS, "notes": []})
print(*out["notes"], "", out["verdict"], sep="\n")
Output
Captivating world, slow start, unputdownable.
Boring, confusing, and unfinished.
Desert classic, brilliantly rich characters.

Despite a slow and confusing start for some, this desert-set novel ultimately delivers a
captivating, unputdownable experience driven by brilliantly rich characters.
Also uses reviews.py
REVIEWS = [
    "Couldn't put it down. The world-building is incredible, though the start is slow.",
    "Too long and the politics confused me. I gave up halfway.",
    "A classic for a reason. Rich characters and a brilliant desert setting.",
]

What you practised

You used Send to fan out model calls, a reducer to collect them, and a reduce node for the verdict. Because the summaries run in parallel, ten reviews take about as long as one.

keyvalue
Sendone call per review
Reducercollects notes
Reduce nodethe verdict
Parallel10 ≈ 1 in time

Summary

Map reduce with Send makes many model calls in parallel, then combines them. Next, Command, which updates state and chooses the next node in one go.

Key takeaways
  • Fan out model calls
  • Collect with a reducer
  • One verdict
Next up: Command — update and go