fix(audio): log streamed transcripts whose done event reports detected languages

This commit is contained in:
mateo-berri 2026-10-20 02:16:00 -07:00 • committed by Emerson Gomes
parent 8855c1385e
commit 2501523ecd
No known key found for this signature in database
GPG key ID: D3DF28AB5D1B5E17
2 changed files with 11 additions and 3 deletions

View file

@ -11,8 +11,11 @@ from openai.types.audio import (
TranscriptionTextDeltaEvent,
TranscriptionTextDoneEvent,
)
from pydantic import TypeAdapter
from litellm.types.utils import TranscriptionResponse
from litellm.types.utils import TranscriptionDetectedLanguage, TranscriptionResponse
_DETECTED_LANGUAGES: Final = TypeAdapter(list[TranscriptionDetectedLanguage])
class TranscriptionStreamLogging(Protocol):
@ -72,7 +75,11 @@ class _TranscriptionEventCollector:
response: Final = TranscriptionResponse(
text=done_event.text if done_event is not None else "".join(self.text_deltas),
usage=done_event.usage.model_dump() if done_event is not None and done_event.usage is not None else None,
languages=([language.model_dump() for language in done_languages] if done_languages is not None else None),
languages=(
_DETECTED_LANGUAGES.validate_python(done_languages, from_attributes=True)
if done_languages is not None
else None
),
)
if self.duration is not None:
response.set_audio_transcription_duration(self.duration)

View file

@ -75,7 +75,7 @@ def test_sync_transcription_stream_logs_final_text_and_usage_once() -> None:
content=(
'data: {"type":"transcript.text.delta","delta":"hello "}\n\n'
'data: {"type":"transcript.text.done","text":"hello world",'
'"usage":{"type":"duration","seconds":2.5}}\n\n'
'"usage":{"type":"duration","seconds":2.5},"languages":[{"code":"en"}]}\n\n'
),
)
@ -97,6 +97,7 @@ def test_sync_transcription_stream_logs_final_text_and_usage_once() -> None:
logged_response: Final = logging_obj.success_handler.call_args.args[0]
assert logged_response.text == "hello world"
assert logged_response.usage.model_dump(exclude_none=True) == {"type": "duration", "seconds": 2.5}
assert logged_response.model_dump()["languages"] == [{"code": "en"}]
logging_obj.failure_handler.assert_not_called()