diff --git a/litellm/llms/openai/transcriptions/handler.py b/litellm/llms/openai/transcriptions/handler.py index 014251db821..cd6c6cdcc1b 100644 --- a/litellm/llms/openai/transcriptions/handler.py +++ b/litellm/llms/openai/transcriptions/handler.py @@ -128,7 +128,11 @@ class OpenAIAudioTranscription(OpenAIChatCompletion): if isinstance(response, BaseModel): stringified_response = response.model_dump() else: - stringified_response = TranscriptionResponse(text=response).model_dump() + duration: Final = extract_duration_from_srt_or_vtt(response) + stringified_response = { + **TranscriptionResponse(text=response).model_dump(), + **({"_audio_transcription_duration": duration} if duration is not None else {}), + } ## LOGGING logging_obj.post_call( diff --git a/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py b/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py index f2dbb71fea1..959cd13c728 100644 --- a/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py +++ b/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py @@ -1,10 +1,14 @@ -from typing import Final +from datetime import datetime, timezone +from typing import Final, Literal from unittest.mock import Mock import httpx import pytest from openai import AsyncOpenAI, OpenAI +from litellm import completion_cost +from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.llms.openai.cost_calculation import cost_per_second from litellm.llms.openai.transcriptions.handler import OpenAIAudioTranscription from litellm.types.utils import TranscriptionResponse @@ -57,6 +61,53 @@ def test_audio_transcriptions_records_provider_response_headers(): _assert_headers_recorded(response, logging_obj) +@pytest.mark.parametrize( + ("response_format", "subtitle", "expected_duration"), + ( + ("srt", "1\n00:00:00,000 --> 00:00:01,000\nHello\n\n2\n00:00:01,000 --> 00:00:12,500\nWorld\n", 12.5), + ("vtt", "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nHello\n\n00:00:01.000 --> 00:00:12.500\nWorld\n", 12.5), + ("srt", "", None), + ("text", "Hello world", None), + ), + ids=("srt", "vtt", "empty-subtitle", "plain-text"), +) +def test_sync_transcription_retains_text_and_charges_subtitle_duration( + response_format: Literal["srt", "vtt", "text"], subtitle: str, expected_duration: float | None +) -> None: + logging_obj: Final = Logging( + model="whisper-1", + messages=None, + stream=False, + call_type="transcription", + start_time=datetime(2025, 1, 1, tzinfo=timezone.utc), + litellm_call_id="subtitle-transcription", + function_id="subtitle-transcription", + ) + transport: Final = httpx.MockTransport( + lambda request: httpx.Response(200, text=subtitle, headers={"content-type": "text/plain"}) + ) + with httpx.Client(transport=transport) as http_client: + response: Final = OpenAIAudioTranscription().audio_transcriptions( + model="whisper-1", + audio_file=("audio.wav", b"riff-bytes", "audio/wav"), + optional_params={"response_format": response_format}, + litellm_params={}, + model_response=TranscriptionResponse(), + timeout=10.0, + max_retries=0, + logging_obj=logging_obj, + api_key="transport-only", + api_base=None, + client=OpenAI(api_key="transport-only", http_client=http_client), + ) + + assert response.model_dump() == TranscriptionResponse(text=subtitle).model_dump() + assert response._hidden_params.get("audio_transcription_duration") == expected_duration + assert completion_cost(completion_response=response, model="whisper-1", call_type="transcription") == sum( + cost_per_second(model="whisper-1", custom_llm_provider="openai", duration=expected_duration or 0.0) + ) + + @pytest.mark.asyncio async def test_async_audio_transcriptions_records_provider_response_headers(): logging_obj = _logging_obj()