agents icon indicating copy to clipboard operation
agents copied to clipboard

GPT-Live / DuplexRealtimeAdapter: tool output waits for the in-flight filler to finish playing, adding its length to every tool answer

Open syedappinventiv opened this issue 1 month ago • 4 comments

Bug Description

With GPT-Live (default responses delegation), a tool's result reaches the backend model only after the voice model's in-flight burst of speech has finished playing, plus the gate's 0.8 s quiet window. The tool itself returns almost at once, but the backend's continuation waits for the filler ("let me check that for you…") to end. So every tool answer is delayed by however long the filler runs.

The GPT-Live docs say "Delegated work doesn't block the conversation. The voice model keeps listening and speaking while the backend reasons and calls tools" (docs). The adapter's own comment agrees: "a call joins the burst in flight so the tool runs while the model talks". The tool does run while the model talks, but its output is not sent until the talking is over.

The code path, on main at c168b713:

  1. DuplexRealtimeAdapter._on_function_call puts the call on the open burst's function_ch (duplex_adapter.py L515-L524). That burst is the filler the model is speaking.
  2. _realtime_generation_task_impl waits for that generation's playout: await speech_handle.wait_if_not_interrupted([*tasks]) and await process_msg_task (agent_activity.py L4651, L4665). The burst only ends when the GPT-Live gate has seen 0.8 s below its threshold (_MIN_SILENCE_DURATION = 0.8, gpt_live.py L47-L49).
  3. Only then does it await exe_task (L4825) and wait for any other speech to finish, "most realtime models don't support generating multiple responses at the same time" (L4876-L4887), before sending the outputs.

That reason doesn't hold for a duplex model, which the adapter declares with supports_overlapping_speech=True. GPT-Live is designed to keep talking while its backend works.

Measured on 4 phone-style calls (7 tool turns), timing the raw server events from openai_server_event_received. Medians:

Stage Median Measured between
voice delegates 0.40 s caller stops speaking → session.delegation.created
backend decides 0.84 s backend's first response.created → completed function_call item
tool + hold 1.83 s (p90 3.0 s, worst 7.2 s) completed function_call item → function_tools_executed (emitted after the burst's playout, just before the outputs are sent)
backend answers 0.81 s function_tools_executed → backend's last response.completed
voice speaks 0.69 s → agent state speaking

The tools in these turns return from data already in memory, so nearly all of "tool + hold" is the hold. We metered the agent's output audio to check it isn't low-level noise holding the gate open: the burst carrying the call is 90–100% frames above the gate threshold, so the hold tracks the length of the filler speech. Low-level sound adds only 0–0.9 s. The 7.2 s case was a long filler.

Expected Behavior

For a duplex model with overlapping speech, the tool output goes to the model as soon as the tool returns, while the filler is still playing. The backend then continues in parallel, and the voice speaks the answer when it's ready. This would take about 1.5–7 s off each tool answer in our measurements.

Reproduction Steps

  1. Run an agent with openai.realtime.GPTLiveModel (default responses delegation) and one @function_tool that returns immediately. Tell the voice to say a short filler while it waits (as the docs suggest).
  2. Log timestamps for:
    • the tool returning (inside the tool);
    • function_tools_executed and agent_state_changed;
    • and, from duplex_session.on("openai_server_event_received", ...), the next response.event carrying response.created for the same delegation_id (the backend's continuation).
  3. Ask something that needs the tool.

Result: the tool returns within milliseconds, but function_tools_executed fires, and the backend's continuation starts, only after the filler's burst has ended (about 0.8 s after the filler's last audible frame). A longer filler gives a longer gap.

Operating System

Linux (Debian, python:3.12-slim container)

Models Used

OpenAI GPT-Live (gpt-live-1) with the default responses delegation backend; Silero VAD.

Package Versions

livekit-agents==1.8.4
livekit-plugins-openai==1.8.4
livekit==1.1.20

The same code path is unchanged on main (c168b713, where GPT-Live now lives in livekit-agents/livekit/agents/llm/_realtime/gpt_live.py) and in 1.8.9 / 1.8.10.

Session/Room/Call IDs

No response

Proposed Solution

When _rt_overlapping_speech_enabled is true:

  • deliver tool outputs when exe_task finishes, instead of after the burst's playout (wait_if_not_interrupted([*tasks]) and process_msg_task); and
  • skip the "wait all speeches played" loop.

The open questions are probably the chat-context and RunResult ordering of the output relative to the burst's message, and what happens to an output already sent if the burst is later interrupted. With overlapping speech the framework doesn't interrupt on the caller (_pause_enabled() returns False), so the last case may not arise.

Additional Context

Workarounds we looked at, none of which remove the hold:

  • A custom DuplexRealtimeAdapter(gate=...) with a shorter quiet window (0.8 → 0.5 s) saves about 0.3 s and risks splitting sentences.
  • Prompting the voice for one- or two-word fillers shortens the hold but doesn't remove it.

syedappinventiv avatar Nov 01 '26 09:11 syedappinventiv

Confirmed on current main: with DuplexRealtimeAdapter, the tool call lands inside the filler burst, and _realtime_generation_task_impl in voice/agent_activity.py waits for that burst's playout (wait_if_not_interrupted / await process_msg_task) and then the "wait all speeches played" loop before it ever awaits exe_task and sends the tool output back. So the output is delayed by the filler plus the ~0.8 s silence gate, which matches your measurements.

Proposal: only when _rt_overlapping_speech_enabled, deliver the tool outputs from a side task as soon as exe_task finishes (same upsert into chat_ctx, function_tools_executed and send to the realtime session, extracted into a helper), without waiting for playout. The non-duplex path and tool calls that return an Agent (handoff) would keep today's behaviour. The open question is ordering in chat_ctx: I'd append the output after the burst's message.

Would you accept a PR along those lines? Happy to adjust if you'd rather solve it differently.

pcbeingused33 avatar Nov 01 '26 10:11 pcbeingused33

I reproduced this without keys on main (8f62cabfa). The test runs the real DuplexRealtimeAdapter inside an AgentSession, with a fake duplex model and virtual time. The tool returns immediately while a filler burst of known length is playing, and the test measures when the tool output is actually sent:

Filler length Tool output sent after
1.0 s 1.6 s
3.0 s 3.6 s

The delay grows one-to-one with the filler, plus about 0.6 s: the 0.8 s quiet gate, counted from roughly 0.2 s into the filler, where the tool fires.

Reproduction run

sequenceDiagram
  participant M as GPT-Live voice model
  participant D as DuplexRealtimeAdapter
  participant A as AgentActivity
  participant T as Tool
  participant B as Backend model
  M->>D: filler burst starts, let me check
  D->>A: function call joins the open burst
  A->>T: run tool
  T-->>A: result at once
  alt today
    A->>A: wait for the filler playout and the 0.8 s quiet gate
    A->>B: tool output after filler length plus 0.6 s
  else proposal
    A->>B: tool output as soon as the tool returns
    A->>A: playout and interruption handled as before
  end
flowchart TD
  A["Tool task finishes"] --> B{"Overlapping speech enabled, duplex path?"}
  B -- "no" --> C["Today: send after playout"]
  B -- "yes" --> D{"Tool returned an Agent handoff?"}
  D -- "yes" --> C
  D -- "no" --> E["Send output from a side task now"]
  E --> F{"Filler interrupted after the output was sent?"}
  F -- "no" --> G["Commit burst as usual"]
  F -- "yes" --> H["Open question: keep or retract the sent output"]

The chain: _on_function_call puts the call on the open burst, and _realtime_generation_task_impl awaits that burst's playout (L4671, L4685) before it sends the output.

On @pcbeingused333's proposal (send from a side task as soon as exe_task finishes, only when _rt_overlapping_speech_enabled, keep handoffs as today), I agree with the shape. One case it needs to settle: if the user interrupts the filler after the output was already sent, the interrupted branch (around L4793) currently decides interrupted-vs-committed in one step, assuming nothing was sent yet. The burst's assistant message is also added to the local chat context only when the burst closes, so the output can be sent ahead of the message it belongs to.

@pcbeingused333, are you planning to open the PR? If not, I'd like to take it and use the reproduction above as the regression test. For maintainers: if a sent tool output's filler is interrupted, should the output stay as sent, or should the turn be marked interrupted and the model told?

Reproduction file: duplex_tool_output_delay.py.

himanshumehta avatar Nov 03 '26 18:11 himanshumehta

Friendly ping on this one: is the approach above OK with you? Happy to open a PR (or adjust the plan) as soon as you confirm. Thanks!

pcbeingused33 avatar Nov 18 '26 03:11 pcbeingused33

One thing to check before the PR: sending from a side task when exe_task finishes won't help on its own. The call is on the burst's function_ch, which closes only in _close_burst (after the 0.8 s quiet window), and perform_tool_executions doesn't finish until that stream ends (async for fnc_call in function_stream). So exe_task completes at the same moment the output is sent today.

With @himanshumehta's repro I timed it on d78f39990: exe_task finished 1.6 s / 3.6 s after the tool returned for a 1 s / 3 s filler, same as the send. Sending each output from _tool_execution_completed_cb (overlapping speech only, handoffs excluded, under _rt_chat_ctx_lock) brought it to 0 in both cases.

If you go that way, two things to cover: the interrupted branch should skip outputs already sent (it currently re-sends them via _interrupted_tool_output and emits function_tools_executed twice), and _DuplexRealtimeSession.update_chat_ctx replaces self._chat_ctx after awaiting _append_items, so a burst that closes during that await loses its message from the mirror. Not an issue for GPT-Live today since its _append_items doesn't await, but it would be for other duplex models.

ayushmnnit avatar Nov 22 '26 03:11 ayushmnnit