From dda5c10155563b2e825242bca588642e184efdf1 Mon Sep 17 00:00:00 2001 From: Vladimir Rogozhin Assistant Date: Sun, 11 Oct 2026 22:51:50 +0200 Subject: [PATCH 1/4] 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 (96e18fc717e128c3d89318785809880b5877efba). --- litellm/proxy/proxy_server.py | 13 +++ .../proxy/proxy_server/test_routes_audio.py | 108 +++++++++++++++++- 2 files changed, 118 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 089647c6297..b816008cc1f 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -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( diff --git a/tests/unit/proxy/proxy_server/test_routes_audio.py b/tests/unit/proxy/proxy_server/test_routes_audio.py index de76c7257cf..e171ce5d2f3 100644 --- a/tests/unit/proxy/proxy_server/test_routes_audio.py +++ b/tests/unit/proxy/proxy_server/test_routes_audio.py @@ -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 From 2de1520fea7cea0643a3d25c70c7dc5187c9ce58 Mon Sep 17 00:00:00 2001 From: Vladimir Rogozhin Assistant Date: Sun, 25 Oct 2026 11:43:47 +0100 Subject: [PATCH 2/4] test(proxy): assert the transcription failure hook --- tests/unit/proxy/proxy_server/test_routes_audio.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/proxy/proxy_server/test_routes_audio.py b/tests/unit/proxy/proxy_server/test_routes_audio.py index 653f51e5422..a891e4bec57 100644 --- a/tests/unit/proxy/proxy_server/test_routes_audio.py +++ b/tests/unit/proxy/proxy_server/test_routes_audio.py @@ -386,4 +386,8 @@ def test_audio_transcription_text_format_rejects_malformed_response( "code": "500", } } - assert proxy_server.proxy_logging_obj.post_call_failure_hook.await_count == 1 + logging_obj: Final = proxy_server.proxy_logging_obj + assert isinstance(logging_obj, MagicMock) + failure_hook: Final = logging_obj.post_call_failure_hook + assert isinstance(failure_hook, AsyncMock) + assert failure_hook.await_count == 1 From a9343059ee4f97ac206fd00b320dec7d6524d88c Mon Sep 17 00:00:00 2001 From: Vladimir Rogozhin Assistant Date: Sun, 25 Oct 2026 11:57:04 +0100 Subject: [PATCH 3/4] test(proxy): cover transcription errors for text requests --- .../proxy/proxy_server/test_routes_audio.py | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/unit/proxy/proxy_server/test_routes_audio.py b/tests/unit/proxy/proxy_server/test_routes_audio.py index a891e4bec57..d3f19c3b750 100644 --- a/tests/unit/proxy/proxy_server/test_routes_audio.py +++ b/tests/unit/proxy/proxy_server/test_routes_audio.py @@ -299,14 +299,31 @@ def test_audio_transcription_happy_path(client, auth_as, patched_transcription, @pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"]) -def test_audio_transcription_error(client, auth_as, patched_transcription_error, path): +@pytest.mark.parametrize("response_format", [None, "text"], ids=["default", "text"]) +def test_audio_transcription_error( + client: TestClient, + auth_as: Callable[..., AbstractContextManager[None]], + patched_transcription_error: None, + path: str, + response_format: str | None, +) -> None: """Pins ``POST /v1/audio/transcriptions`` / ``POST /audio/transcriptions`` (error).""" - files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")} - data = {"model": "whisper-1"} + files: Final = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")} + data: Final[dict[str, str]] = {"model": "whisper-1"} | ( + {"response_format": response_format} if response_format is not None else {} + ) with auth_as(): - response = client.post(path, files=files, data=data) + response: Final = client.post(path, files=files, data=data) assert response.status_code == 500 - assert len(response.content) > 0 + assert response.headers["content-type"] == "application/json" + assert response.json() == { + "error": { + "message": "transcription boom", + "type": "None", + "param": "None", + "code": "500", + } + } @pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"], ids=["v1", "alias"]) From 9130f24d49e6fae73ad2ed2b789c56b2a0010e6b Mon Sep 17 00:00:00 2001 From: Vladimir Rogozhin Assistant Date: Fri, 30 Oct 2026 04:44:53 +0100 Subject: [PATCH 4/4] test(proxy): assert transcription error format forwarding --- .../unit/proxy/proxy_server/test_routes_audio.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/unit/proxy/proxy_server/test_routes_audio.py b/tests/unit/proxy/proxy_server/test_routes_audio.py index 17ee040b44a..a1306b6c619 100644 --- a/tests/unit/proxy/proxy_server/test_routes_audio.py +++ b/tests/unit/proxy/proxy_server/test_routes_audio.py @@ -23,6 +23,7 @@ from fastapi.testclient import TestClient from litellm import Router from litellm.proxy import proxy_server from litellm.proxy._types import ProxyErrorTypes, ProxyException +from litellm.proxy.common_utils.http_parsing_utils import get_form_data from litellm.types.llms.openai import HttpxBinaryResponseContent from litellm.types.router import RouterErrors from litellm.types.utils import TranscriptionResponse @@ -154,12 +155,15 @@ def patched_transcription(monkeypatch): @pytest.fixture -def patched_transcription_error(monkeypatch, patched_transcription): +def patched_transcription_error(monkeypatch: pytest.MonkeyPatch, patched_transcription: None) -> AsyncMock: + monkeypatch.setattr(proxy_server, "get_form_data", get_form_data) + async def _raise(*args, **kwargs): raise ValueError("transcription boom") - monkeypatch.setattr(proxy_server, "route_request", _raise) - yield + route_request: Final[AsyncMock] = AsyncMock(side_effect=_raise) + monkeypatch.setattr(proxy_server, "route_request", route_request) + return route_request @pytest.fixture @@ -408,7 +412,7 @@ def test_audio_transcription_keeps_the_status_of_a_proxy_rejection( def test_audio_transcription_error( client: TestClient, auth_as: Callable[..., AbstractContextManager[None]], - patched_transcription_error: None, + patched_transcription_error: AsyncMock, path: str, response_format: str | None, ) -> None: @@ -435,6 +439,9 @@ def test_audio_transcription_error( "code": "500", } } + route_request_call: Final = patched_transcription_error.await_args + assert route_request_call is not None + assert route_request_call.kwargs["data"].get("response_format") == response_format @pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"], ids=["v1", "alias"])