Graph routing example
This walkthrough runs graph workflows with conditional edges, loops, and parallel join nodes. You verify branch order through trace metadata (GraphNodeStarted, GraphNodeCompleted). Scripts live under Graph routing walkthrough.
What this example demonstrates
Graph mode (Workflow(..., graph=True)) schedules nodes instead of a flat step list. Three patterns ship in the repo:
| Script | Pattern |
|---|---|
reflection_loop.py | Conditional loop with max_iterations guard |
parallel_search.py | Fan-out to two branches, join at synthesize |
react_agent.py | ReAct-style tool loop in graph form |
This document focuses on reflection and parallel search; open react_agent.py for tool-loop routing.
Prerequisites
| Item | Required |
|---|---|
| Python SDK | Built and importable |
| Provider | Stub default |
| Postgres | Optional for embedded SDK; required for recovery-heavy server paths |
| Reading | Graph workflows |
| Tutorial track | Track D |
Note: graph checkpoint resume remains partial (Graph recovery resume). Linear recovery is complete; do not rely on mid-graph resume in production.
Step 1: Reflection loop with conditional edge
Run:
Core graph wiring from the script:
The needs_more condition routes back to draft when step output matches; termination uses add_edge("revise", None).
Step 2: Parallel fan-out and join
Run:
Join configuration waits for both search branches:
Expected: router runs first, both search nodes run, synthesize runs after join preconditions are met.
Step 3: Verify trace branch order
For parallel search, confirm search_web and search_docs both appear before final synthesize completion events.
Expected output
Reflection loop:
run_id=<uuid> steps=<n>
Parallel search prints similar run_id and steps lines. Step count reflects graph iterations, not only node count. Values vary with routing and stub output.
Pass criteria:
| Check | Expected |
|---|---|
result.status | completed |
result.run_id | UUID |
| Graph trace kinds | Present for each executed node |
| Parallel join | Synthesize after both branches in trace order |
Trace events you should see
| Event kind | When |
|---|---|
WorkflowStarted | Graph run begins |
GraphNodeStarted | Each node begins (includes iteration metadata) |
GraphNodeCompleted | Node finishes |
StepStarted / StepCompleted | Underlying step execution per node |
WorkflowCompleted | Terminal success |
Conditional re-entry adds repeated GraphNodeStarted for the same node id across iterations until exit or max_iterations.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Infinite loop or early exit | Condition string mismatch | Ensure agent output trim matches edge condition values |
| Join never fires | Missing join_node or wrong branch ids | Match wait_for ids to node names in parallel_search pattern |
WorkflowExecutionError: max iterations | Loop guard hit | Increase max_iterations or fix termination edge |
| No graph events in trace | Old SDK build | Rebuild; confirm graph=True on Workflow |
Related
| Resource | Link |
|---|---|
| Tutorial track | Track D |
| Graph guide | Graph workflows |
| Maturity note | Graph recovery resume graph resume |