feat(realtime): support multi-message generation per response#5763
Merged
Conversation
Some realtime providers (e.g. GPT-Realtime-2.0) emit multiple message
items in a single response. Process each one serially: push frames,
flush, wait_for_playout. Only one flush is ever in flight at a time, so
room_io and the transcript synchronizer keep their single-segment
invariants without modification.
Per-msg state is derived from the playback_finished event:
- 'full' -> emit ChatMessage(interrupted=False) with the msg's id
- 'partial' -> emit ChatMessage(interrupted=True); call truncate() with
the msg's local playback position
- 'skipped' -> drop from local chat ctx; call update_chat_ctx() so the
realtime server removes never-played items from history
This is a cleaner alternative to flushing per-message, which would
require keeping multiple in-flight flush_tasks / synchronizer segments
alive simultaneously.
Server-side truncation must run independently of local ChatMessage emission. The previous order skipped truncate() when forwarded_text was empty (transcription disabled, or interrupt before the text stream caught up to audio), leaving the realtime server with the full un-truncated audio.
theomonnom
approved these changes
May 20, 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.
Summary
MessageGenerationfromgeneration_ev.message_streamserially viaperform_audio_forwarding+perform_text_forwarding+wait_for_playout. Only one flush is in flight at a time.playback_finishedevent:full→ emitChatMessage(interrupted=False)with the msg'smessage_idpartial→ emitChatMessage(interrupted=True)and call_rt_session.truncate(...)with this msg's localplayback_position(not a cumulative offset)skipped→ drop locally and callupdate_chat_ctx(...)so the realtime server removes never-played items from its history_on_first_framenow early-returns oncestarted_speaking_atis set, so per-msg first-frame callbacks don't re-fire_update_agent_state("speaking")for each message.Alternative considered
#5690 makes multi-message work by flushing per message — that needs the synchronizer to keep pending/finalizing impls alive and serialize concurrent flushes in
room_io/_output.py. Our AudioOutput assumes there is only one speech at a time, serializing per-message at thewait_for_playoutboundary (this PR) avoids both changes.close #5690, #5684