mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(proxy): return text/plain for audio transcription response_format=text
Fixes #31457 When callers request response_format=text, the proxy now returns a PlainTextResponse with the transcript body instead of serializing the normalized TranscriptionResponse object as JSON. LiteLLM response headers are forwarded to the plain-text response.
This commit is contained in:
parent
c14329128b
commit
96e18fc717
3 changed files with 64 additions and 2 deletions
|
|
@ -2,12 +2,14 @@
|
|||
Contains utils used by OpenAI compatible endpoints
|
||||
"""
|
||||
|
||||
from typing import Optional, Set
|
||||
from typing import Optional, Set, Union
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import PlainTextResponse
|
||||
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker
|
||||
from litellm.proxy.common_utils.http_parsing_utils import _read_request_body
|
||||
from litellm.types.utils import TranscriptionResponse
|
||||
|
||||
SENSITIVE_DATA_MASKER = SensitiveDataMasker()
|
||||
|
||||
|
|
@ -79,3 +81,22 @@ def get_custom_llm_provider_from_request_headers(request: Request) -> Optional[s
|
|||
if "custom-llm-provider" in request.headers:
|
||||
return request.headers["custom-llm-provider"]
|
||||
return None
|
||||
|
||||
|
||||
def get_transcription_proxy_response(
|
||||
response: TranscriptionResponse,
|
||||
response_format: Optional[str],
|
||||
headers: Optional[dict] = None,
|
||||
) -> Union[TranscriptionResponse, PlainTextResponse]:
|
||||
"""
|
||||
Preserve OpenAI-compatible HTTP envelopes for transcription response formats.
|
||||
|
||||
LiteLLM normalizes upstream transcription bodies into TranscriptionResponse
|
||||
objects, but callers requesting response_format=text expect a text/plain body.
|
||||
"""
|
||||
if response_format == "text":
|
||||
return PlainTextResponse(
|
||||
content=response.text or "",
|
||||
headers=headers or {},
|
||||
)
|
||||
return response
|
||||
|
|
|
|||
|
|
@ -310,6 +310,7 @@ from litellm.proxy.common_utils.load_config_utils import (
|
|||
)
|
||||
from litellm.proxy.common_utils.model_listing_utils import TeamModelNameTranslator
|
||||
from litellm.proxy.common_utils.openai_endpoint_utils import (
|
||||
get_transcription_proxy_response,
|
||||
remove_sensitive_info_from_deployment,
|
||||
)
|
||||
from litellm.proxy.common_utils.proxy_state import ProxyState
|
||||
|
|
@ -10051,7 +10052,11 @@ async def audio_transcriptions(
|
|||
if callback_headers:
|
||||
fastapi_response.headers.update(callback_headers)
|
||||
|
||||
return response
|
||||
return get_transcription_proxy_response(
|
||||
response=response,
|
||||
response_format=data.get("response_format"),
|
||||
headers=dict(fastapi_response.headers),
|
||||
)
|
||||
except Exception as e:
|
||||
await proxy_logging_obj.post_call_failure_hook(
|
||||
user_api_key_dict=user_api_key_dict, original_exception=e, request_data=data
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
from litellm.proxy.common_utils.openai_endpoint_utils import (
|
||||
get_transcription_proxy_response,
|
||||
)
|
||||
from litellm.types.utils import TranscriptionResponse
|
||||
|
||||
|
||||
def test_get_transcription_proxy_response_text_format_returns_plain_text():
|
||||
response = TranscriptionResponse(text="Hello world.")
|
||||
|
||||
result = get_transcription_proxy_response(
|
||||
response,
|
||||
"text",
|
||||
headers={"x-litellm-call-id": "test-call-id"},
|
||||
)
|
||||
|
||||
assert result.media_type.startswith("text/plain")
|
||||
assert result.body == b"Hello world."
|
||||
assert result.headers["x-litellm-call-id"] == "test-call-id"
|
||||
|
||||
|
||||
def test_get_transcription_proxy_response_json_format_returns_model():
|
||||
response = TranscriptionResponse(text="Hello world.")
|
||||
|
||||
result = get_transcription_proxy_response(response, "json")
|
||||
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
assert result.text == "Hello world."
|
||||
|
||||
|
||||
def test_get_transcription_proxy_response_default_format_returns_model():
|
||||
response = TranscriptionResponse(text="Hello world.")
|
||||
|
||||
result = get_transcription_proxy_response(response, None)
|
||||
|
||||
assert isinstance(result, TranscriptionResponse)
|
||||
assert result.text == "Hello world."
|
||||
Loading…
Add table
Reference in a new issue