chore: import upstream snapshot with attribution
Continuous Integration / Pre-commit Linter (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.10) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.11) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.12) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.12) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Has been cancelled
Copybara PR Handler / close-imported-pr (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / Pre-commit Linter (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.10) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.11) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.12) (push) Has been cancelled
Continuous Integration / Mypy Check (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.12) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / Unit Tests (Python 3.14) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.10) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.11) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.12) (push) Has been cancelled
Copybara PR Handler / close-imported-pr (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.13) (push) Has been cancelled
Continuous Integration / A2A v0.3 Tests (Python 3.14) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
# ADK Workflow use_as_output Sample
|
||||
|
||||
## Overview
|
||||
|
||||
This sample demonstrates how to use `ctx.run_node(node, use_as_output=True)` to delegate a node's output to a dynamically executed child node.
|
||||
|
||||
When `use_as_output=True` is set, the child node's output replaces the parent's output. The parent's own output event is suppressed to avoid duplication, and the child's output flows downstream through the graph as if the parent produced it.
|
||||
|
||||
The child node can be any node type — this sample uses a single_turn LLM agent (`summarizer`) as the delegated child.
|
||||
|
||||
## Sample Inputs
|
||||
|
||||
- `The quick brown fox jumped over the lazy dog near the riverbank on a warm summer afternoon`
|
||||
|
||||
## Graph
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
START --> orchestrate
|
||||
orchestrate -.->|delegates output via ctx.run_node| summarizer
|
||||
summarizer --> finalize
|
||||
```
|
||||
|
||||
## How To
|
||||
|
||||
1. **Define the child node**: The child can be a function or an LLM agent. Its output becomes the parent's output and flows to downstream nodes.
|
||||
|
||||
```python
|
||||
from google.adk import Agent
|
||||
|
||||
summarizer = Agent(
|
||||
name='summarizer',
|
||||
model='gemini-2.5-flash',
|
||||
instruction='Summarize the following text in one sentence.',
|
||||
)
|
||||
```
|
||||
|
||||
1. **Mark the orchestrator as rerun_on_resume**: The parent node that calls `ctx.run_node` must use `@node(rerun_on_resume=True)`.
|
||||
|
||||
```python
|
||||
from google.adk.workflow import node
|
||||
|
||||
@node(rerun_on_resume=True)
|
||||
async def orchestrate(ctx: Context, node_input: str) -> str:
|
||||
return await ctx.run_node(
|
||||
summarizer, node_input=node_input, use_as_output=True
|
||||
)
|
||||
```
|
||||
|
||||
1. **Downstream receives delegated output**: The `finalize` node receives the LLM's summary as its `node_input`, not the parent's.
|
||||
|
||||
```python
|
||||
def finalize(node_input: str) -> str:
|
||||
return f'final: {node_input}'
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
# 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 google.adk import Agent
|
||||
from google.adk import Context
|
||||
from google.adk.workflow import node
|
||||
from google.adk.workflow import Workflow
|
||||
from google.adk.workflow._base_node import START
|
||||
|
||||
summarizer = Agent(
|
||||
name='summarizer',
|
||||
instruction='Summarize the following text in one sentence.',
|
||||
)
|
||||
|
||||
|
||||
@node(rerun_on_resume=True)
|
||||
async def orchestrate(ctx: Context, node_input: str) -> str:
|
||||
return await ctx.run_node(
|
||||
summarizer, node_input=node_input, use_as_output=True
|
||||
)
|
||||
|
||||
|
||||
def finalize(node_input: str) -> str:
|
||||
return f'final: {node_input}'
|
||||
|
||||
|
||||
root_agent = Workflow(
|
||||
name='root_agent',
|
||||
edges=[(START, orchestrate, finalize)],
|
||||
)
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"appName": "use_as_output",
|
||||
"events": [
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "go"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-1",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "summarizer",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "Please provide the text you would like me to summarize!"
|
||||
}
|
||||
],
|
||||
"role": "model"
|
||||
},
|
||||
"finishReason": "STOP",
|
||||
"id": "e-2",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"messageAsOutput": true,
|
||||
"outputFor": [
|
||||
"root_agent@1/orchestrate@1/summarizer@1",
|
||||
"root_agent@1/orchestrate@1"
|
||||
],
|
||||
"path": "root_agent@1/orchestrate@1/summarizer@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "root_agent",
|
||||
"id": "e-3",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"outputFor": [
|
||||
"root_agent@1/finalize@1",
|
||||
"root_agent@1"
|
||||
],
|
||||
"path": "root_agent@1/finalize@1"
|
||||
},
|
||||
"output": "final: Please provide the text you would like me to summarize!"
|
||||
}
|
||||
],
|
||||
"id": "ba40366e-0563-469f-a4b2-09e007adf6ff",
|
||||
"state": {
|
||||
"__session_metadata__": {
|
||||
"displayName": "go"
|
||||
}
|
||||
},
|
||||
"userId": "user"
|
||||
}
|
||||
Reference in New Issue
Block a user