agents icon indicating copy to clipboard operation
agents copied to clipboard

RealtimeSession.generate_reply() races server-VAD responses → conversation_already_has_active_response; server error never correlated to the pending future (dies as 10s timeout)

Open areebkhan-tech opened this issue 3 months ago • 1 comments

Bug Description

With the OpenAI Realtime model and server-side turn detection, two independent writers can start a response: (1) the server itself after VAD end-of-speech, and (2) the client via RealtimeSession.generate_reply(). Two distinct problems:

generate_reply() (realtime_model.py, ~L1604) sends response.create unconditionally — it never consults has_active_generation (defined ~25 lines below but unused on this path). If the server has just auto-created a response (client hasn't processed response.created yet), the server rejects with conversation_already_has_active_response. Window is widest right after a tool-reply generation starts or during a user turn ending. When the server rejects, the error body carries event_id='response_create_*' — the exact client event id of the losing generate_reply. _handle_error (~L2211) ignores it: it only logs and emits realtime_model_error. The pending future in _response_created_futures is never failed, so the reply dangles until the 10s _on_timeout and dies as a generic "generate_reply timed out." The agent's intended speech is silently dropped.

error=APIError('OpenAI Realtime API returned an error', body=RealtimeError(message='Conversation already has an active response in progress: resp_EBlPemAbVfWVITtKcdTmQ. Wait until the response is finished before creating a new one.', type='invalid_request_error', code='conversation_already_has_active_response', event_id='response_create_c94357505d06', param=None), retryable=True) recoverable=True

Expected Behavior

Client-initiated response.create should not race server-created responses — either wait for/cancel the active generation before sending, or on rejection fail the matching future immediately (correlating error.event_id) so callers can react, instead of a 10s timeout and a dropped reply.

Reproduction Steps

1.AgentSession with openai.realtime.RealtimeModel (server turn detection on, create_response default).
2.Register a function_tool that takes ~15–20s and returns an error string.
3.During a call, ask something that triggers the tool; while it runs, say a short utterance ("no") right as the tool completes.
4.The tool-reply generation (server side) and the end-of-turn reply race → error above. Also reproducible by calling session.generate_reply() from any event handler (e.g. SIP DTMF) while the user is finishing a turn.

Operating System

macOS

Models Used

No response

Package Versions

livekit-agents==1.6.7

Session/Room/Call IDs

No response

Proposed Solution

Minimal: in _handle_error, when error.code == "conversation_already_has_active_response" and error.event_id matches a key in _response_created_futures, fail (or transparently retry after the active generation's _done_fut) that future immediately.
Fuller: have generate_reply() consult has_active_generation and defer/queue the response.create until the current generation completes (mirroring how _scheduling_task already serializes client-side speech).

Additional Context

No response

Screenshots and Recordings

No response

areebkhan-tech avatar Aug 12 '26 08:08 areebkhan-tech

I'd like to work on this, will raise a PR shortly.

ByteMaster-1 avatar Aug 12 '26 08:08 ByteMaster-1