From 446d33df70975882b80bb3126d44cd11106af287 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:30:32 -0700 Subject: [PATCH] fix(soniox): let a server VAD turn answer only the oldest pending commit --- .../llms/soniox/realtime/transformation.py | 32 +++++++++---------- .../test_soniox_realtime_transformation.py | 13 ++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/litellm/llms/soniox/realtime/transformation.py b/litellm/llms/soniox/realtime/transformation.py index b1061067300..ed7590b6f39 100644 --- a/litellm/llms/soniox/realtime/transformation.py +++ b/litellm/llms/soniox/realtime/transformation.py @@ -2,7 +2,6 @@ import asyncio import time from collections import deque from collections.abc import Awaitable, Callable, Mapping -from dataclasses import dataclass from itertools import chain, groupby from types import MappingProxyType, TracebackType from typing import Final @@ -261,19 +260,13 @@ def _soniox_error(response: _SonioxResponse) -> SonioxException | None: ) -@dataclass(frozen=True, slots=True) -class _PendingFinalize: - discards_turn: bool - endpoint_turns: int - - class SonioxEventTransformer: def __init__(self, *, drops_originals: bool) -> None: self._drops_originals: Final = drops_originals self._item_id: str | None = None self._final_tokens: Final[list[_SonioxToken]] = [] # mutable-ok: a turn's tokens append in O(1) each - self._finalizes: Final[deque[_PendingFinalize]] = deque() # mutable-ok: pending finalizes pop in O(1) - self._endpoint_turns: int = 0 + self._finalizes: Final[deque[bool]] = deque() # mutable-ok: pending finalizes queue and pop in O(1) each + self._oldest_commit_answered: bool = False self._processed_ms: int = 0 self._billed_ms: int = 0 self._refusal: SonioxException | None = None @@ -297,7 +290,7 @@ class SonioxEventTransformer: if discards_turn and not self._finalizes: self._item_id = None self._final_tokens.clear() - self._finalizes.append(_PendingFinalize(discards_turn=discards_turn, endpoint_turns=self._endpoint_turns)) + self._finalizes.append(discards_turn) def take_unbilled_usage(self, stream_ms: int = 0) -> RealtimeInputAudioTranscriptionUsage | None: billable_ms: Final = max(self._processed_ms, stream_ms) @@ -322,7 +315,7 @@ class SonioxEventTransformer: if self._discarding: return () if token.text == _ENDPOINT_TOKEN: - return self._complete(speech_stopped=True) + return self._end_turn() started: Final = self._start() if not token.is_final or (self._drops_originals and token.translation_status == "original"): return started @@ -331,18 +324,26 @@ class SonioxEventTransformer: @property def _discarding(self) -> bool: - return bool(self._finalizes) and self._finalizes[0].discards_turn + return bool(self._finalizes) and self._finalizes[0] def _finalized(self) -> tuple[OpenAIRealtimeEvents, ...]: - pending: Final = self._finalizes.popleft() if self._finalizes else None - if pending is not None and pending.discards_turn: + discarded: Final = self._finalizes.popleft() if self._finalizes else None + answered: Final = self._oldest_commit_answered + self._oldest_commit_answered = False + if discarded: return () if self._item_id is not None: return self._complete(speech_stopped=False) - if pending is None or pending.endpoint_turns != self._endpoint_turns: + if discarded is None or answered: return () return (_completed(_new_item_id(), (), self.take_unbilled_usage()),) + def _end_turn(self) -> tuple[OpenAIRealtimeEvents, ...]: + events: Final = self._complete(speech_stopped=True) + if events and self._finalizes: + self._oldest_commit_answered = True + return events + def _start(self) -> tuple[OpenAIRealtimeEvents, ...]: if self._item_id is not None: return () @@ -361,7 +362,6 @@ class SonioxEventTransformer: tokens: Final = tuple(self._final_tokens) self._item_id = None self._final_tokens.clear() - self._endpoint_turns += speech_stopped stopped: Final[tuple[OpenAIRealtimeEvents, ...]] = ( (speech_event("input_audio_buffer.speech_stopped", item_id),) if speech_stopped else () ) diff --git a/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py b/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py index cdd7c5a74b5..653f73ce22c 100644 --- a/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py +++ b/tests/unit/llms/soniox/realtime/test_soniox_realtime_transformation.py @@ -319,6 +319,19 @@ def test_a_commit_soniox_answers_after_its_endpoint_already_ended_the_turn_adds_ assert [event["transcript"] for event in events if "transcript" in event] == ["Hello"] +def test_a_server_vad_turn_answers_only_the_oldest_pending_commit(): + config = SonioxRealtimeConfig() + config.transform_realtime_request(_append(), MODEL) + _events(config, [_token("Hello")]) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + config.transform_realtime_request(_append(), MODEL) + config.transform_realtime_request(_event("input_audio_buffer.commit"), MODEL) + + events = _events(config, [_token(""), _token(""), _token("")]) + + assert [event["transcript"] for event in events if "transcript" in event] == ["Hello", ""] + + def test_a_clear_without_speech_still_answers_nothing(): config = SonioxRealtimeConfig() config.transform_realtime_request(_append(), MODEL)