This commit is contained in:
Elowen 2026-10-24 08:30:43 +00:00 • committed by GitHub
commit e9a426f393
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 18 deletions

View file

@ -7,6 +7,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
@ -131,7 +132,15 @@ 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)
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 {}),
}
## LOGGING
logging_obj.post_call(

View file

@ -10854,25 +10854,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:

View file

@ -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,56 @@ 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),
("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", "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
) -> 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()

View file

@ -100,6 +100,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,
@ -221,6 +222,33 @@ def test_litellm_proxy_responses_api_config_manager_returns_proxy_config() -> No
# 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")