Module 19: Structured Routing - Dictionary Edges and ctx.route
๐งญ Where We Are in Part 3: In Module 18, you wrote
if/elsebranching inside a@nodefunction โ powerful for multi-step chains, but because those branches live inside Python code, the static Dev UI Graph View only sees a single@nodebox rather than displaying every downstream specialist! Here in Module 19, you combine the@nodeskill you just mastered with Dictionary Router Edges (ctx.route) so every specialist branch appears explicitly on your visual workflow graph!
Figure: High-speed intent routing through structured decision dictionaries and deterministic edge resolvers.
Theoryโ
Module 18 (if/else) vs. Module 19 (Dictionary Edges)โ
Why use a Dictionary Edge instead of writing if/else inside @node?
- Full Graph View Transparency: Every target branch (
tech_agent,billing_agent,general_agent) is wired directly inedges=[...], giving architects and auditors a 1:1 visual map in the Dev UI Graph View. - Declarative Downstream Pipelines: Each dictionary branch can continue into further static chains or
JoinNodes (from Module 17) without writing any extra Python orchestration. - Zero Routing Hallucinations: Combined with a Pydantic
Literal[...]schema, your graph can only follow valid, pre-declared branches.
Figure: 4-stage pipeline showing query classification, route key emission, edge dictionary mapping, and specialist execution.
Router Edges (Conditional Dictionary Routing)โ
Think of an office switchboard that routes incoming requests to the right department using a simple lookup table (a Python dictionary) inside edges:
edges=[
("START", switchboard_router),
(switchboard_router, {
"technical": tech_agent,
"billing": billing_agent,
"other": general_agent,
}),
]
The Switchboard Analogy: Putting @node + ctx.route Togetherโ
Suppose a customer writes: "I was charged twice on my invoice!"
Gemini (classifier_node) is great at reading that message and replying {"category": "billing"}. However, an Agent only generates text or JSON โ it doesn't have hands to turn the switchboard dial.
Using the exact same @node + await ctx.run_node(...) pattern you learned in Module 18, we write a tiny 4-line switchboard operator (switchboard_router):
- It asks Gemini which department should handle the message (
await ctx.run_node(...)). - It dials that department's extension (
ctx.route = result["category"]). - It transfers the customer on the line (
return node_input) so the specialist hears the real question, not just the word"billing".
Think of it in one sentence:
ctx.routeis the extension number you dial;return node_inputis the customer on the line you connect to that extension.
from pydantic import BaseModel
from typing import Literal
from google.adk import Agent, Workflow, Context
from google.adk.workflow import node
class RequestType(BaseModel):
category: Literal["technical", "billing", "other"]
# 1. The Classifier Agent
classifier_node = Agent(
name="intent_classifier",
model="gemini-3.8-flash",
instruction="Classify the request as 'technical', 'billing', or 'other'.",
output_schema=RequestType,
)
# 2. The Switchboard Operator (@node)
@node(rerun_on_resume=True)
async def switchboard_router(ctx: Context, node_input: str):
result = await ctx.run_node(classifier_node, node_input) # Plain dict from output_schema
ctx.route = result["category"] # Dial the extension
return node_input # Transfer the customer's prompt
# 3. The Workflow Graph
my_workflow = Workflow(
name="SmartRouter",
edges=[
("START", switchboard_router),
(switchboard_router, {
"technical": tech_agent,
"billing": billing_agent,
"other": general_agent,
}),
],
)
Line-by-Line Recapโ
classifier_node = Agent(..., output_schema=RequestType): Reads the message and returns guaranteed JSON matchingRequestType(e.g.,{"category": "billing"}).@node(rerun_on_resume=True): Marks the Python function as a graph step that re-runs on every new chat turn.async def switchboard_router(ctx: Context, node_input: str):: Receives the workflow control panel (ctx) and the user's message (node_input).result = await ctx.run_node(classifier_node, node_input): Runsclassifier_nodeand returns its JSON as a plain Pythondict({"category": "billing"}).ctx.route = result["category"]: The one new line in Module 19! Sets the routing signal ("billing"), telling ADK which key in the dictionary edge to activate.return node_input: Passes the customer's original message untouched tobilling_agent(instead of overwriting it with{"category": "billing"}).
Reserved Graph Keywords ("START" and "END")โ
"START": Mandatory entry point of everyWorkflow."END": Optional explicit exit target; any node with no outgoing edges terminates its branch automatically.
In a `Workflow` with a dictionary router edge `(switchboard_router, {'technical': tech_agent, 'billing': billing_agent})`, why must `switchboard_router` both set `ctx.route` AND `return node_input`?
Key Takeawaysโ
- Dictionary Router Edges make conditional 1-to-N branching 100% visible in
edgesand in the Dev UI Graph View. ctx.routeselects the dictionary branch (extension number), whilereturn node_inputforwards the user's message (customer on the line).- Use Module 18 (
if/elsein@node) for dynamic multi-step compositions, and Module 19 (Dictionary Edges) for clean, auditable graph switches!