diff --git a/litellm/litellm_core_utils/audio_utils/transcription_streaming.py b/litellm/litellm_core_utils/audio_utils/transcription_streaming.py index cf2681f5d36..719909d66ea 100644 --- a/litellm/litellm_core_utils/audio_utils/transcription_streaming.py +++ b/litellm/litellm_core_utils/audio_utils/transcription_streaming.py @@ -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) diff --git a/tests/unit/llms/openai/transcriptions/test_gpt_transcribe.py b/tests/unit/llms/openai/transcriptions/test_gpt_transcribe.py index ccc176e6451..dc8feac29e9 100644 --- a/tests/unit/llms/openai/transcriptions/test_gpt_transcribe.py +++ b/tests/unit/llms/openai/transcriptions/test_gpt_transcribe.py @@ -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()