Skip to content

Realtime: speech start or interrupt() during an audio-less response truncates the previous, fully heard item #5190

Closed

Description

@kosesena

Please read this first

Describe the bug

When the user starts speaking (server VAD input_audio_buffer.speech_started) or the app calls session.interrupt(), OpenAIRealtimeWebSocketModel skips conversation.item.truncate if the last assistant audio item has already played in full. That skip is bypassed whenever any response is in progress:

  • openai_realtime.py, speech_started branch: ... and truncated_ms >= max_audio_ms and not self._ongoing_response
  • _interrupt_audio_playback: (self._ongoing_response and not event.playback_only) or ...

The bypass makes sense when the in-progress response produced the item, because the item may still grow. Nothing checks that, though. If the assistant finishes speaking and the next response has no audio, such as a tool-call-only response, every barge-in during it truncates the previous, fully heard item at its full length.

Per the API reference, truncating also removes the item's server-side transcript, so the model can lose what it just said. #2370 reported the same symptom from these unnecessary truncates: the model started repeating itself. In tool-using voice agents it is common for the user to talk while a tool call is running.

The data to tell the two cases apart already exists: ModelAudioState.response_id records which response produced the item, and the response sequencer tracks the in-progress response id. They are just never compared.

Debug information

  • Agents SDK version: main at c467732
  • Python version: 3.12
  • OS: macOS 15
  • Model: any Realtime model; reproduced with raw server events, no network

Repro steps

Self-contained. r1 speaks item a1 (1000 ms of audio) and completes. 5 s pass, so a1 has fully played. r2 starts with no audio. Then the user speaks.

import asyncio, base64, time
from unittest.mock import AsyncMock

from agents.realtime.openai_realtime import OpenAIRealtimeWebSocketModel


def resp(rid, status):
    return {"id": rid, "object": "realtime.response", "status": status, "output": []}


async def run(r2_in_progress: bool):
    model = OpenAIRealtimeWebSocketModel()
    model._websocket = AsyncMock()
    sent = []
    async def capture(ev): sent.append(ev)
    model._send_raw_message = capture  # type: ignore[method-assign]

    ev = model._handle_ws_event
    await ev({"type": "response.created", "event_id": "e1", "response": resp("r1", "in_progress")})
    await ev({"type": "response.output_audio.delta", "event_id": "e2", "response_id": "r1",
              "item_id": "a1", "output_index": 0, "content_index": 0,
              "delta": base64.b64encode(b"\x00" * 48_000).decode()})  # 1000 ms pcm16@24k
    await ev({"type": "response.done", "event_id": "e3", "response": resp("r1", "completed")})

    # 5 s later: a1 has finished playing on the client.
    model._audio_state_tracker.get_state("a1", 0).initial_received_time -= 5

    if r2_in_progress:  # function-call-only response, no audio
        await ev({"type": "response.created", "event_id": "e4", "response": resp("r2", "in_progress")})

    await ev({"type": "input_audio_buffer.speech_started", "event_id": "e5",
              "item_id": "u2", "audio_start_ms": 0})
    return [(e.type, getattr(e, "item_id", None), getattr(e, "audio_end_ms", None))
            for e in sent if e.type == "conversation.item.truncate"]


async def main():
    print("no response in progress   ->", await run(False))
    print("audio-less r2 in progress ->", await run(True))

asyncio.run(main())

Output on main:

no response in progress   -> []
audio-less r2 in progress -> [('conversation.item.truncate', 'a1', 1000)]

session.interrupt() / _send_interrupt behaves the same way.

Expected behavior

No truncate in either case: a1 was heard in full, and r2 owns no audio that could still grow. The in-progress override should apply only when the item belongs to the in-progress response. I have a small fix with regression tests ready and will open a PR referencing this issue.

Activity

  1. kosesena commented on Sep 27, 2026

    @kosesena
    ContributorAuthor

    Update: I see PRs are now collaborator-only, so I won't open one. In case it saves time, the fix with regression tests is on my fork: kosesena/openai-agents-python@main...fix/realtime-truncate-only-owned-audio (it adds an ownership check in both truncate paths; code-change-verification/scripts/run.sh passes). Happy to adjust anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions