LangGraph's parallel edge model lets you fan out from a single node to multiple independent branches that execute concurrently, then converge at a single fan-in node. This notebook models an employee onboarding pipeline where HR, Finance, and Infra tasks run in parallel after initiation — rather than sequentially — then merge at a final verification step. The pattern maps directly to any real-world workflow where independent tasks have no data dependency on each other but must all complete before a downstream decision can be made.
Code Snippet:
from langgraph.graph import StateGraph, START, END # Fan-out: all three branches start as soon as "init" completes wf_state.add_edge("init", "infra_p") wf_state.add_edge("init", "hr_p") wf_state.add_edge("init", "finance_p") # Fan-in: verify waits for all three branches to finish wf_state.add_edge("infra_p", "verify") wf_state.add_edge("hr_p", "verify") wf_state.add_edge("finance_p", "verify") def onboarding_verification(state: EmployeeOnBoardingState): # All branch outputs are already merged into state before this runs onboarding_status = "Success" if all([ state["emp_id"], state["joining_kit"], state["email_id"], state["laptop_assigned"], state["bank_acc_linked"], ]) else "Pending" return {"status": onboarding_status}Fan-out via multiple edges from one node: Adding three add_edge("init", ...) calls tells LangGraph to dispatch all three branches the moment init completes — no manual threading or async coordination needed.
State acts as the shared accumulator: Each parallel node writes only its own slice (email_id, joining_kit, bank_acc_linked) — because TypedDict fields don't overlap, there are no write conflicts.
Fan-in is implicit: LangGraph holds the verify node until all incoming edges have resolved. The graph runtime handles synchronization; the node just reads a fully merged state.
Verification is deterministic: onboarding_verification checks all required fields in one place — it doesn't need to know which branch produced which value.
Tradeoff: Flat TypedDict state works cleanly here because branches write disjoint keys. If branches wrote the same key, you'd need an Annotated reducer to avoid last-write-wins collisions.
Full Code Reference: Parallel Workflow

No comments:
Post a Comment