> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional Routing

> Explains conditional routing in workflows where a router node selects labeled branches for messages, mapping routes to handler nodes and using a fallback for unknown inputs.

You now have a support workflow — but today it behaves like a straight line: every incoming message runs through the exact same sequence of nodes and an answer comes out the other side. That design treats all messages identically.

Monday arrives and different customers send different problems:

* "Why was I charged twice this month?"
* "The app crashes every time I hit Save."
* "Where do I find my past orders?"

These are distinct issues, yet the current pipeline sends them down a single path: billing checks receive crash reports, technical steps look for duplicate charges, and the general handler sits idle. To route different messages to the right handler, you need a router node.

A router is a node that inspects input and selects a branch. It behaves like a function node — it runs code — but instead of returning data for the next node to process, it returns a route label: a short string that tells the runtime which branch to take.

Key points:

* Router returns an Event (or similar) with a `route` field set to a route label (string).
* The workflow must declare a route map that maps route labels to target nodes.
* Always include a fallback route (for example, `"GENERAL"`) so unknown inputs still get processed.

Always provide a fallback route so the workflow can handle unexpected or unknown inputs without failing.

## Example router function (Python)

This simple router looks for keywords and returns matching route labels:

```python theme={null}
from google.adk import Event

def route_message(node_input: str) -> Event:
    text = node_input.lower()
    if "charged" in text:
        return Event(route="BILLING")
    if "crash" in text or "crashes" in text:
        return Event(route="TECHNICAL")
    return Event(route="GENERAL")
```

Notes:

* The router returns an `Event` whose `route` field is a string label.
* Route labels are compared exactly (case-sensitive) against the route map keys — be consistent in casing.

## Mapping routes to nodes

A sequential edge is simply a list of nodes to run in order. A routing edge contains the router node and a dictionary that maps route labels to destination nodes. You declare all branches up front in the workflow's edges so the runtime knows every allowed path before messages arrive.

Example workflow definition with a route map:

```python theme={null}
from google.adk import Workflow, START

workflow = Workflow(
    name="support_router",
    edges=[
        # Run START -> process_message -> route_message
        (START, process_message, route_message),
        # route_message maps route labels to destination nodes
        (route_message, {
            "BILLING": billing_handler,
            "TECHNICAL": technical_handler,
            "GENERAL": general_handler,
        }),
    ],
)
```

Table: route map example

| Route label | Handler node |
| - | - |
| `BILLING` | `billing_handler` |
| `TECHNICAL` | `technical_handler` |
| `GENERAL` | `general_handler` |

Remember: the dictionary keys (route labels) must match the string returned by the router exactly.

## Visualization: how the graph changes

Think of the graph transforming from a single line into a fork:

start → process\_message → Router → \{ BILLING → billing\_handler, TECHNICAL → technical\_handler, GENERAL → general\_handler }

When a message flows through:

* A customer sends: "I was charged twice for this same order this month."\
  The workflow runs START → process\_message → route\_message. The router inspects the text and returns `route="BILLING"`. The runtime looks up `"BILLING"` in the route map and runs only the billing branch; the technical and general handlers are not executed.

![A dark-themed flowchart titled "A Billing Message" with a customer complaint reading "I was charged twice for this same order this month." It shows a start → process\_message → Router flow that branches to Billing, Technical, and General handlers.](https://mintcdn.com/kodekloud-c4ac6d9a/heceQKBf52UJgxP-/images/Google-ADK-20/ADKs-Workflow-Runtime/Conditional-Routing/billing-duplicate-charge-flowchart.jpg?fit=max\&auto=format\&n=heceQKBf52UJgxP-\&q=85\&s=41466d28098f5789cf605058710d62ab)

* Marcus sends: "The app crashes every time I hit Save."\
  The router returns `route="TECHNICAL"`, and the runtime follows the technical branch. The same graph and same code run — only the router's returned label differs.

## Best practices and common pitfalls

* Use clear, consistent route labels (e.g., uppercase constants like `"BILLING"`). Route label matching is exact — `"billing"` ≠ `"BILLING"`.
* Always include a fallback route (e.g., `"GENERAL"`) to avoid dead-ends for unexpected input.
* Keep router logic focused on routing decisions (keywords, intent classification, simple heuristics). Heavy processing should remain in handler nodes.
* Log the chosen route for observability and easier debugging.
* If you support many routes, consider using structured classification (ML model or intent detection) rather than a large chain of keyword checks.

## Quick checklist

* [ ] Router returns an `Event` with a `route` string.
* [ ] Workflow edges include a route map that lists all allowed labels.
* [ ] Include a fallback route like `"GENERAL"`.
* [ ] Route label keys and router return values match exactly (case-sensitive).
* [ ] Handlers are focused on resolving the routed issue.

What you implemented is conditional routing: the graph itself remains static (branches declared up front), while the runtime selects the predefined path that best fits each incoming message.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/google-adk-2-0/module/913ea931-c1bd-4df3-8a96-c2894e9df0d2/lesson/1991faaf-888e-4ad1-bf75-c8ca17855dc1)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.