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,81 @@
|
||||
# ADK Workflow Message Sample
|
||||
|
||||
## Overview
|
||||
|
||||
This sample demonstrates different ways to send a message to a user using `Event(message=...)` within an **ADK Workflows** node. It covers:
|
||||
|
||||
1. **String Messages**: Standard string text replies.
|
||||
1. **Multi-modal Messages**: Returning mixed modality inputs, such as a string combined with an inline image.
|
||||
1. **Multiple Messages**: Emitting multiple full messages from the same node with a delay between them.
|
||||
1. **Streaming Messages**: Simulating an LLM streaming response by breaking a message into chunks and yielding them with the `partial=True` flag at intervals.
|
||||
|
||||
## Sample Inputs
|
||||
|
||||
This workflow executes sequentially and successfully without any expected user input. Since it has only one `Workflow` node chain that automatically progresses from `START`, you can just type anything (e.g. `start`) to kick it off.
|
||||
|
||||
## Graph
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
START --> send_string
|
||||
send_string --> send_multimodal
|
||||
send_multimodal --> multiple_messages
|
||||
multiple_messages --> stream_sentence
|
||||
stream_sentence --> END[Workflow Ends]
|
||||
```
|
||||
|
||||
## How To
|
||||
|
||||
To send messages in an ADK node, yield an `Event` object with the `message` argument:
|
||||
|
||||
1. **Send a simple string**:
|
||||
|
||||
```python
|
||||
yield Event(message="Hello world!")
|
||||
```
|
||||
|
||||
1. **Send text with an image** (multi-modal):
|
||||
|
||||
```python
|
||||
from google.genai import types
|
||||
yield Event(
|
||||
message=[
|
||||
types.Part.from_text(text="Look at this image:"),
|
||||
types.Part.from_bytes(data=image_bytes, mime_type="image/png"),
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
1. **Send multiple messages**:
|
||||
To send multiple distinct messages from a single node, yield multiple `Event` objects sequentially.
|
||||
|
||||
> **Note**: When yielding multiple messages with delays (`await asyncio.sleep(...)`), your node function **must be an asynchronous generator** (`async def`). This allows ADK to yield each message to the client immediately without blocking.
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
|
||||
async def multiple_messages(node_input: Any = None):
|
||||
yield Event(message="Processing step 1...")
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
yield Event(message="Processing step 2...")
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
yield Event(message="Done processing.")
|
||||
```
|
||||
|
||||
1. **Stream a message in chunks**:
|
||||
Provide the `partial=True` flag for intermediate chunks. This provides a better user experience by allowing the UI to show the response in a streaming fashion, thereby lowering the latency to see the first word. ADK automatically accumulates all partial messages and merges them into a final message for you for session storage.
|
||||
|
||||
> **Note**: To stream multiple messages or tokens smoothly, your node function **must be an asynchronous generator** (`async def`). This allows ADK to yield messages to the client immediately without blocking.
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
|
||||
async def stream_sentence(node_input: str):
|
||||
yield Event(message="How ", partial=True)
|
||||
await asyncio.sleep(0.5)
|
||||
yield Event(message="may I", partial=True)
|
||||
await asyncio.sleep(0.5)
|
||||
yield Event(message=" help you?", partial=True)
|
||||
```
|
||||
@@ -0,0 +1,104 @@
|
||||
# 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.
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from google.adk import Event
|
||||
from google.adk import Workflow
|
||||
from google.genai import types
|
||||
|
||||
|
||||
async def sleep_if_not_pytest(seconds: float):
|
||||
if "PYTEST_CURRENT_TEST" not in os.environ:
|
||||
await asyncio.sleep(seconds)
|
||||
|
||||
|
||||
def send_string(node_input: Any = None):
|
||||
"""Sends a single string message."""
|
||||
yield Event(message="#1 This is a simple string message.")
|
||||
|
||||
|
||||
def send_multimodal(node_input: Any = None):
|
||||
"""Sends a multi-modal message containing a string and an inline image."""
|
||||
# A 16x16 solid red PNG base64 encoded
|
||||
red_square_png = base64.b64decode(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAXElEQVR4nO2TSQ7AIAwD"
|
||||
"7fz/z+ZQtapwmrJc8QklmjBIgZJgIZMiAIl9KYbhjx4fgwosbNxgMrF0+4uhgHnYDM6"
|
||||
"AzQHJeg5HYtyHFfgy2AztN/5tZWfrBtVzkl4DzfQkEPd+cEkAAAAASUVORK5CYII="
|
||||
)
|
||||
yield Event(
|
||||
message=[
|
||||
types.Part.from_text(
|
||||
text=(
|
||||
"#2 Here is a multi-modal message with an inline image (red"
|
||||
" circle):"
|
||||
)
|
||||
),
|
||||
types.Part.from_bytes(data=red_square_png, mime_type="image/png"),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def multiple_messages(node_input: Any = None):
|
||||
"""Sends multiple complete messages from the same node with an interval."""
|
||||
yield Event(message="#3 Multiple messages")
|
||||
await sleep_if_not_pytest(1.0)
|
||||
|
||||
yield Event(message="Processing step 1...")
|
||||
await sleep_if_not_pytest(1.0)
|
||||
|
||||
yield Event(message="Processing step 2...")
|
||||
await sleep_if_not_pytest(1.0)
|
||||
|
||||
yield Event(message="Done processing.")
|
||||
|
||||
|
||||
async def stream_sentence(node_input: Any = None):
|
||||
"""
|
||||
Demonstrates streaming by sending a sentence in chunks.
|
||||
The `partial=True` flag tells the UI that this is part of an ongoing message.
|
||||
"""
|
||||
yield Event(message="#4 Starting to stream...")
|
||||
sentence = """\
|
||||
This is a streaming message sent in chunks.
|
||||
|
||||
You can stream in markdown as well. For example, the table below:
|
||||
|
||||
| Header 1 | Header 2 |
|
||||
|----------|----------|
|
||||
| Cell 1 | Cell 2 |
|
||||
| Cell 3 | Cell 4 |
|
||||
"""
|
||||
|
||||
for i in range(0, len(sentence), 5):
|
||||
chunk = sentence[i : i + 5]
|
||||
yield Event(message=chunk, partial=True)
|
||||
await sleep_if_not_pytest(0.2)
|
||||
|
||||
|
||||
root_agent = Workflow(
|
||||
name="message",
|
||||
edges=[
|
||||
(
|
||||
"START",
|
||||
send_string,
|
||||
send_multimodal,
|
||||
multiple_messages,
|
||||
stream_sentence,
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,146 @@
|
||||
{
|
||||
"appName": "message",
|
||||
"events": [
|
||||
{
|
||||
"author": "user",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "go"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-1",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "#1 This is a simple string message."
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-2",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/send_string@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "#2 Here is a multi-modal message with an inline image (red circle):"
|
||||
},
|
||||
{
|
||||
"inlineData": {
|
||||
"data": "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8_9hAAAAXElEQVR4nO2TSQ7AIAwD7fz_z-ZQtapwmrJc8QklmjBIgZJgIZMiAIl9KYbhjx4fgwosbNxgMrF0-4uhgHnYDM6AzQHJeg5HYtyHFfgy2AztN_5tZWfrBtVzkl4DzfQkEPd-cEkAAAAASUVORK5CYII=",
|
||||
"mimeType": "image/png"
|
||||
}
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-3",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/send_multimodal@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "#3 Multiple messages"
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-4",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/multiple_messages@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "Processing step 1..."
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-5",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/multiple_messages@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "Processing step 2..."
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-6",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/multiple_messages@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "Done processing."
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-7",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/multiple_messages@1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"author": "message",
|
||||
"content": {
|
||||
"parts": [
|
||||
{
|
||||
"text": "#4 Starting to stream..."
|
||||
}
|
||||
],
|
||||
"role": "user"
|
||||
},
|
||||
"id": "e-8",
|
||||
"invocationId": "i-1",
|
||||
"nodeInfo": {
|
||||
"path": "message@1/stream_sentence@1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"id": "9cfc0ef6-11d6-4260-84cf-be22731ab69e",
|
||||
"state": {
|
||||
"__session_metadata__": {
|
||||
"displayName": "go"
|
||||
}
|
||||
},
|
||||
"userId": "user"
|
||||
}
|
||||
Reference in New Issue
Block a user