Independent work does not need to wait for unrelated work to finish. Parallel branches in a LangGraph graph let multiple nodes run in the same superstep, then pass their combined results to a joining node.
Each branch reads the state available at the start of its superstep. When branches update the same state key, a reducer combines their updates before the next node runs; without one, concurrent writes to that key raise an error. Completion order can vary, so a joining node should not depend on which branch finishes first.
Fixed fan-out fits a known set of independent tasks, such as counting words and converting the same text to uppercase. A Python environment with LangGraph installed is sufficient for these operations, without a language model or an API key.
Steps to add parallel branches to a LangGraph graph:
- Create a file named parallel_branches.py in your text editor containing the shared state schema.
- parallel_branches.py
import operator from typing import Annotated, TypedDict from langgraph.graph import END, START, StateGraph class State(TypedDict): text: str results: Annotated[list[str], operator.add] summary: str
The text key holds the input, results collects branch output, and summary holds the joined response. The reducer on results concatenates incoming lists instead of replacing the existing list.
- Append the two branch functions after the State class.
def count_words(state: State): return {"results": [f"words={len(state['text'].split())}"]} def uppercase(state: State): return {"results": [f"upper={state['text'].upper()}"]}
Both functions read text independently and return only their own update to results. Returning the whole state would also write unchanged keys, which can cause concurrent-update conflicts.
- Append a joining function that formats the combined branch results.
def join_results(state: State): return {"summary": " | ".join(sorted(state["results"]))}
Sorting gives the summary a stable order without relying on branch completion order. Only this node writes summary, so that key does not need a reducer.
- Append the graph builder and node registrations.
builder = StateGraph(State) builder.add_node("count_words", count_words) builder.add_node("uppercase", uppercase) builder.add_node("join_results", join_results)
- Append the fan-out edges, the waiting join and the graph compilation.
builder.add_edge(START, "count_words") builder.add_edge(START, "uppercase") builder.add_edge(["count_words", "uppercase"], "join_results") builder.add_edge("join_results", END) graph = builder.compile()
The two edges from START schedule both branches in the first superstep. The list of source nodes on the joining edge makes join_results wait for both named nodes. The edge to END finishes the run after the summary is produced.
- Append a streaming call that displays node updates and merged state.
for mode, data in graph.stream( {"text": "parallel work", "results": [], "summary": ""}, stream_mode=["updates", "values"], ): if mode == "updates": for node, update in data.items(): print(node, update) else: print("results:", data["results"]) if data["summary"]: print("summary:", data["summary"])
The input starts with an empty results list. The updates stream reports each node's returned values, while values reports the state after each superstep.
- Save parallel_branches.py in the project directory containing your LangGraph environment.
- Verify the branch results by running parallel_branches.py from your activated LangGraph environment.
$ python3 parallel_branches.py results: [] count_words {'results': ['words=2']} uppercase {'results': ['upper=PARALLEL WORK']} results: ['words=2', 'upper=PARALLEL WORK'] join_results {'summary': 'upper=PARALLEL WORK | words=2'} results: ['words=2', 'upper=PARALLEL WORK'] summary: upper=PARALLEL WORK | words=2The two branch-update lines can appear in either order, but they must sit together between the first two results: lines, because values prints the state once per superstep. An extra results: line between them means the branches ran one after the other. The merged results must hold both values before join_results runs, and the final summary must contain both. A missing branch value or a concurrent-update exception means the fan-out and join did not complete.