From 33e25c7aaea5655a7b34e4d7885d818c3413acf6 Mon Sep 17 00:00:00 2001 From: randombet <6344553+randombet@users.noreply.github.com> Date: Fri, 6 Feb 2026 15:43:00 -0800 Subject: [PATCH] fix: early return after agent handoff in realtime pipeline After updateAgent() triggers activity transition in the realtime tool output handler, the old code continued using this.realtimeSession for chat context updates and reply generation. Since updateAgent() calls drain() and close() on the old activity (including its realtime session), this caused errors and broken agent transfers. Added early return to skip post-handoff processing in the realtime pipeline, matching the equivalent fix in the Python agents SDK. The STT-LLM-TTS pipeline (line 1691) is unaffected - it uses a local chatCtx variable and does not reference this.realtimeSession. --- agents/src/voice/agent_activity.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/agents/src/voice/agent_activity.ts b/agents/src/voice/agent_activity.ts index db16674e5..28e2418fd 100644 --- a/agents/src/voice/agent_activity.ts +++ b/agents/src/voice/agent_activity.ts @@ -2183,6 +2183,10 @@ export class AgentActivity implements RecognitionHooks { if (!ignoreTaskSwitch && newAgentTask !== null) { this.agentSession.updateAgent(newAgentTask); draining = true; + // Skip chat context update and reply creation - the new agent will handle this. + // The old realtime session is being closed by updateAgent(), so we cannot + // continue using this.realtimeSession after this point. + return; } if (functionToolsExecutedEvent.functionCallOutputs.length > 0) {