Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): narrow audio_speech response before reading upstream content-type

mateo-berri99a6dd02af50
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 70057201af8..c996735ddbb 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -11068,8 +11068,11 @@ async def audio_speech(
custom_headers.update(callback_headers)
requested_format: Final = data.get("response_format")
+ upstream_content_type: Final = (
+ response.response.headers.get("content-type") if isinstance(response, HttpxBinaryResponseContent) else None
+ )
media_type: Final = resolve_speech_media_type(
- upstream_content_type=response.response.headers.get("content-type"),
+ upstream_content_type=upstream_content_type,
response_format=requested_format if isinstance(requested_format, str) else None,
)
diff --git a/tests/test_litellm/proxy/proxy_server/test_routes_audio.py b/tests/test_litellm/proxy/proxy_server/test_routes_audio.py
index 522a20cd34b..de76c7257cf 100644
--- a/tests/test_litellm/proxy/proxy_server/test_routes_audio.py
+++ b/tests/test_litellm/proxy/proxy_server/test_routes_audio.py
@@ -16,6 +16,7 @@ import httpx
import pytest
from litellm.proxy import proxy_server
+from litellm.types.llms.openai import HttpxBinaryResponseContent
@pytest.fixture
@@ -38,20 +39,14 @@ def patched_speech(monkeypatch, request):
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
- class _FakeBinaryResp:
- response = httpx.Response(
- status_code=200,
- headers={} if upstream_content_type is None else {"content-type": upstream_content_type},
- )
-
- async def aiter_bytes(self, chunk_size: int = 8192):
- async def _gen():
- yield b"\x00\x01\x02"
-
- return _gen()
-
async def _llm_call():
- return _FakeBinaryResp()
+ return HttpxBinaryResponseContent(
+ httpx.Response(
+ status_code=200,
+ headers={} if upstream_content_type is None else {"content-type": upstream_content_type},
+ content=b"\x00\x01\x02",
+ )
+ )
async def _fake_route_request(*args, **kwargs):
return _llm_call()