fix: realtime agent handoff race conditions#4746
Open
randombet wants to merge 2 commits intolivekit:mainfrom
Open
fix: realtime agent handoff race conditions#4746randombet wants to merge 2 commits intolivekit:mainfrom
randombet wants to merge 2 commits intolivekit:mainfrom
Conversation
Three fixes for agent transfer with Gemini realtime models: 1. Early return after handoff in realtime pipeline (agent_activity.py): After update_agent() triggers activity transition, the old code continued using self._rt_session for chat context updates and reply generation. Since update_agent() closes the old realtime session, this caused errors and broken transfers. Added early return to skip post-handoff processing. 2. Resume paused agent activity on transfer back (agent_session.py): When transferring back to an agent that was previously active, its activity was paused rather than closed. The code always started a new activity, losing the paused state. Now checks for existing activity and resumes it instead. 3. Enable manual function calls for Gemini realtime (realtime_api.py): Set manual_function_calls=True so the framework controls tool execution flow rather than Gemini auto-executing, preventing race conditions during agent handoff where tool responses could be sent to the wrong session.
2fc2b01 to
bdf59f7
Compare
3 tasks
f2269ce to
729d755
Compare
Member
|
Hi @randombet, I am unable to reproduce this. Could you provide a reproducible snippet? Also curious as to how you set up your handoff function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
update_agent()triggers activity transition, the old code continued usingself._rt_sessionfor chat context updates and reply generation. Sinceupdate_agent()closes the old realtime session, this caused errors and broken agent transfers. Added earlyreturnto skip post-handoff processing.manual_function_calls=Trueso the framework controls tool execution flow rather than Gemini auto-executing, preventing race conditions during agent handoff where tool responses could be sent to the wrong session.Context
These bugs were discovered while building a multi-agent example with Gemini Realtime API. Transferring from main agent to a specialist worked, but transferring back consistently failed because:
AgentHandoff, triggeringupdate_agent()which starts closing the old realtime sessionself._rt_session.update_chat_ctx()on the closing sessionTest plan
manual_function_calls=True)