Branching & Conditional Routing
Complex routing logic in LangGraph. Conditional edges with multiple targets, LLM-based dynamic routing, parallel branches with merge, nested conditions, and the router node pattern for specialized sub-graphs.
Quick Reference
- →Conditional edges: add_conditional_edges(source, routing_function, path_map) routes to different nodes based on state. The routing function returns the name of the next node.
- →Multiple targets: a single conditional edge can route to 3+ nodes. Return different strings from the routing function for each case.
- →Dynamic routing: use an LLM with structured output to classify the input and decide which node to route to — no hardcoded if/else.
- →Parallel branches: use Send() to dispatch state to multiple nodes simultaneously. Results merge back through reducers on the state.
- →Nested conditions: compose routing by chaining conditional edges. Node A routes to B or C, then C routes to D or E based on different criteria.
Conditional Edges with Multiple Targets
A conditional edge lets you route execution to different nodes based on the current state. The routing function inspects the state and returns a string indicating which node to execute next. Unlike simple edges that always go from A to B, conditional edges act as switches: A goes to B, C, or D depending on the state. This is the fundamental building block for all complex routing in LangGraph.
Use Literal type hints on the return type of routing functions (e.g., Literal['refund', 'technical', 'general']). This makes the graph structure explicit, catches typos at type-check time, and helps LangGraph Studio visualize the possible paths.