Skip to content

Conversation

AlexeyChernenkoPlato
Copy link

Description

Fixes an issue in base_llm_flow.py where, in Bidi-streaming (live) mode, the multi-agent structure causes duplicated responses after tool calling.

Problem

In Bidi-streaming (live) mode, when utilizing a multi-agent structure, the leaf-level sub-agent and its parent agent both process the same function call response, leading to duplicate replies. This duplication occurs because the parent agent's live connection remains open while initiating a new connection with the child agent.

Root Cause

The issue originated from the placement of agent transfer logic in the _postprocess_live method at lines 547-557. When a transfer_to_agent function call was made:

  1. The function response was processed in _postprocess_live
  2. A recursive call to agent_to_run.run_live was initiated
  3. This prevented the closure of the parent agent's connection at line 175 of the run_live method, as that code path was never reached
  4. Both the parent and child agents remained active, causing both to process subsequent function responses

Solution

This PR addresses the issue by ensuring the parent agent's live connection is closed before initiating a new one with the child agent. Changes made:

Connection Management: Moved the agent transfer logic from _postprocess_live method to the run_live method, specifically:

  • Removed agent transfer handling from lines 547-557 in _postprocess_live
  • Added agent transfer handling after connection closure at lines 176-184 in run_live

Code Refactoring: The agent transfer now occurs in the proper sequence:

  1. Parent agent processes the transfer_to_agent function response
  2. Parent agent's live connection is properly closed (line 175)
  3. New connection with child agent is initiated (line 182)
  4. Child agent handles subsequent function calls without duplication

Improved Flow Control: This ensures that each agent processes function call responses without duplication, maintaining proper connection lifecycle management in multi-agent structures.

Testing

To verify this fix works correctly:

  1. Multi-Agent Structure Test: Set up a multi-agent structure with a parent agent that transfers to a child agent via transfer_to_agent function call
  2. Bidi-Streaming Mode: Enable Bidi-streaming (live) mode in the configuration
  3. Function Call Verification: Trigger a function call that results in agent transfer
  4. Response Monitoring: Verify that only one response is generated (not duplicated)
  5. Connection Management: Confirm that parent agent's connection is properly closed before child agent starts

Expected Behavior:

  • Single function response per call
  • Clean agent handoffs without connection leaks
  • Proper connection lifecycle management

Backward Compatibility

This change is fully backward compatible:

  • No changes to public APIs or method signatures
  • Existing single-agent flows remain unaffected
  • Non-live (regular async) flows continue to work as before
  • Only affects the internal flow control in live multi-agent scenarios

Copy link

google-cla bot commented Aug 18, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot adk-bot added bot triaged [Bot] This issue is triaged by ADK bot live [Component] This issue is related to live, voice and video chat labels Aug 18, 2025
@adk-bot adk-bot requested a review from hangfei August 18, 2025 11:05
@adk-bot
Copy link
Collaborator

adk-bot commented Aug 18, 2025

Response from ADK Triaging Agent

Hello @AlexeyChernenkoPlato, thank you for creating this PR!

Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). You can find more information at https://cla.developers.google.com/about.

Also, for bug fixes, could you please associate a GitHub issue with this PR? If there is no existing issue, could you please create one?

This information will help reviewers to review your PR more efficiently. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot triaged [Bot] This issue is triaged by ADK bot live [Component] This issue is related to live, voice and video chat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants