This sample demonstrates different ways to send a message to a user using Event(message=...) within an ADK Workflows node. It covers:
- String Messages: Standard string text replies.
- Multi-modal Messages: Returning mixed modality inputs, such as a string combined with an inline image.
- Multiple Messages: Emitting multiple full messages from the same node with a delay between them.
- Streaming Messages: Simulating an LLM streaming response by breaking a message into chunks and yielding them with the
partial=Trueflag at intervals.
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 TD
START --> send_string
send_string --> send_multimodal
send_multimodal --> multiple_messages
multiple_messages --> stream_sentence
stream_sentence --> END[Workflow Ends]
To send messages in an ADK node, yield an Event object with the message argument:
-
Send a simple string:
yield Event(message="Hello world!")
-
Send text with an image (multi-modal):
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"), ] )
-
Send multiple messages: To send multiple distinct messages from a single node, yield multiple
Eventobjects 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.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.")
-
Stream a message in chunks: Provide the
partial=Trueflag 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.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)