fix(realtime): surface pending-reply drop on reconnect via SpeechHandle.exception()
Follow-up agreed in #6352 review ("yes we can make the drop on reconnect inside the plugin observable via SpeechHandle.exception()").
Incident. On a transient WebSocket drop, the OpenAI realtime session reconnects and _reconnect() discards any pending generate_reply future without re-creating the response. The turn silently produced nothing, and the discard was a bare RealtimeError — indistinguishable from a terminal failure, so an application couldn't tell it was safe to re-prompt.
Fix. Discarded futures are now failed with APIConnectionError (retryable=True) — the session is coming back, so re-prompting is sensible. The voice turn loop already routes generate_reply failures onto the SpeechHandle (_mark_done(error=), from #6304); its catch is widened to APIError so the discard deterministically lands on SpeechHandle.exception(). This reuses the same retryable-flag contract #6352 introduced in this file for fatal server errors.
App contract (no new types, no string-matching):
exc = handle.exception() if isinstance(exc, APIError) and exc.retryable: # transient reconnect discard — safe to re-prompt Deliberately out of scope:
generate_reply timeout still raises bare RealtimeError; converting it to APITimeoutError for the same retryable contract is a follow-up PR. The terminal "realtime session closed" path (fatal error / retries exhausted) stays a bare RealtimeError — nothing to retry against. A response already streaming when the socket drops is closed via _close_current_generation (partial output, no error) — that's playout truncation, a separate issue. Timing note: generate_reply has a 10s client-side timeout. If reconnection outlasts it, the timeout wins the race and the handle gets the (bare RealtimeError) timeout instead of the discard — acceptable, and the follow-up timeout PR will fold that into the same retryable contract.
Behavior change for direct callers: anyone consuming a raw RealtimeSession.generate_reply() future and catching only RealtimeError will now see APIConnectionError on reconnect. No in-repo caller is affected (only the voice turn loop consumes that future, and it's updated here).
@longcw This is the follow-up we agreed on in the #6352 — the pending-reply discard on reconnect now lands on SpeechHandle.exception() as a retryable APIConnectionError, so apps can detect the dropped turn and re-prompt. PTAL when you get a chance.
@davidzhao @longcw Gentle bump when you have a moment — this is the reconnect-drop observability follow-up from the #6352 review. CI is green and the Devin flags are resolved; PTAL ,happy to adjust anything.
Hi @longcw @davidzhao , following up on this PR when you have a moment. It serves as the second half of the https://github.com/livekit/agents/pull/6352 rescope, following the merge of its sibling https://github.com/livekit/agents/pull/6445 last week. Please let me know if you would like any adjustments to the current implementation.
hi
sorry for the long silence, and thanks for the PR.
I am going to close it. The reconnect drop already reaches SpeechHandle.exception() as an llm.RealtimeError, so the change here is only the exception type. APIError.retryable is True by default, so the new check matches most API errors, and the other realtime plugins still raise a bare RealtimeError.