AI Hints
Home›LangGraph Tutorial›Parallel Branches and Schemas
LangGraph · Lesson 22 of 120

Parallel Branches

Fan out, fan in.

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

Some steps don't depend on each other. Checking stock in Bath, and in Bristol, can happen at the same time. In LangGraph, if one node has edges to several nodes, they all run together. Then a later node can wait for all of them.

Parallel Branches
From the video: Parallel Branches
  • Independent steps
  • Run at the same time
  • Then wait for all

Fan out, fan in

Start has edges to two nodes, so they run in parallel. Both write to found, which has operator add, so their results are combined, not overwritten. The edge from a list of nodes to summary means, wait for both. Run it. Both shops report, and the summary sees everything.

parallel.pypython
import operator
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    title: str
    found: Annotated[list[str], operator.add]
    summary: str

def bath(s):    return {"found": ["Bath: 2 copies"]}
def bristol(s): return {"found": ["Bristol: 1 copy"]}
def summary(s): return {"summary": f"{s['title']} -> " + "; ".join(sorted(s["found"]))}

b = StateGraph(State)
for fn in [bath, bristol, summary]: b.add_node(fn)
b.add_edge(START, "bath"); b.add_edge(START, "bristol")
b.add_edge(["bath", "bristol"], "summary"); b.add_edge("summary", END)
print(b.compile().invoke({"title": "Dune", "found": []})["summary"])
Output
Dune -> Bath: 2 copies; Bristol: 1 copy

Parallel rules

Nodes that run together are in the same step. If they write the same key, it needs a reducer, or LangGraph raises an error, because it can't know which value wins. Parallel branches make slow work, like model calls, much faster.

keyvalue
Same steprun together
Shared keyneeds a reducer
Wait for alledge from a list
Fasterfor slow calls

Summary

Edges to several nodes run them in parallel, and an edge from a list waits for all of them. Shared keys need a reducer. Next, controlling what goes in and out of a graph.

Key takeaways
  • Fan out from one node
  • Reducer for shared keys
  • Fan in from a list
Next up: Input and Output Schemas — public and private