Two paths at once
Two edges out of one point, and both nodes run. Not one after the other, but in the same step.
Support work is full of this. Before answering a billing complaint you want the account status and the payment history, and neither lookup needs the other's answer. Doing them one after the other is only slower.
A key for each result
Three keys. One for each check, and one for the summary. They write to different keys on purpose, and the next lesson is about what happens when they do not.
from typing_extensions import TypedDict
class State(TypedDict):
account: str
payments: str
summary: strTwo checks and something to join them
def check_account(state):
return {"account": "active"}
def check_payments(state):
return {"payments": "two charges on 3 March"}def summarise(state):
return {"summary": f"account {state['account']}, payments: {state['payments']}"}This one reads both keys, which only works because both checks have finished by the time it runs.
Two edges out, two edges in
from langgraph.graph import StateGraph, START
builder = StateGraph(State)
builder.add_node("check_account", check_account)
builder.add_node("check_payments", check_payments)
builder.add_node("summarise", summarise)builder.add_edge(START, "check_account")
builder.add_edge(START, "check_payments")
builder.add_edge("check_account", "summarise")
builder.add_edge("check_payments", "summarise")The first two edges are the fan out. Both nodes are ready at once, so both run in the same step. The last two are the fan in, and a node waits for every incoming edge before it runs.
print(builder.compile().invoke({"account": "", "payments": "", "summary": ""})["summary"])Two things worth noticing
Both results were there when summarise ran. That is the point of fanning out and coming back together. The work happened side by side and the results met in one dictionary.
summarise ran once, not twice. Two edges point at it, so you might expect two runs. A node waits for every incoming edge to finish, then runs a single time.
async def and it genuinely overlaps. For plain Python like this, the benefit is the shape of the program, not the speed.- Print the graph's edges and find the two leaving
__start__. - Add a third check that runs alongside the other two.
- Have
check_paymentstry to readstate["account"]and see what it gets.
This is what real progress feels like.