adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Sequential LlmAgents not returning audio with bidi streaming

Open ZachTB123 opened this issue 1 year ago • 8 comments

** Please make sure you read the contribution guide and file the issues in the right place. ** Contribution guide.

Describe the bug I have two separate LlmAgent's who are instructed to only say a specific. After the first agent completes, I want the next LlmAgent to say its message. When using either a SequentialAgent or custom agent to orchestrate this, I'm not getting an audio response.

To Reproduce Steps to reproduce the behavior:

  1. Clone: https://github.com/google/adk-docs/tree/main/examples/python/snippets/streaming/adk-streaming-ws/app
  2. Update google_search_agent/agent.py with the following code:
from google.adk.agents import Agent, SequentialAgent

msg1_agent = Agent(
    name="message_agent_1",
    model="gemini-live-2.5-flash",
    description="Agent to say a message to the user.",
    instruction=(
        """
        Your goal is to say a specific message to the user. The message is:

        This is message one.

        Do not say anything else other than the message. You should only
        say the message exactly as it is.
        """
    ),
    include_contents="none"
)

msg2_agent = Agent(
    name="message_agent_2",
    model="gemini-live-2.5-flash",
    description="Agent to say a message to the user.",
    instruction=(
        """
        Your goal is to say a specific message to the user. The message is:

        This is message two.

        Do not say anything else other than the message. You should only
        say the message exactly as it is.
        """
    ),
    include_contents="none"
)

root_agent = SequentialAgent(
    name="MessagesAgent",
    sub_agents=[msg1_agent, msg2_agent],
    description="An agent that plays messages in sequence.",
)
  1. Use the directions here: https://google.github.io/adk-docs/streaming/custom-streaming-ws/#3.-interact-with-your-streaming-app to run the app.
  2. Say "hello".
  3. You should see This is message one.This is message two. displayed in the page.

Expected behavior I expect to hear those two messages said back to me as audio. Instead, I only see the text representations.

This seems like it is possibly due to the tool call that gets added here. If I instead using a custom agent like follows:

class CustomAgent(BaseAgent):

    def __init__(
        self,
        name: str,
    ):
        super().__init__(
            name=name,
            sub_agents=[msg1_agent, msg2_agent],
        )

    @override
    async def _run_live_impl(
        self, ctx: InvocationContext
    ) -> AsyncGenerator[Event, None]:
        async for event in msg1_agent.run_live(ctx):
            yield event

        async for event in msg2_agent.run_live(ctx):
            yield event

I do hear the first message played back to me, but not the second. As soon as I add tool calls and update the the instructions on my LlmAgent's to match those added by the SequentialAgent, I no longer get audio but both messages are displayed as text on the page.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: mac
  • Python version(python -V): 3.13.3
  • ADK version(pip show google-adk): 1.5.0

Model Information:

gemini-live-2.5-flash

Additional context

My goal is to create a custom agent that implements our deterministic requirements. Sometimes we may have to play multiple messages in a row. We will only know if we have another message to play after the previous has finished.

ZachTB123 avatar Jul 30 '25 20:07 ZachTB123

@ZachTB123, in the example of the bidi streaming the run config is set so when you send text the answer from the model is text when you send audio the answer is audio. If this is not the issue can you describe it more pls ty.

ac-machache avatar Jul 30 '25 21:07 ac-machache

@ac-machache In my case, I am sending in audio but getting text response. I have also tried hard coding the RunConfig to set response_modalities=["AUDIO"] but still not always getting an audio response.

ZachTB123 avatar Jul 30 '25 21:07 ZachTB123

Hi @ZachTB123,

I’ve been testing a similar setup and wanted to share some findings that might help clarify the issue.

based on my tests, audio can be received for both agents, if the handoff between them is managed correctly.

What I found:

  • Audio works for both agents when the first agent explicitly hands off control to the second using a task_completed tool call.
  • The key limitation seems to be around how function/tool calls are orchestrated:
    • If the first agent doesn't call the task_completed tool, the second agent doesn't activate.
    • Even when the tool call is made, there's often a slight delay before the second agent starts speaking due to:
      if (
          event.content
          and event.content.parts
          and event.content.parts[0].function_response
          and event.content.parts[0].function_response.name
          == 'task_completed'
      ):
          # this is used for sequential agent to signal the end of the agent.
          await asyncio.sleep(1)
          # cancel the tasks that belongs to the closed connection.
          send_task.cancel()
          return
      
  • Prompt tuning is critical — you must guide the first agent clearly to invoke the tool or signal the handoff; otherwise the second agent won’t get activated.
  • I’m using PyAudio to handle audio output — not the custom bi-directional streaming example from ADK — so results may vary depending on your audio backend.

TL;DR

If you're getting only text for the second message:

  • Make sure your first agent's instruction includes calling task_completed (or equivalent).
  • Fine-tune the prompt to ensure this behavior reliably triggers.
  • PyAudio seems to behave more consistently for me than the bidi streaming example.
  • I'am also using adk 1.8.0
  • Use a DatabaseSessionService to inspect the saved audio blobs — they are stored correctly.
  • In my case, whenever an agent responds, I do receive the audio, so the streaming works as expected.
  • The real issue is inconsistent model behavior:
    • Sometimes the first agent answers but doesn't call task_completed.
    • Other times it calls the tool but doesn't answer.
  • This means the prompt must be carefully crafted to ensure the model both speaks the response and correctly signals the handoff.

Hope this helps!

here is my test file i used,

import asyncio
from google.adk.agents import LlmAgent, LiveRequestQueue, SequentialAgent
from google.adk.agents.run_config import RunConfig, StreamingMode
from google.adk.runners import Runner
from google.adk.sessions import DatabaseSessionService
from google.genai import types
from google.genai.types import Content, Part, Blob
from dotenv import load_dotenv
import pyaudio

load_dotenv()

# Audio stream constants
CHUNK = 1024 * 2
FORMAT = pyaudio.paInt16
CHANNELS = 1
INPUT_RATE = 16000  # 16kHz for input
OUTPUT_RATE = 24000 # 24kHz for output

VOICE_SYSTEM_PROMPT = ''' you are a helpful agent '''

agent = LlmAgent(
   name="live_agent",
   model="gemini-live-2.5-flash-preview",
   instruction="""greet back the user first this is your task, and then call task_completed.""",
)

agent1 = LlmAgent(
   name = "live_agent2",
   model="gemini-live-2.5-flash-preview",
   instruction="""ask the user how is he doing""",
)

main_agent = SequentialAgent(
   name = "MessagingAgent",
   sub_agents= [agent, agent1]
)



session_service = DatabaseSessionService(db_url="sqlite:///./agrilife.db")
runner = Runner(app_name="voice_assistant", agent=main_agent, session_service=session_service)
live_request_queue = LiveRequestQueue()
VOICE_NAME = "Puck"




async def main():
   """The main function that runs the concurrent sender and receiver."""
   session = await session_service.create_session(
       app_name="voice_assistant",
       user_id="default_user",
   )
   run_config = RunConfig(
       streaming_mode=StreamingMode.BIDI,
       speech_config=types.SpeechConfig(
           voice_config=types.VoiceConfig(
               prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name=VOICE_NAME)
           )
       ),
       response_modalities=["AUDIO"],
       output_audio_transcription=types.AudioTranscriptionConfig(),
       input_audio_transcription=types.AudioTranscriptionConfig(),
   )

   p = pyaudio.PyAudio()

   async def receive_events():
       print("[Receiver] Started. Waiting for events...")
       output_stream = p.open(format=FORMAT, channels=CHANNELS, rate=OUTPUT_RATE, output=True)
       try:
           async for event in runner.run_live(
               session=session,
               live_request_queue=live_request_queue,
               run_config=run_config
           ):
               if event.content and event.content.parts:
                   for part in event.content.parts:
                       if part.text:
                           print(f"[Receiver] Agent '{event.author}' Text: '{part.text}'")
                       if part.inline_data and part.inline_data.data:
                           output_stream.write(part.inline_data.data)
                       if part.function_call:
                           print(f"[Receiver] Model wants to call function: {part.function_call.name} with args: {part.function_call.args}")
           print("[Receiver] Event loop finished.")
       finally:
           output_stream.stop_stream()
           output_stream.close()


   async def send_audio():
       print("[Sender] Started. Recording from microphone...")
       input_stream = p.open(format=FORMAT, channels=CHANNELS, rate=INPUT_RATE, input=True, frames_per_buffer=CHUNK)
       try:
           while True:
               data = await asyncio.to_thread(input_stream.read, CHUNK, exception_on_overflow=False)
               blob = Blob(data=data, mime_type="audio/pcm")
               live_request_queue.send_realtime(blob)
               await asyncio.sleep(0.01) # Yield control to the event loop
       finally:
           input_stream.stop_stream()
           input_stream.close()

   try:
       await asyncio.gather(
           receive_events(),
           send_audio()
       )
   except KeyboardInterrupt:
       print("\nCaught keyboard interrupt. Shutting down.")
   finally:
       live_request_queue.close()
       p.terminate()


if __name__ == "__main__":
   asyncio.run(main())
   ```

ac-machache avatar Jul 31 '25 09:07 ac-machache

I think this is a model issue at this point because I've changed the prompting many times and have yet to get a good response.

With gemini-live-2.5-flash I encounter that each LlmAgent invokes their tool correctly and both agents are invoked back-to-back without me providing input. The problem is audio is not returned.

With gemini-live-2.5-flash-preview-native-audio I encounter that the LlmAgents are responding with audio but are not invoked back-to-back since they do not call their tool until I provide a new user input. If I provide input after the first responds, the second will then respond.

And I was not able to get the example above to work. I get gemini-live-2.5-flash-preview is not supported in the live api.. Switching to gemini-live-2.5-flash partly works, but I only receive responses from the live_agent agent. live_agent2 is never invoked.

ZachTB123 avatar Jul 31 '25 23:07 ZachTB123

@ZachTB123

When you run the script provided above and switch to gemini-live-2.5-flash, would you mind trying the following: i can't run the tests my self i don't have access to gemini-live-2.5-flash only gemini-live-2.5-flash-preview

  • As soon as you start chatting with the first agent, if the agent responds but does not invoke the task_completed tool, explicitly ask the agent to hand over control by calling the task_completed tool.
  • This enforcement ensures second agents will be invoked.

For the model gemini-live-2.5-flash-preview-native-audio:

  • This one is currently not reliable when it comes to tool calling.
  • I recommend avoiding it for now in tool-invocation scenarios.

For testing purposes, especially if you're checking whether both agents return audio, I suggest using:

gemini-2.0-flash-live-001

This one tends to be more stable across regions and is safer for validation. And this way we can make sure we use the same model for better debugging.

Note: Availability of the gemini-live-2.5-flash depends on your region if you're using the Vertex AI API, so keep that in mind.

ac-machache avatar Aug 01 '25 11:08 ac-machache

Thank you @ac-machache for the proposed fix in https://github.com/google/adk-python/pull/2295!

polong-lin avatar Aug 15 '25 08:08 polong-lin

Thank you @ac-machache for the proposed fix in #2295!

Please resolve the conflicts in the PR and then we can merge.

hangfei avatar Oct 28 '25 21:10 hangfei

For sequential agent, right now, it requires explicitly tell the agent that the task has been completed. You can checkout our new user guide here: https://google.github.io/adk-docs/streaming/dev-guide/part3/#sequentialagent-with-bidi-streaming.

Close due to inactivity.

hangfei avatar Nov 23 '25 00:11 hangfei