chore: import upstream snapshot with attribution
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
Continuous Integration / Pre-commit Linter (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.10) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.11) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.12) (push) Waiting to run
Continuous Integration / Mypy Check (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.10) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.11) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.12) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.13) (push) Waiting to run
Continuous Integration / Unit Tests (Python 3.14) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Waiting to run
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Waiting to run
Copybara PR Handler / close-imported-pr (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# ADK Workflow Routing Sample
|
||||
|
||||
## Overview
|
||||
|
||||
This sample demonstrates how to use routing in **ADK Workflows**.
|
||||
|
||||
It takes user input and uses an LLM node to categorize it as a **question**, a **statement**, or **other**. Based on the classification, it appropriately routes the execution to a specialized agent or function to handle that specific type of input.
|
||||
|
||||
In ADK Workflows, **routing** allows conditionally executing different execution paths based on the output of a previous node.
|
||||
|
||||
## Sample Inputs
|
||||
|
||||
- `What is the capital of France?`
|
||||
|
||||
- `The weather is very nice today.`
|
||||
|
||||
- `Translate bonjour to english`
|
||||
|
||||
## Graph
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
START --> process_input
|
||||
process_input --> classify_input
|
||||
classify_input --> route_on_category
|
||||
route_on_category -->|question| answer_question
|
||||
route_on_category -->|statement| comment_on_statement
|
||||
route_on_category -->|other| handle_other
|
||||
```
|
||||
|
||||
## How To
|
||||
|
||||
1. A node (agent or function) yields an `Event` with a specific route name:
|
||||
|
||||
```python
|
||||
yield Event(route="your_route_name")
|
||||
```
|
||||
|
||||
1. In the `Workflow` edges definition, conditional edges are constructed using a routing map dict as the second element of the edge tuple:
|
||||
|
||||
```python
|
||||
(source_node, {"your_route_name": target_node})
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright 2026 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from google.adk import Agent
|
||||
from google.adk import Event
|
||||
from google.adk import Workflow
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class InputCategory(BaseModel):
|
||||
category: Literal["question", "statement", "other"]
|
||||
|
||||
|
||||
def process_input(node_input: str):
|
||||
return Event(state={"input": node_input})
|
||||
|
||||
|
||||
classify_input = Agent(
|
||||
name="classify_input",
|
||||
instruction=(
|
||||
"Based on this input, decide which category it belongs to: {input}"
|
||||
),
|
||||
output_schema=InputCategory,
|
||||
output_key="category",
|
||||
)
|
||||
|
||||
|
||||
def route_on_category(category: InputCategory):
|
||||
"""Yields an Event with a specific route based on the classification."""
|
||||
yield Event(route=category.category)
|
||||
|
||||
|
||||
answer_question = Agent(
|
||||
name="answer_question",
|
||||
instruction="""Answer the question: {input}""",
|
||||
)
|
||||
|
||||
|
||||
comment_on_statement = Agent(
|
||||
name="comment_on_statement",
|
||||
instruction="""Comment on the statement: {input}""",
|
||||
)
|
||||
|
||||
|
||||
def handle_other():
|
||||
yield Event(
|
||||
message="Sorry I can only anwer questions or comment on statements."
|
||||
)
|
||||
|
||||
|
||||
root_agent = Workflow(
|
||||
name="root_agent",
|
||||
edges=[
|
||||
("START", process_input, classify_input, route_on_category),
|
||||
(
|
||||
route_on_category,
|
||||
{
|
||||
"question": answer_question,
|
||||
"statement": comment_on_statement,
|
||||
"other": handle_other,
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,106 @@
|
||||
{
|
||||
"appName": "route",
|
||||
"events": [
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "who are you"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-1",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"actions": {
|
||||
"stateDelta": {
|
||||
"input": "who are you"
|
||||
}
|
||||
},
|
||||
"author": "root_agent",
|
||||
"id": "e-2",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "root_agent@1/process_input@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"actions": {
|
||||
"stateDelta": {
|
||||
"category": {
|
||||
"category": "question"
|
||||
}
|
||||
}
|
||||
},
|
||||
"author": "classify_input",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "{\"category\": \"question\"}"
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-3",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"messageAsOutput": true,
|
||||
"outputFor": [
|
||||
"root_agent@1/classify_input@1"
|
||||
],
|
||||
"path": "root_agent@1/classify_input@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"actions": {
|
||||
"route": "question"
|
||||
},
|
||||
"author": "root_agent",
|
||||
"id": "e-4",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "root_agent@1/route_on_category@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "answer_question",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "I am a large language model, trained by Google."
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-5",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"messageAsOutput": true,
|
||||
"outputFor": [
|
||||
"root_agent@1/answer_question@1",
|
||||
"root_agent@1"
|
||||
],
|
||||
"path": "root_agent@1/answer_question@1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"id": "f1ec33bf-99ef-49a5-b64a-e4d31f85db85",
|
||||
"state": {
|
||||
"__session_metadata__": {
|
||||
"displayName": "who are you"
|
||||
},
|
||||
"category": {
|
||||
"category": "question"
|
||||
},
|
||||
"input": "who are you"
|
||||
},
|
||||
"userId": "user"
|
||||
}
|
||||
Reference in New Issue
Block a user