fix(proxy): return transcription text as plain HTTP

Handle response_format=text on both transcription routes after collecting proxy and callback headers. Return the transcript as text/plain and recompute content headers while keeping unrelated headers. Add HTTP coverage for both aliases, Unicode, multiline and empty text, plus JSON defaults.

This continues the approach in PR #31479 by syf2211, adapted to the current route without cherry-picking the earlier commit. The original implementation commit was authored by Cursor Agent (96e18fc717).
This commit is contained in:
Vladimir Rogozhin Assistant 2026-10-11 22:51:50 +02:00
parent c0ca4a51f7
commit dda5c10155
2 changed files with 118 additions and 3 deletions

View file

@ -166,6 +166,7 @@ from litellm.types.utils import (
StreamingChoices,
TextCompletionResponse,
TokenCountResponse,
TranscriptionResponse,
echoed_cost_map_pricing_fields,
is_server_derived_pricing_key,
pricing_override_fields,
@ -996,6 +997,7 @@ from fastapi.responses import (
HTMLResponse,
JSONResponse,
ORJSONResponse,
PlainTextResponse,
RedirectResponse,
StreamingResponse,
)
@ -12990,6 +12992,7 @@ async def audio_transcriptions(
try:
# Use orjson to parse JSON data, orjson speeds up requests significantly
form_data: Final = await get_form_data(request)
requested_response_format: Final = form_data.get("response_format")
data = {key: value for key, value in form_data.items() if key != "file"} | data
# Include original request and headers in the data
@ -13088,6 +13091,16 @@ async def audio_transcriptions(
if callback_headers:
fastapi_response.headers.update(callback_headers)
if requested_response_format == "text":
response_headers: Final = {
key: value
for key, value in fastapi_response.headers.items()
if key.lower() not in {"content-type", "content-length"}
}
if not isinstance(response, TranscriptionResponse):
raise TypeError("Audio transcription response must be a TranscriptionResponse")
return PlainTextResponse(content=response.text or "", headers=response_headers)
return response
except Exception as e:
await proxy_logging_obj.post_call_failure_hook(

View file

@ -10,6 +10,7 @@ Pins (PR2):
from __future__ import annotations
import io
from typing import Final
from unittest.mock import AsyncMock, MagicMock
import httpx
@ -17,6 +18,7 @@ import pytest
from litellm.proxy import proxy_server
from litellm.types.llms.openai import HttpxBinaryResponseContent
from litellm.types.utils import TranscriptionResponse
@pytest.fixture
@ -119,9 +121,7 @@ def patched_transcription(monkeypatch):
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
monkeypatch.setattr(
proxy_server, "check_file_size_under_limit", lambda **kwargs: True
)
monkeypatch.setattr(proxy_server, "check_file_size_under_limit", lambda **kwargs: True)
async def _form_data(request):
from starlette.datastructures import FormData, UploadFile
@ -153,6 +153,54 @@ def patched_transcription_error(monkeypatch, patched_transcription):
yield
@pytest.fixture
def patched_transcription_http(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest):
transcript: Final = getattr(request, "param", "hello world")
router: Final = MagicMock()
router.model_names = ["whisper-1"]
router.get_deployment_by_model_group_name.return_value = None
monkeypatch.setattr(proxy_server, "llm_router", router)
async def _response_headers(data: dict[str, object], **kwargs: object) -> dict[str, str]:
return {
"x-callback-trace": "callback-value",
**(
{"Content-Type": "application/json", "Content-Length": "999"}
if data.get("response_format") == "text"
else {}
),
}
monkeypatch.setattr(
proxy_server,
"proxy_logging_obj",
MagicMock(
pre_call_hook=AsyncMock(side_effect=lambda **kw: kw["data"]),
post_call_failure_hook=AsyncMock(),
post_call_response_headers_hook=AsyncMock(side_effect=_response_headers),
update_request_status=AsyncMock(),
),
)
async def _add_data(data: dict[str, object], **kwargs: object) -> dict[str, object]:
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
async def _llm_call() -> TranscriptionResponse:
response: Final = TranscriptionResponse(text=transcript)
response.hidden_params = {
"litellm_call_id": "transcription-call-id",
"model_id": "transcription-model-id",
"cache_key": "transcription-cache-key",
}
return response
route_request: Final[AsyncMock] = AsyncMock(return_value=_llm_call())
monkeypatch.setattr(proxy_server, "route_request", route_request)
return transcript, route_request
@pytest.mark.parametrize("path", ["/v1/audio/speech", "/audio/speech"])
def test_audio_speech_happy_path(client, auth_as, patched_speech, path):
"""Pins ``POST /v1/audio/speech`` and ``POST /audio/speech`` (happy)."""
@ -254,3 +302,57 @@ def test_audio_transcription_error(client, auth_as, patched_transcription_error,
response = client.post(path, files=files, data=data)
assert response.status_code == 500
assert len(response.content) > 0
@pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"], ids=["v1", "alias"])
@pytest.mark.parametrize(
"patched_transcription_http",
["hello world", "Grüße, 世界\nline two\n", ""],
indirect=True,
ids=["plain", "unicode-multiline", "empty"],
)
def test_audio_transcription_text_format_returns_plain_text(client, auth_as, patched_transcription_http, path):
transcript, route_request = patched_transcription_http
files: Final = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
data: Final = {"model": "whisper-1", "response_format": "text"}
with auth_as():
response: Final = client.post(
path,
files=files,
data=data,
headers={"x-litellm-call-id": "request-call-id"},
)
assert response.status_code == 200
assert response.headers["content-type"] == "text/plain; charset=utf-8"
assert response.content == transcript.encode("utf-8")
assert response.headers["content-length"] == str(len(transcript.encode("utf-8")))
assert response.headers["x-litellm-call-id"] == "transcription-call-id"
assert response.headers["x-litellm-model-id"] == "transcription-model-id"
assert response.headers["x-litellm-cache-key"] == "transcription-cache-key"
assert response.headers["x-callback-trace"] == "callback-value"
assert route_request.await_args.kwargs["data"]["response_format"] == "text"
@pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"], ids=["v1", "alias"])
@pytest.mark.parametrize("response_format", [None, "json"], ids=["default", "json"])
def test_audio_transcription_json_formats_keep_body_and_headers(
client, auth_as, patched_transcription_http, path, response_format
):
transcript, route_request = patched_transcription_http
files: Final = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
data: Final = {"model": "whisper-1"} | ({"response_format": response_format} if response_format is not None else {})
with auth_as():
response: Final = client.post(
path,
files=files,
data=data,
headers={"x-litellm-call-id": "request-call-id"},
)
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.json() == {"text": transcript, "usage": None}
assert response.headers["x-litellm-call-id"] == "transcription-call-id"
assert response.headers["x-litellm-model-id"] == "transcription-model-id"
assert response.headers["x-litellm-cache-key"] == "transcription-cache-key"
assert response.headers["x-callback-trace"] == "callback-value"
assert route_request.await_args.kwargs["data"].get("response_format") == response_format