Repository navigation
MAINT Simplify OpenAI Realtime receive state handling - #2988
Merged
Roman Lutz (romanlutz) merged 2 commits intoOct 9, 2026
Merged
Roman Lutz (romanlutz) merged 2 commits into
Roman Lutz (romanlutz) merged 2 commits into
Conversation
Move the per-turn bookkeeping in RealtimeTarget.receive_events_async into a typed _RealtimeReceiveState and split the loop body into a wait step (fixed soft-finish deadline, stream end, connection close) and a per-event step (stale-response filtering, delta accumulation, response.done, errors, audio.done). receive_events_async still owns the receive lifecycle. The grace deadline is set once by the first audio.done and is never extended, as before; behavior is otherwise unchanged. Adds tests for the event sequences: normal turn, late transcript within grace, grace expiry, errors and cancellation during grace, stale responses, and stopping at the terminal response.done. Fixes microsoft#2414 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Roman Lutz (romanlutz)
approved these changes
Oct 9, 2026
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.
Description
Refactors
RealtimeTarget.receive_events_asyncaround an explicit per-turn state object, as proposed in #2414. Fixes #2414._RealtimeReceiveState(a private dataclass) holds the audio buffer, the transcripts, the response id the turn is bound to, the count of accepted events, and the soft-finish deadline. It encodes the existing rules:response.donethat arrives as the first event, with no audio, is left over from a prior turn's soft-finish.audio.done._apply_receive_eventis the per-event step. It has the same shape as_OpenAIRealtimeDispatcher._route_event_asyncon the streaming side and handles stale filtering, delta accumulation,response.done, server errors andaudio.done._next_receive_event_asyncis the wait step. It waits no longer than the fixed deadline and handles the stream ending, grace expiry, and a connection closing after audio has arrived.receive_events_asyncstill owns the receive lifecycle; its loop is now a few lines.This is meant to preserve behavior. The fixed grace deadline already landed in #2420, so this PR keeps it (now as a rule enforced by the state object) rather than changing it. Barge-in isn't part of the atomic receive path; it lives in the streaming dispatcher, so that code is untouched. The only signature change is to the private
_handle_response_done_event, which now takes a transcript count instead of the result object.Roman Lutz (@romanlutz)
Tests and Documentation
audio.donenot moving the deadlineresponse.donereceive_events_asyncgave the same results, exceptions, events consumed and timeouts.pytest tests/unit/prompt_targetpasses (1604 passed, 120 skipped). ruff, ty and the other pre-commit hooks pass on the changed files.This change was prepared with the help of Claude Code (an AI coding assistant).