Skip to content

fix(openai): skip realtime truncate when no audio was played fixes : #6157#6158

Open
ByteMaster-1 wants to merge 2 commits into
livekit:mainfrom
ByteMaster-1:fix/realtime-truncate-zero-length-audio
Open

fix(openai): skip realtime truncate when no audio was played fixes : #6157#6158
ByteMaster-1 wants to merge 2 commits into
livekit:mainfrom
ByteMaster-1:fix/realtime-truncate-zero-length-audio

Conversation

@ByteMaster-1

@ByteMaster-1 ByteMaster-1 commented Jun 18, 2026

Copy link
Copy Markdown

Fixes #6157

Skip the realtime conversation.item.truncate when the generation is
interrupted before any audio frame has played (audio_end_ms == 0), which
the Realtime API rejects with unsupported_content_type
("Only model output audio messages can be truncated").

@ByteMaster-1 ByteMaster-1 requested a review from a team as a code owner June 18, 2026 17:16
@CLAassistant

CLAassistant commented Jun 18, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@ByteMaster-1 ByteMaster-1 force-pushed the fix/realtime-truncate-zero-length-audio branch from 0de3d9f to 9fa9693 Compare June 18, 2026 17:41
content_index=0,
item_id=message_id,
audio_end_ms=audio_end_ms,
if audio_end_ms > 0:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could there be two situations with audio_end_ms == 0:

  1. the generation was interrupted/cancelled before any audio generated on the server side
  2. there are audio generated but agent didn't play any of frames

this fix works for 1 but leaks the not played message for 2? maybe we should delete this item if the audio end ms is 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — you're right that audio_end_ms == 0 conflates two cases. Updated to delete the item (case 2) so the unplayed message isn't left dangling in the remote chat ctx, instead of just skipping. One thing I found while doing this: _handle_error only suppresses "Cancellation failed", so an unconditional delete would itself surface an error in case 1 (item never committed server-side). So I guarded the delete on the item being present in _remote_chat_ctx. Added tests for all three branches.

One open question: there's a small race where the item exists server-side but the conversation.item.added hasn't reflected into _remote_chat_ctx yet — in that window we'd skip the delete and the leak could persist. Happy to handle that differently (e.g. always delete and suppress the not-found error) if you'd prefer.

Address review (longcw): when audio_end_ms == 0 the server may hold a
generated-but-unheard audio item (case 2). Delete it so the remote chat
ctx matches the local one, guarded on the item actually existing remotely
so we don't surface a delete error for the never-committed case (case 1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RealtimeModel: "Only model output audio messages can be truncated" when interrupted before first audio frame

4 participants