From 7e66a3a14189bc2ca963495a3bcfebba4761c8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=81=E5=AE=89?= <73627805+Mar-garet@users.noreply.github.com> Date: Sun, 4 Oct 2026 19:03:35 +0800 Subject: [PATCH 1/2] fix(transcription): preserve duration for synchronous subtitle responses --- litellm/llms/openai/transcriptions/handler.py | 6 ++- .../test_openai_transcriptions_handler.py | 53 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) 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() From 1babfc65244631ae07b34bec339ff4fbf7ca49ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=81=E5=AE=89?= <73627805+Mar-garet@users.noreply.github.com> Date: Sun, 4 Oct 2026 19:18:52 +0800 Subject: [PATCH 2/2] fix(transcription): parse cue timings only for subtitle responses --- litellm/llms/openai/transcriptions/handler.py | 7 ++++- litellm/utils.py | 31 +++++++++---------- .../test_openai_transcriptions_handler.py | 5 ++- tests/unit/test_utils.py | 28 +++++++++++++++++ 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/litellm/llms/openai/transcriptions/handler.py b/litellm/llms/openai/transcriptions/handler.py index cd6c6cdcc1b..450eaab339e 100644 --- a/litellm/llms/openai/transcriptions/handler.py +++ b/litellm/llms/openai/transcriptions/handler.py @@ -6,6 +6,7 @@ from pydantic import BaseModel if TYPE_CHECKING: from aiohttp import ClientSession +from litellm.litellm_core_utils.audio_utils.subtitle_utils import SUBTITLE_RESPONSE_FORMATS from litellm.litellm_core_utils.audio_utils.utils import get_audio_file_name from litellm.litellm_core_utils.core_helpers import set_provider_response_headers_in_hidden_params from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj @@ -128,7 +129,11 @@ class OpenAIAudioTranscription(OpenAIChatCompletion): if isinstance(response, BaseModel): stringified_response = response.model_dump() else: - duration: Final = extract_duration_from_srt_or_vtt(response) + duration: Final = ( + extract_duration_from_srt_or_vtt(response) + if data.get("response_format") in SUBTITLE_RESPONSE_FORMATS + else None + ) stringified_response = { **TranscriptionResponse(text=response).model_dump(), **({"_audio_transcription_duration": duration} if duration is not None else {}), diff --git a/litellm/utils.py b/litellm/utils.py index a7d7447d7f3..dc6b779f3ab 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -10126,25 +10126,24 @@ def extract_duration_from_srt_or_vtt(srt_or_vtt_content: str) -> float | None: srt_or_vtt_content (str): The content of an SRT or VTT file as a string. Returns: - Optional[float]: The total duration in seconds, or None if no timestamps are found. + Optional[float]: The total duration in seconds, or None if no cue timestamps are found. """ - # Regular expression to match timestamps in the format "hh:mm:ss,ms" or "hh:mm:ss.ms" - timestamp_pattern: Final = r"(\d{2}):(\d{2}):(\d{2})[.,](\d{3})" + timestamp_pattern: Final = ( + r"^[ \t]*(?:\d{2,}:)?\d{2}:\d{2}[.,]\d{3}[ \t]+-->[ \t]+" + r"(?:(\d{2,}):)?(\d{2}):(\d{2})[.,](\d{3})" + ) - timestamps: Final[Sequence[tuple[str, str, str, str]]] = re.findall(timestamp_pattern, srt_or_vtt_content) + timestamps: Final[Sequence[tuple[str, str, str, str]]] = re.findall( + timestamp_pattern, srt_or_vtt_content, re.MULTILINE + ) - if not timestamps: - return None - - # Convert timestamps to seconds and find the max (end time) - durations: Final = [] - match: tuple[str, str, str, str] - for match in timestamps: - hours, minutes, seconds, milliseconds = map(int, match) - total_seconds = hours * 3600 + minutes * 60 + seconds + milliseconds / 1000.0 - durations.append(total_seconds) - - return max(durations) if durations else None + return max( + ( + int(hours or 0) * 3600 + int(minutes) * 60 + int(seconds) + int(milliseconds) / 1000.0 + for hours, minutes, seconds, milliseconds in timestamps + ), + default=None, + ) def _add_path_to_api_base(api_base: str, ending_path: str) -> str: 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 959cd13c728..7c0a1eb50e3 100644 --- a/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py +++ b/tests/unit/llms/openai/transcriptions/test_openai_transcriptions_handler.py @@ -68,8 +68,11 @@ def test_audio_transcriptions_records_provider_response_headers(): ("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), + ("text", "The timecode is 00:00:12.500", None), + ("text", "00:00:01.000 --> 00:00:12.500\nHello world", None), + ("vtt", "WEBVTT\n\n00:01.500 --> 00:02.000\nHello world\n", 2.0), ), - ids=("srt", "vtt", "empty-subtitle", "plain-text"), + ids=("srt", "vtt", "empty-subtitle", "plain-text", "text-timecode", "text-cue", "short-vtt"), ) def test_sync_transcription_retains_text_and_charges_subtitle_duration( response_format: Literal["srt", "vtt", "text"], subtitle: str, expected_duration: float | None diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index a72aec07766..1a2f43db26c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -76,6 +76,7 @@ from litellm.utils import ( async_post_call_success_deployment_hook, calculate_max_parallel_requests, client, + extract_duration_from_srt_or_vtt, get_non_default_completion_params, get_optional_params_image_gen, get_prompt_cache_min_tokens, @@ -86,6 +87,33 @@ from litellm.utils import ( # Adds the parent directory to the system path +@pytest.mark.parametrize( + ("subtitle", "expected_duration"), + ( + ("1\n01:01:01,000 --> 01:01:12,500\nHello\n", 3672.5), + ("WEBVTT\n\n00:01.500 --> 00:02.000\nHello\n", 2.0), + ("WEBVTT\n\n59:59.000 --> 01:00:02.500 align:start\nHello\n", 3602.5), + ("WEBVTT\n\n100:00:00.000 --> 100:00:02.000\nHello\n", 360002.0), + ("1\n00:00:00,000 --> 00:00:03,000\nThe timecode is 99:00:00.000\n", 3.0), + ("WEBVTT\n\n00:00.000 --> 00:05.000\nHello\n\n00:03.000 --> 00:04.000\nWorld\n", 5.0), + ("", None), + ("The timecode is 00:00:12.500", None), + ), + ids=( + "srt-hours", + "vtt-no-hours", + "vtt-mixed-hours", + "vtt-long-hours", + "payload-timecode", + "overlap", + "empty", + "text", + ), +) +def test_subtitle_duration_uses_the_latest_cue_end_time(subtitle: str, expected_duration: float | None) -> None: + assert extract_duration_from_srt_or_vtt(subtitle) == expected_duration + + def test_non_ocr_wrapper_preserves_logging_executor_and_context(monkeypatch: pytest.MonkeyPatch) -> None: marker: Final = contextvars.ContextVar("non-ocr-logging-context", default="missing") token: Final = marker.set("caller-context")