From 67c28cf9f4923ff383f39d67b7c7e4da0ada9504 Mon Sep 17 00:00:00 2001
From: dheemutech <50630823+dheemutech@users.noreply.github.com>
Date: Mon, 7 Sep 2026 16:08:03 +0530
Subject: [PATCH] fix(transcription): preserve literal text in WebVTT cues
---
.../audio_utils/subtitle_utils.py | 3 +-
.../audio_utils/test_subtitle_utils.py | 31 +++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/litellm/litellm_core_utils/audio_utils/subtitle_utils.py b/litellm/litellm_core_utils/audio_utils/subtitle_utils.py
index 91427ba09ad..9a7db0d96bf 100644
--- a/litellm/litellm_core_utils/audio_utils/subtitle_utils.py
+++ b/litellm/litellm_core_utils/audio_utils/subtitle_utils.py
@@ -3,6 +3,7 @@
import unicodedata
from collections.abc import Sequence
from dataclasses import dataclass
+from html import escape
from itertools import accumulate, groupby
from typing import Final
@@ -210,7 +211,7 @@ def _render_vtt(cues: Sequence[SubtitleCue]) -> str:
for cue in cues
for line in (
f"{_format_timestamp(cue.start_ms, '.')} --> {_format_timestamp(cue.end_ms, '.')}",
- cue.text,
+ escape(cue.text, quote=False),
"",
)
)
diff --git a/tests/test_litellm/litellm_core_utils/audio_utils/test_subtitle_utils.py b/tests/test_litellm/litellm_core_utils/audio_utils/test_subtitle_utils.py
index 210513f4967..45a9e7dc55b 100644
--- a/tests/test_litellm/litellm_core_utils/audio_utils/test_subtitle_utils.py
+++ b/tests/test_litellm/litellm_core_utils/audio_utils/test_subtitle_utils.py
@@ -1,3 +1,7 @@
+from typing import Final
+
+import pytest
+
from litellm.litellm_core_utils.audio_utils.subtitle_utils import (
SubtitleToken,
_merge_tokens_into_words,
@@ -105,6 +109,19 @@ class TestRenderSubtitleTokensAsSrt:
class TestRenderSubtitleTokensAsVtt:
+ @pytest.mark.parametrize(
+ ("text", "payload"),
+ (
+ ("Use bold.", "Use <b>bold</b>."),
+ ("Write & literally.", "Write & literally."),
+ ("A --> B", "A --> B"),
+ ("\"नमस्ते\" & 'hello'", "\"नमस्ते\" & 'hello'"),
+ ),
+ )
+ def test_transcript_text_is_escaped_for_cue_payload(self, text: str, payload: str) -> None:
+ tokens: Final = (SubtitleToken(text=text, start_ms=0, end_ms=1000),)
+ assert render_subtitle_tokens_as_vtt(tokens) == f"WEBVTT\n\n00:00:00.000 --> 00:00:01.000\n{payload}\n"
+
def test_single_cue_full_document(self):
tokens = (
SubtitleToken(text="Hello ", start_ms=0, end_ms=500),
@@ -130,6 +147,20 @@ class TestRenderSubtitleTokensAsVtt:
class TestSynthesizeSubtitleDocument:
+ @pytest.mark.parametrize(
+ ("response_format", "expected"),
+ (
+ ("vtt", "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\n<word> &\n"),
+ ("srt", "1\n00:00:00,000 --> 00:00:01,000\n &\n"),
+ ),
+ )
+ def test_transcript_escaping_is_specific_to_vtt(self, response_format: str, expected: str) -> None:
+ words: Final = (
+ {"word": "", "start": 0.0, "end": 0.5},
+ {"word": "&", "start": 0.5, "end": 1.0},
+ )
+ assert synthesize_subtitle_document(words, response_format) == expected
+
WORDS = [
{"word": "Four", "start": 0.4, "end": 0.7, "speaker": "spk:0"},
{"word": "score", "start": 0.7, "end": 1.1, "speaker": "spk:0"},