← Back to Pull requests

Keep only heard assistant speech in history after interruptions #804

Closed
unknown closed this pull request without merging on Nov 10, 2026 09:57 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 22:18 UTC
Author

Closes #801.

Problem

After a barge-in, the next model request's history did not match what the user heard. On main:

  • A reply cancelled mid-response was removed from history entirely, including the part that was spoken.

  • A reply that finished generating while its audio was still buffered was kept in full. conversation.item.truncate was a no-op.
  • Resulting behavior

  • Cancelled audio replies keep only the transcript of audio already sent to the client, as an incomplete assistant message. No marker is added to the text; this is the maintainer's "played text only" decision.

  • - If the last segment was still being synthesized, its full duration is estimated from the session's measured speaking rate, or 12 chars/s before one is measured. Unsynthesized words are therefore not kept.
    - If no audio was sent, no assistant message is kept.
  • WebSocket clients that never send truncate (e.g. the Reachy Mini conversation app, which only sends response.cancel and flushes its player): the WebSocket transport estimates playback from send times, assuming real-time playback from the moment each chunk is sent. On server-VAD barge-in and response.cancel, the audio not yet played by that estimate is removed from the newest items, the same way WebRTC removes its discarded RTP audio. The estimate assumes the client stops playback at that point.

  • conversation.item.truncate cuts an ended item's history to the transcript heard before audio_end_ms. It is measured against the item's original audio, so it replaces the server's WebSocket estimate instead of compounding it. That includes restoring, in its original place, a reply the estimate removed entirely. An explicit conversation.item.delete, an evicted or compacted turn, or any new item reusing the id prevents the restore, and a late report never edits that new item. That includes replies that completed while their audio was still buffered. An item with no heard audio is removed from model history but, as after a truncation to 0 ms, still exists on the wire.

  • - No conversation.item.truncated acknowledgement is sent. conversation.item.retrieve (added in #810) returns a cut item with its heard transcript and incomplete status, and a removed one as incomplete with an empty transcript (not an error, so these checks don't count in /v1/usage error metrics). conversation.item.delete removes it for good.
  • WebRTC: when barge-in, response.cancel or output_audio_buffer.clear discards queued RTP audio, the discarded duration is removed from the newest items. This matches OpenAI's description of server-side truncation for WebRTC.

  • The packaged local/talk client tracks played bytes per item and sends conversation.item.truncate when it clears interrupted playback. This covers both the in-process path and the echo-cancellation worker path. It then sends conversation.item.retrieve and redraws the interrupted reply's terminal line with the text the model history kept, marked [interrupted] (or prints it on a new line if other output followed).

  • Browser demo history panel: after each truncate (or, over WebRTC, after each barge-in for the last two replies) the demo retrieves the item and strikes through the words the model history did not keep: statically in the reply's history row (marked Interrupted), and with a short drawn-line animation in its bubble while still on screen. The server now cuts WebRTC history before sending input_audio_buffer.speech_started, so that retrieve sees the cut.

  • Server logs: each cut is logged at DEBUG with the kept text, the dropped character count and its cause.

  • Uninterrupted replies, tool-call rollback and out-of-band responses are unchanged. No new server events.

  • Responses API: retained or replaced messages get a generated msg_ id. The Realtime item id stays an alias, because replayed Responses assistant messages are believed to need msg-prefixed ids. This was not verified against the live API.
  • The mapping is documented in the Realtime README under "Interrupted replies". Each transcript segment is recorded with the audio delivered after it. Fully played segments are kept whole. The segment playing at the cutoff keeps whole words in proportion to its played duration, rounding down.

    Diagram

    Where the played position comes from and how it becomes the heard text, using the example from the tests: a reply interrupted at 2.60 s keeps "Sure, here are five ways to improve it. First,". (PDF)

    <img src="https://gist.githubusercontent.com/andimarafioti/f41191c2d3c5e2e07087a268fd13b447/raw/be111d7772e3901631e995093b34d8385a175805/interrupted-reply-history.png" alt="Handbook-style page: interrupted reply history functional diagram and a reply cut at 2.60 seconds" width="720">

    Known limitation: Agents SDK < 0.20 over WebSocket

    The server sends a cancelled reply's response.output_audio.done before input_audio_buffer.speech_started. @openai/agents-realtime below 0.20.0 resets its playback item on output_audio.done. So on server-VAD barge-in it sends no truncate and emits no audio_interrupted. Apps that flush their player on that event keep playing the cancelled reply, while the server's playback estimate cuts its history. Those apps lose some heard speech from history rather than keeping unheard speech.

    OpenAI does not document the order of these events, so the wire order is unchanged. SDK 0.20.0+ tracks playback past output_audio.done and truncates whatever the order (checked in the published packages). Since #810, the demo and CI pin 0.21.0, so this only affects apps on older SDK versions.

    WebRTC, the browser demo and the packaged client are not affected. The README documents this.

    Review

    Eleven independent review passes, each a fresh reviewer:

    | Pass | Finding | Disposition |
    |---|---|---|
    | 1 | A reply cancelled mid-synthesis kept its unsynthesized words. | Fixed with the speaking-rate duration estimate. |
    | 2 | Responses replay would use item_ ids. | Fixed: generated msg_ ids, with the Realtime id kept as an alias. |
    | 3 | The README overstated stock-SDK coverage. | Corrected. |
    | 4 | demo/README.md was stale. | Fixed. Also documented that output_audio_buffer.clear alone leaves the active reply untouched. |
    | 5 | Approve. | Documented how an unheard item is removed. (Later changed: retrieve now returns it as an empty incomplete item instead of conversation_item_not_found.) |
    | 6 | Approve. | No changes needed. |
    | 7 (WebSocket playback estimate) | Stock SDK < 0.20 keeps playing after server-VAD barge-in, so the estimate cuts speech its users heard. | Documented in the README and above. Also added a router-level speech_started test with a real WebSocketTransport. |
    | 8 (maintainer review of f066e2e2) | A client truncate could not restore a reply the estimate had removed entirely. | Fixed: removed replies remember their position and are restored on a later non-empty truncate; explicit deletes stay final. Regression tests added. |
    | 9 | Restoring after the turn was evicted put the reply at the start of history; a reused id could clash. | Both now skip the restore, with tests. Not fixed: after a previous_item_id insert right before the reply, the restored reply lands before the inserted item. |
    | 10 (maintainer review of 60991ef4) | A new assistant message reusing a removed reply's id was overwritten by a late playback report. | Fixed: the restore record is checked before any lookup or edit, so any item holding the id is left untouched. Regression test added. |
    | 11 | A removed reply that opened the conversation could be restored after its turn was evicted or compacted. | Fixed: eviction and compaction drop records for replies whose turn they remove. Tests added. |

    Deferred as optional:

  • The cancel-time estimate can drop a few heard trailing words.

  • CJK text without spaces.

  • Floor rounding at the very end of an item.

  • Clear-without-cancel accounting.

  • A response.cancel sent with no active response also applies the WebSocket estimate, so a client that sends it without flushing its player loses heard speech from history.

  • A client truncate that restores a fully played item after an estimate cut leaves it incomplete.

  • Responses reasoning companions dropped on replacement. This is the existing conversation.item.delete behaviour and is unverified against the live API.

  • No end-to-end test of the echo-cancellation worker's truncation queue.
  • Validation

  • New tests:

  • - tests/openai_realtime/test_interrupted_playback_history.py
    - 4 in test_audio_client.py
    - 4 in test_webrtc.py
  • The original 15 new tests fail on c7c3e8d1 and pass here.

  • ruff check, ruff format --check and mypy src/ are clean.

  • Full pytest tests/ on the final head: 3695 passed, 10 skipped. The only failures seen were pre-existing flakes, reproduced on unmodified main:

  • - MiniMax test_stalled_request_is_closed_and_worker_released[session_end], filed as #802.
    - test_audio_output_batches_immediately_available_chunks, intermittent under load.
  • CI: see the checks on the latest head.

  • Demo test:agents:adapter 63/63, test:ui 3/3, test:agents:ws 2/2, test:agents:webrtc 1/1 on @openai/agents 0.21.0 (stock SDK transports against the real server).

  • Not verified:

  • - real-device playback with the echo-cancellation worker
    - browser WebRTC jitter-buffer offset
    - a live Responses API call with the msg_ ids
    - LLM reply quality after truncation

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jackboyla Review duration: 6458m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #804"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%