how to get streaming text response. #3339
-
|
I'm trying to get the text streaming response. but this gives the audio format streaming. how to get text streaming responses? import asyncio os.environ["GOOGLE_API_KEY"] = "AIzaSyB3FhuL0UUSXaZeTBAxaEX9fIFM7r7lI6M" agent = Agent( live_request_queue = LiveRequestQueue() async def main(): asyncio.run(main()) Output: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: You are receiving audio output because "live" agents in ADK default to an audio modality. To receive text, you must explicitly configure the agent to output text by passing a Hello! I can help with your question. The behavior you are observing is expected. The To get a streaming text response, you need to configure the run to expect the Here is the corrected version of your code: import asyncio
from google.adk.agents import Agent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.genai import types
from google.adk.events import Event
from google.adk.agents import LiveRequestQueue
from google.adk.agents.run_config import RunConfig # Import RunConfig
from dotenv import load_dotenv
load_dotenv()
import os
os.environ["GOOGLE_API_KEY"] = "YOUR_API_KEY" # It is recommended to use environment variables for API keys
agent = Agent(
name="streaming_agent",
model="gemini-2.0-flash-live-001",
description="A simple streaming agent example",
instruction="Respond to user queries in a streaming manner."
)
live_request_queue = LiveRequestQueue()
async def main():
session_service = InMemorySessionService()
user_id = "user123"
runner = Runner(agent=agent, app_name="streaming_agent_app", session_service=session_service)
session = await session_service.create_session(
app_name="streaming_agent_app",
user_id=user_id
)
user_input = types.Content(role="user", parts=[types.Part(text="Hello, Tell me about best beach in america in a five sentence.")])
await session_service.append_event(session, Event(author="user", content=user_input))
# Configure the run to expect TEXT responses
run_config = RunConfig(response_modalities=["TEXT"])
# Pass the run_config to the runner
async for event in runner.run_live(user_id=user_id, session_id=session.id, live_request_queue=live_request_queue, run_config=run_config):
if event.content and event.content.parts and event.content.parts[0].text:
print(event.content.parts[0].text, end="", flush=True)
print("\nStreaming complete.")
asyncio.run(main())By making this change, the agent will know to generate text instead of audio, and you will get the streaming text response you're looking for. This response was generated by an automated agent. [1] https://google.github.io/adk-docs/runtime/runconfig/ |
Beta Was this translation helpful? Give feedback.
Response from ADK Answering Agent (experimental, answer may be inaccurate)
TLDR: You are receiving audio output because "live" agents in ADK default to an audio modality. To receive text, you must explicitly configure the agent to output text by passing a
RunConfigobject to yourrunner.run_livecall.Hello! I can help with your question.
The behavior you are observing is expected. The
gemini-2.0-flash-live-001model is a "live" agent, and these agents are optimized for real-time, spoken conversations. By default, they produce an audio stream (audio/pcm) as output. [2]To get a streaming text response, you need to configure the run to expect the
"TEXT"modality. You can do this by creati…