litellm/tests/unit/proxy/proxy_server/test_routes_audio.py
Emerson Gomes ecb8ef5ca8
feat(realtime)!: support latest OpenAI audio models (#35600)
* feat(realtime): support latest OpenAI audio models

* fix(transcription): type SDK stream duration metadata explicitly

* fix(realtime): preserve transport settings and translation duration

* refactor(realtime): reuse SDK client initialization

* fix(audio): bill finalized streams and authorize transcription models

* refactor(audio): keep transcription pricing selection together

* fix(realtime): reconcile model metadata and test contracts

* fix(realtime): preserve provider duration and model capabilities

* fix(realtime): keep transcription validation compatible with cost map guard

* fix(realtime): address transcription and billing review findings

* fix(realtime): preserve input usage and provider routing

* fix(realtime): bill retained output and route Azure aliases

* fix(realtime): reject malformed provider durations

* fix(realtime): preserve batch cache rates and Azure v1 routes

* fix(transcription): reject nonfinite provider durations

* refactor(realtime): align annotations with current lint rules

* fix(realtime): preserve translation deployment pricing and input usage

* test(transcription): verify synchronous stream finalization

* fix(realtime): retain missing translation input usage at close

* fix(transcription): reject streams that bypass output guardrails

* fix(realtime): reconcile audio protocols billing and current CI

* fix(realtime): trust confirmed formats and authorize default models

* fix(realtime): preserve absent models and reject unsupported guardrails

* fix(realtime): reject translation policy pipelines

* chore(ci): rerun transient infrastructure timing failure

* fix(realtime): reconcile audio protocols with current upstream

* fix(audio): validate provider responses at SDK boundaries

* fix(realtime): preserve minimum OpenAI SDK compatibility

* fix(realtime): preserve deferred Live session authorization

* fix(realtime): preserve upstream authentication and transcription behavior

* fix(audio): scope provider duration precedence to configured models

* fix(typing): avoid Final declarations inside transcription cost loop

* refactor(audio): reuse transcription mapping and duration resolution

* fix(realtime): enforce model budgets and preserve translation routing

* fix(audio): log streamed transcripts whose done event reports detected languages

* fix(audio): pass stream, languages and keywords through unchecked for non-OpenAI transcription providers

Providers without a streaming transcription handler got UnsupportedParamsError
for stream=True, languages and keywords, where the merge base forwarded them as
provider-specific params. Only openai and azure validate them now.

* fix(audio): bill streamed gpt-transcribe from the provider's reported duration

The stream path computed cost after logging had replaced the transcription's
duration usage, so it fell back to the measured file length while the
unstreamed call billed the provider's seconds.

* fix(transcription): log streamed transcription usage from the provider response

The stream logging path built the spend payload from the response after
its duration usage was transformed away, so whisper-1 with stream=true and
streamed gpt-transcribe logged an empty usage object. Bill and log from the
provider's TranscriptionResponse in both the sync and async paths.

* fix(realtime): keep main's routing, protocol and transport behavior around the new translation routes

- Send OpenAI realtime and translation client_secrets and calls through litellm's httpx client again, so ssl_verify and custom CA settings apply and upstream errors keep main's shape
- Use the input transcription model as the session model only for transcription sessions
- Drop the new /openai/v1 translation aliases and record each lazy feature's own routes, so the /openai pass-through and the MCP routes resolve as on main
- Let a configured Azure realtime_protocol win for every model, with GA as the default only when none is set
- Return x-litellm headers on streamed transcriptions and mark the request done
- Refuse translation sessions before connecting upstream when realtime guardrails apply or the provider is not OpenAI or Azure
- Accept json and text for gpt-transcribe, and drop other formats under drop_params instead of failing
- Bill translation sessions on output audio seconds only, at the single published rate
- Document the 201 SDP answer on the translation calls routes and the transcription 200 response

* fix(realtime): restore main behavior for guardrails, Azure transcription, and realtime clients

Streamed transcription runs unstreamed when a deployment post_call guardrail applies, so the
guardrail still sees the transcript. Azure transcription maps v1, latest and preview to the
default dated version on the deployments route for every model. The SDK client secret keeps
session.model ahead of model, realtime WebSockets connect to api_base again, realtime metadata
keeps only guardrails, and managed agents need an explicit transcription model. Reverts the two
unrelated streamed Responses error codes and the duplicate cached_tokens_details field, and the
gpt-transcribe format check only reads the request provider's cost map entry.

* fix(realtime): keep main's transcription, client secret and route behavior where the PR changed it

Transcription reaches every provider as on main again, sends stream only when asked, forwards language and
languages together, keeps form key order, bills provider seconds only when above zero, honors base_model for the
new flags, keeps main's Azure api_version on the proxy path, and streams carry chat's headers, an error event on a
mid-stream failure, and a spend row when the stream ends early. Translation cost reads both per-second rates from
one source and spend rows carry the billed seconds.

Realtime transcription WebSockets keep model in the upstream URL outside OpenAI's hosts, client secrets forward
explicit nulls, accept conversation and delay, and answer 400 on a malformed body, the /openai translation paths
are back to main, translation refuses only for guardrails realtime sessions run, nested aliases resolve and later
session.update models are authorized, translation client secrets run per-model auth, beta clients bridged to GA
get beta-shaped session events, and transcription sessions send the deployment model in both shapes.

* fix(realtime): address review findings on transcription billing, streaming and realtime auth

Per-second pricing replaces token billing only when the priced entry sets no token rates, translation rates fall back per key to the cost map, transcription cache keys include languages and keywords, a stream request a post_call guardrail forces unstreamed comes back as redacted SSE, Azure beta clients are bridged only for GA-only deployments, nested transcription models on the realtime WebSocket are checked against the key, and translation spend carries key and team metadata

* fix(azure): pick the GA realtime protocol for deployments whose model_info.base_model is GA-only

* fix(realtime): preserve prior merge resolutions after rebase

* fix(realtime): enforce translation policies and normalize Azure transcription URLs

* fix(cookbook): stop translation once output idles after the upload ends

* fix(realtime): bill translation on input audio and drop pre-read stream cost headers

Translation spend now uses the input audio seconds the session sent, priced
through input_cost_per_second, instead of the translated output duration.
A streamed transcription no longer returns cost headers before the stream is
read, since the cost is only known after the last event. Also removes an
unused realtime guardrail helper.

* fix(realtime): drop top-level session nulls, bill dropped transcription streams, and bridge beta response fields

Client secret, translation and transcription session bodies drop top-level nulls again, as main did, while nested nulls such as turn_detection still pass through. Transcription client secrets send the deployment model as session.model. A streamed transcription the client drops still runs its success callbacks, and the stream ends with data: [DONE] like OpenAI's. The beta to GA bridge maps max_response_output_tokens, speed and noise reduction, remaps response.create, and drops temperature under drop_params. The Entra token for realtime HTTP routes is fetched off the event loop

* test(e2e): cover realtime translation, live transcription, client secrets, and streamed transcription

* fix(realtime): close review gaps in translation billing, transcription streams, and Azure GA

Bill translation sessions from the deployment's own per-second rates, add per-second prices for
Azure gpt-4o transcribe models, keep stream-scoped guardrails seeing the caller's stream flag, map
mid-stream transcription errors to LiteLLM exceptions with a [DONE] frame, map GA responses back to
the beta shape for bridged beta clients, honor the minting key's guardrail opt-out on translation
calls, restore the Azure transcription session URL, and flag GA-only Azure realtime models

* fix(realtime): bill a translation rate the deployment omits from the session model

* test(e2e): check translated speech is not English instead of exact French wording

* fix(realtime): keep main's behavior for non-bridged backends, stream error frames, gateway URLs and Azure transcribe prices

Remap beta events to GA only for bridged beta clients and translation sessions, bridge beta clients on GA-pinned Azure deployments, log error-only upstream sessions as failures, flag dated Azure GA realtime models, drop the unpublished Azure per-second transcribe rates, log a mid-stream transcription failure once, keep main's SSE error frame outside transcription, drop unsupported transcription params under drop_params, keep the caller's Azure auth headers, keep a gateway /v1 path, let key opt-outs release string-callback guardrails on translation, and keep the cookbook waiting while a long upload is still sending

* fix(realtime): keep the proxy's realtime session gate off translation sessions

A key that opts out of a legacy callback guardrail now gets a working translation session, since the transcription-guardrail gate no longer sends a realtime session.update the translation upstream rejects

* fix(realtime): keep websocket model limits, bill measured transcription length, route transcription sessions on the allowed model

A WebSocket opened without a query model now authorizes as an empty model, transcription cost prefers the measured upload length over the provider's reported seconds, transcription sessions overwrite nested models with the authorized top-level model again, and a non-boolean transcription stream value answers 400 or is dropped under drop_params.

* fix(ci): copy the rust bridge extension into the openapi docs gate's base worktree

* fix(model-catalog): accept the realtime translation endpoints in the cost map

* fix(cookbook): close the translation session and read until session.closed

* fix(realtime): treat an empty transcription stream as unset and pass the cost-map guard

An empty `stream` form value runs the transcription unstreamed again, as
OpenAI does, instead of answering 400. The four translate cost-map entries
drop the translation routes from supported_endpoints until the main
branch's catalog parser accepts them, and the nested transcription model
lookup uses the router registry entry point.

* test(proxy): drop the mid-stream error frame pin that main's error shape change replaced

* fix(transcription): bill uploaded seconds when a stream fails mid-transcript

* fix(proxy): bill uploaded seconds when a streamed transcription breaks mid-stream

* fix(audio): keep transcription's positional arguments and redact streamed error details

- Move the new languages, keywords and stream options to the end of transcription's parameters, so a prompt or format passed by position keeps its meaning
- Redact internal hostnames, paths and tracebacks from a streamed transcription's mid-stream error event, as chat streams already do
- Move the CA-bundle check for the realtime HTTP routes, which serves real TLS on loopback, from the unit suite to the SDK integration suite

* fix(audio): stream the guarded transcript to SDK callers that asked for a stream

* fix(logging): keep a streamed transcript out of callbacks that turned off message logging

A streamed transcription's assembled response reached success callbacks with its text intact under turn_off_message_logging or a callback's message_logging=False, because the shared streaming redactor had no transcription branch. It now redacts the text, as the unstreamed call already does

* fix(proxy): stream a transcription the deployment streamed when the client sent no stream field

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-03 07:27:52 +00:00

1079 lines
40 KiB
Python

"""Behavior pins for ``proxy_server.py`` audio routes.
Pins (PR2):
- POST /v1/audio/speech
- POST /audio/speech
- POST /v1/audio/transcriptions
- POST /audio/transcriptions
"""
from __future__ import annotations
import contextlib
import io
import json
import math
import wave
from collections.abc import AsyncIterator, Callable, Mapping
from contextlib import AbstractContextManager
from typing import Final
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
import respx
from fastapi import HTTPException
from fastapi.testclient import TestClient
from openai import AsyncOpenAI
from starlette.datastructures import FormData, UploadFile
import litellm
from litellm import Router
from litellm.caching.caching import Cache
from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.proxy import proxy_server
from litellm.proxy._types import ProxyErrorTypes, ProxyException
from litellm.proxy.route_llm_request import route_request
from litellm.proxy.utils import _failure_fields_to_lift
from litellm.types.guardrails import GuardrailEventHooks, GuardrailStreamScope
from litellm.types.llms.openai import HttpxBinaryResponseContent
from litellm.types.proxy.policy_engine.pipeline_types import GuardrailPipeline, PipelineStep
from litellm.types.router import RouterErrors
from litellm.types.utils import TranscriptionResponse
_TAG_ROUTING_DENIAL = f"{RouterErrors.no_deployments_with_tag_routing.value}. Passed model=tts-1 and tags=['x']"
@pytest.fixture
def patched_speech(monkeypatch, request):
upstream_content_type = getattr(request, "param", "audio/mpeg")
monkeypatch.setattr(proxy_server, "llm_router", MagicMock())
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(return_value={}),
update_request_status=AsyncMock(),
),
)
async def _add_data(data, **kwargs):
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
async def _llm_call():
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()
monkeypatch.setattr(proxy_server, "route_request", _fake_route_request)
yield
@pytest.fixture
def patched_speech_error(monkeypatch):
monkeypatch.setattr(proxy_server, "llm_router", MagicMock())
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(return_value={}),
update_request_status=AsyncMock(),
),
)
async def _add_data(data, **kwargs):
return data
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _add_data)
async def _raise(*args, **kwargs):
raise ValueError("speech boom")
monkeypatch.setattr(proxy_server, "route_request", _raise)
yield
@pytest.fixture
def patched_speech_provider_rejection(monkeypatch, patched_speech_error):
import litellm
async def _raise(*args, **kwargs):
raise litellm.BadRequestError(
message=(
"Gemini TTS only produces raw PCM16 audio, so response_format='mp3' is not supported."
" Supported response formats: pcm, wav."
),
model="gemini-3.1-flash-tts-preview",
llm_provider="gemini",
)
monkeypatch.setattr(proxy_server, "route_request", _raise)
yield
@pytest.fixture
def patched_transcription(monkeypatch):
router = MagicMock()
router.model_names = ["whisper-1"]
monkeypatch.setattr(proxy_server, "llm_router", router)
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(return_value={}),
update_request_status=AsyncMock(),
),
)
async def _add_data(data, **kwargs):
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)
async def _form_data(request):
from starlette.datastructures import FormData, UploadFile
upload = UploadFile(
filename="audio.mp3",
file=io.BytesIO(b"\x00\x01\x02"),
)
return FormData([("file", upload), ("model", "whisper-1")])
monkeypatch.setattr(proxy_server, "get_form_data", _form_data)
async def _llm_call():
return {"text": "hello world"}
async def _fake_route_request(*args, **kwargs):
return _llm_call()
monkeypatch.setattr(proxy_server, "route_request", _fake_route_request)
yield
@pytest.fixture
def patched_transcription_error(monkeypatch, patched_transcription):
async def _raise(*args, **kwargs):
raise ValueError("transcription boom")
monkeypatch.setattr(proxy_server, "route_request", _raise)
yield
@pytest.fixture
def patched_transcription_stream(monkeypatch, patched_transcription):
class _FakeEvent:
def model_dump_json(self, exclude_unset: bool = False) -> str:
return '{"type":"transcript.text.done","text":"hello world"}'
class _FakeAsyncStream:
def __init__(self):
self.closed = False
def __aiter__(self):
async def _events():
yield _FakeEvent()
return _events()
async def aclose(self):
self.closed = True
async def _form_data(request):
from starlette.datastructures import FormData, UploadFile
upload = UploadFile(
filename="audio.mp3",
file=io.BytesIO(b"\x00\x01\x02"),
)
return FormData([("file", upload), ("model", "gpt-transcribe"), ("stream", "true")])
stream = _FakeAsyncStream()
async def _llm_call():
return stream
async def _fake_route_request(*args, **kwargs):
return _llm_call()
monkeypatch.setattr(proxy_server, "get_form_data", _form_data)
monkeypatch.setattr(proxy_server, "route_request", _fake_route_request)
yield stream
@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)."""
payload = {"model": "tts-1", "input": "Hi", "voice": "alloy"}
with auth_as():
response = client.post(path, json=payload)
assert response.status_code == 200
response_summary = {
"status_code": response.status_code,
"content_type": response.headers.get("content-type", ""),
"body_bytes": response.content,
}
assert response_summary == {
"status_code": 200,
"content_type": "audio/mpeg",
"body_bytes": b"\x00\x01\x02",
}
@pytest.mark.parametrize(
("patched_speech", "response_format", "expected_content_type"),
[
("audio/wav", "wav", "audio/wav"),
("audio/flac", "flac", "audio/flac"),
("audio/pcm", "pcm", "audio/pcm"),
("audio/wav", "mp3", "audio/wav"),
("application/json", "flac", "audio/flac"),
(None, "wav", "audio/wav"),
(None, None, "audio/mpeg"),
],
indirect=["patched_speech"],
)
def test_audio_speech_content_type_matches_audio_format(
client, auth_as, patched_speech, response_format, expected_content_type
):
"""Regression for LIT-6482: /v1/audio/speech mislabeled wav/flac/pcm as audio/mpeg."""
payload = {
"model": "tts-1",
"input": "Hi",
"voice": "alloy",
**({} if response_format is None else {"response_format": response_format}),
}
with auth_as():
response = client.post("/v1/audio/speech", json=payload)
assert response.status_code == 200
assert response.headers.get("content-type", "").split(";")[0] == expected_content_type
@pytest.mark.parametrize("path", ["/v1/audio/speech", "/audio/speech"])
def test_audio_speech_error(client, auth_as, patched_speech_error, path):
"""Pins ``POST /v1/audio/speech`` and ``POST /audio/speech`` (error)."""
payload = {"model": "tts-1", "input": "Hi", "voice": "alloy"}
with auth_as():
response = client.post(path, json=payload)
assert response.status_code == 500
error = response.json()["error"]
assert (error["type"], error["param"]) == ("internal_server_error", None)
def test_audio_speech_bad_request_maps_to_400(client, auth_as, patched_speech_provider_rejection):
"""Regression for LIT-6501: a BadRequestError from the speech path surfaced as a generic 500."""
payload = {"model": "gemini-tts", "input": "Hi", "voice": "Kore", "response_format": "mp3"}
with auth_as():
response = client.post("/v1/audio/speech", json=payload)
assert response.status_code == 400
error = response.json()["error"]
assert "response_format='mp3'" in error["message"]
assert "pcm" in error["message"]
assert "wav" in error["message"]
@pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"])
def test_audio_transcription_happy_path(client, auth_as, patched_transcription, path):
"""Pins ``POST /v1/audio/transcriptions`` / ``POST /audio/transcriptions`` (happy)."""
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
data = {"model": "whisper-1"}
with auth_as():
response = client.post(path, files=files, data=data)
assert response.status_code == 200
body = response.json()
assert body == {"text": "hello world"}
response_summary = {
"status_code": response.status_code,
"text_field": body["text"],
"media_type_hint": response.headers.get("content-type", "").split(";")[0],
}
assert response_summary == {
"status_code": 200,
"text_field": "hello world",
"media_type_hint": "application/json",
}
@pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"])
def test_audio_transcription_over_the_file_size_cap_is_rejected_as_a_bad_request(
client, auth_as, patched_transcription, monkeypatch, path
):
from litellm.proxy.common_utils.http_parsing_utils import check_file_size_under_limit
capped_router = Router(
model_list=[
{"model_name": "whisper-1", "litellm_params": {"model": "openai/whisper-1", "max_file_size_mb": 0.000001}}
]
)
monkeypatch.setattr(proxy_server, "llm_router", capped_router)
monkeypatch.setattr(proxy_server, "premium_user", True)
monkeypatch.setattr(proxy_server, "check_file_size_under_limit", check_file_size_under_limit)
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
with auth_as():
response = client.post(path, files=files, data={"model": "whisper-1"})
assert response.status_code == 400
error = response.json()["error"]
assert "File size is too large" in error["message"]
assert error["param"] == "file"
def test_audio_speech_tag_routing_denial_is_typed_as_a_proxy_auth_error(
client, auth_as, patched_speech_error, monkeypatch
):
async def _deny(*args, **kwargs):
raise ValueError(_TAG_ROUTING_DENIAL)
monkeypatch.setattr(proxy_server, "route_request", _deny)
with auth_as():
response = client.post("/v1/audio/speech", json={"model": "tts-1", "input": "Hi", "voice": "alloy"})
assert response.status_code == 401
error = response.json()["error"]
assert (error["type"], error["param"]) == ("auth_error", None)
@pytest.mark.parametrize(
("failure", "expected_status", "expected_type"),
[
pytest.param(ValueError(_TAG_ROUTING_DENIAL), 401, "auth_error", id="tag_routing_denial"),
pytest.param(
HTTPException(status_code=403, detail="Blocked by the content policy"),
403,
"permission_error",
id="hook_policy_block",
),
],
)
def test_audio_transcription_failure_is_typed_by_who_rejected_it(
client, auth_as, patched_transcription, monkeypatch, failure, expected_status, expected_type
):
async def _deny(*args, **kwargs):
raise failure
monkeypatch.setattr(proxy_server, "route_request", _deny)
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
with auth_as():
response = client.post("/v1/audio/transcriptions", files=files, data={"model": "whisper-1"})
assert response.status_code == expected_status
error = response.json()["error"]
assert (error["type"], error["param"]) == (expected_type, None)
@pytest.mark.parametrize(
"rejection",
[
pytest.param(
ProxyException(
message="Invalid type for 'metadata': expected an object, but got an integer instead.",
type=ProxyErrorTypes.bad_request_error,
param="metadata",
code=400,
),
id="bad_metadata",
),
pytest.param(
ProxyException(
message="tag budgets are unavailable", type=ProxyErrorTypes.no_db_connection, param=None, code=503
),
id="tag_budget_store_down",
),
],
)
def test_audio_transcription_keeps_the_status_of_a_proxy_rejection(
client, auth_as, patched_transcription, monkeypatch, rejection
):
async def _reject(data, **kwargs):
raise rejection
monkeypatch.setattr(proxy_server, "add_litellm_data_to_request", _reject)
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
with auth_as():
response = client.post("/v1/audio/transcriptions", files=files, data={"model": "whisper-1"})
assert response.status_code == int(rejection.code)
assert response.json()["error"] == rejection.to_dict()
@pytest.mark.parametrize("path", ["/v1/audio/transcriptions", "/audio/transcriptions"])
def test_audio_transcription_error(client, auth_as, patched_transcription_error, path):
"""Pins ``POST /v1/audio/transcriptions`` / ``POST /audio/transcriptions`` (error)."""
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
data = {"model": "whisper-1"}
with auth_as():
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"])
def test_audio_transcription_stream_returns_sse(client, auth_as, patched_transcription_stream, path):
files = {"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")}
data = {"model": "gpt-transcribe", "stream": "true"}
with auth_as():
response = client.post(path, files=files, data=data)
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/event-stream")
assert response.text == 'data: {"type":"transcript.text.done","text":"hello world"}\n\ndata: [DONE]\n\n'
assert patched_transcription_stream.closed is True
@pytest.mark.usefixtures("patched_transcription_stream")
@pytest.mark.parametrize("configuration", ["default", "policy"])
def test_streaming_transcription_keeps_streaming_with_output_guardrails(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
configuration: str,
) -> None:
guardrail: Final = CustomGuardrail(
guardrail_name="transcription-output",
event_hook=GuardrailEventHooks.post_call,
default_on=configuration == "default",
)
monkeypatch.setattr(litellm, "callbacks", [guardrail])
if configuration == "policy":
pipeline: Final = GuardrailPipeline(mode="post_call", steps=[PipelineStep(guardrail="transcription-output")])
proxy_server.proxy_logging_obj.pre_call_hook.side_effect = lambda **kwargs: {
**kwargs["data"],
"metadata": {"_guardrail_pipelines": [("transcription-policy", pipeline)]},
}
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/event-stream")
assert '"text":"hello world"' in response.text
@pytest.mark.usefixtures("patched_transcription_stream")
def test_streamed_transcription_returns_the_litellm_call_and_model_headers(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
) -> None:
upstream: Final = AsyncOpenAI(
api_key="sk-test",
base_url="https://upstream.example/v1",
http_client=httpx.AsyncClient(
transport=httpx.MockTransport(
lambda request: httpx.Response(
200,
content=b'data: {"type":"transcript.text.done","text":"hello world"}\n\n',
headers={"content-type": "text/event-stream"},
)
)
),
)
async def route_to_the_sdk(
data: Mapping[str, object], route_type: str, llm_router: object, user_model: object
) -> object:
return litellm.atranscription(
model="openai/gpt-transcribe",
file=data["file"],
stream=True,
client=upstream,
litellm_call_id=data["litellm_call_id"],
model_info={"id": "transcription-deployment"},
)
monkeypatch.setattr(proxy_server, "route_request", route_to_the_sdk)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
headers={"x-litellm-call-id": "streamed-transcription-call"},
)
assert response.status_code == 200
assert '"text":"hello world"' in response.text
assert response.headers["x-litellm-call-id"] == "streamed-transcription-call"
assert response.headers["x-litellm-model-id"] == "transcription-deployment"
assert [name for name in response.headers if name.startswith("x-litellm-response-cost")] == []
proxy_server.proxy_logging_obj.update_request_status.assert_called_once_with(
litellm_call_id="streamed-transcription-call", status="success"
)
class _TranscriptRedactor(CustomGuardrail):
def __init__(self, stream_scope: GuardrailStreamScope = "both") -> None:
super().__init__(
guardrail_name="transcript-redactor", event_hook=GuardrailEventHooks.post_call, stream_scope=stream_scope
)
async def async_post_call_success_hook(
self, data: dict[str, object], user_api_key_dict: object, response: object
) -> None:
if isinstance(response, TranscriptionResponse):
response.text = "[REDACTED]"
def _streams_when_asked(request: httpx.Request) -> httpx.Response:
if b'name="stream"' in request.content:
return httpx.Response(
200,
content=b'data: {"type":"transcript.text.done","text":"my pin is 1234"}\n\n',
headers={"content-type": "text/event-stream"},
)
return httpx.Response(200, json={"text": "my pin is 1234", "usage": {"type": "duration", "seconds": 2}})
def _sse_events(body: str) -> tuple[object, ...]:
assert body.endswith("data: [DONE]\n\n")
return tuple(json.loads(frame.removeprefix("data: ")) for frame in body.removesuffix("data: [DONE]\n\n").split("\n\n") if frame)
@pytest.mark.usefixtures("patched_transcription_stream")
@pytest.mark.parametrize("deployment_guardrails", (["transcript-redactor"], None))
def test_streamed_transcription_honors_a_deployment_post_call_guardrail(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
deployment_guardrails: list[str] | None,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setattr(litellm, "callbacks", [_TranscriptRedactor()])
respx_mock.post("https://transcribe-upstream.example/v1/audio/transcriptions").mock(side_effect=_streams_when_asked)
deployment_params: Final = {
"model": "openai/gpt-transcribe",
"api_key": "sk-test",
"api_base": "https://transcribe-upstream.example/v1",
}
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{
"model_name": "gpt-transcribe",
"litellm_params": (
{**deployment_params, "guardrails": deployment_guardrails}
if deployment_guardrails
else deployment_params
),
}
]
),
)
monkeypatch.setattr(proxy_server, "route_request", route_request)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert response.status_code == 200
if deployment_guardrails:
assert response.headers["content-type"].startswith("text/event-stream")
assert _sse_events(response.text) == (
{"type": "transcript.text.delta", "delta": "[REDACTED]"},
{"type": "transcript.text.done", "text": "[REDACTED]", "usage": {"type": "duration", "seconds": 2}},
)
else:
assert response.headers["content-type"].startswith("text/event-stream")
assert '"text":"my pin is 1234"' in response.text
@pytest.mark.usefixtures("patched_transcription_stream")
@pytest.mark.parametrize(
"stream_scope,upstream_streamed,expected_text",
[("streaming", False, "[REDACTED]"), ("non_streaming", True, "my pin is 1234")],
)
def test_streamed_transcription_applies_a_deployment_guardrail_only_in_its_stream_scope(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
stream_scope: GuardrailStreamScope,
upstream_streamed: bool,
expected_text: str,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setattr(litellm, "callbacks", [_TranscriptRedactor(stream_scope=stream_scope)])
upstream_route: Final = respx_mock.post("https://transcribe-upstream.example/v1/audio/transcriptions").mock(
side_effect=_streams_when_asked
)
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{
"model_name": "gpt-transcribe",
"litellm_params": {
"model": "openai/gpt-transcribe",
"api_key": "sk-test",
"api_base": "https://transcribe-upstream.example/v1",
"guardrails": ["transcript-redactor"],
},
}
]
),
)
monkeypatch.setattr(proxy_server, "route_request", route_request)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert response.status_code == 200, response.text
assert response.headers["content-type"].startswith("text/event-stream")
assert (b'name="stream"' in upstream_route.calls.last.request.content) is upstream_streamed
assert _sse_events(response.text)[-1]["text"] == expected_text
async def _form_without_stream_field(request: object) -> FormData:
return FormData(
[("file", UploadFile(filename="sample.wav", file=io.BytesIO(b"audio"))), ("model", "gpt-transcribe")]
)
@pytest.mark.usefixtures("patched_transcription")
@pytest.mark.parametrize("deployment_guardrails", (["transcript-redactor"], None))
def test_a_deployment_that_streams_transcription_answers_sse_to_a_client_that_sent_no_stream_field(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
deployment_guardrails: list[str] | None,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setattr(litellm, "callbacks", [_TranscriptRedactor()])
monkeypatch.setattr(proxy_server, "get_form_data", _form_without_stream_field)
respx_mock.post("https://transcribe-upstream.example/v1/audio/transcriptions").mock(side_effect=_streams_when_asked)
deployment_params: Final = {
"model": "openai/gpt-transcribe",
"api_key": "sk-test",
"api_base": "https://transcribe-upstream.example/v1",
"stream": True,
}
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{
"model_name": "gpt-transcribe",
"litellm_params": (
{**deployment_params, "guardrails": deployment_guardrails}
if deployment_guardrails
else deployment_params
),
}
]
),
)
monkeypatch.setattr(proxy_server, "route_request", route_request)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe"},
)
assert response.status_code == 200, response.text
assert response.headers["content-type"].startswith("text/event-stream")
assert _sse_events(response.text)[-1]["text"] == ("[REDACTED]" if deployment_guardrails else "my pin is 1234")
header_hook_response: Final = proxy_server.proxy_logging_obj.post_call_response_headers_hook.await_args.kwargs[
"response"
]
if deployment_guardrails:
assert isinstance(header_hook_response, TranscriptionResponse)
assert header_hook_response.text == "[REDACTED]"
@pytest.mark.usefixtures("patched_transcription_stream")
def test_cached_guarded_transcription_still_streams_to_a_stream_client(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setattr(litellm, "callbacks", [_TranscriptRedactor()])
monkeypatch.setattr(litellm, "cache", Cache(type="local"))
upstream_route: Final = respx_mock.post("https://transcribe-upstream.example/v1/audio/transcriptions").mock(
side_effect=_streams_when_asked
)
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{
"model_name": "gpt-transcribe",
"litellm_params": {
"model": "openai/gpt-transcribe",
"api_key": "sk-test",
"api_base": "https://transcribe-upstream.example/v1",
"guardrails": ["transcript-redactor"],
},
}
],
cache_responses=True,
),
)
monkeypatch.setattr(proxy_server, "route_request", route_request)
with auth_as():
responses: Final = [
client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
for _ in range(2)
]
assert upstream_route.call_count == 1
assert [response.headers["content-type"].split(";")[0] for response in responses] == ["text/event-stream"] * 2
assert _sse_events(responses[1].text)[-1] == {
"type": "transcript.text.done",
"text": "[REDACTED]",
"usage": {"type": "duration", "seconds": 2},
}
@pytest.mark.usefixtures("patched_transcription_stream")
def test_streamed_transcription_frames_keep_the_provider_event_shape(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
) -> None:
provider_events: Final = (
{"type": "transcript.text.delta", "delta": "hello"},
{"type": "transcript.text.done", "text": "hello", "usage": {"type": "duration", "seconds": 2}},
)
upstream: Final = AsyncOpenAI(
api_key="sk-test",
base_url="https://upstream.example/v1",
http_client=httpx.AsyncClient(
transport=httpx.MockTransport(
lambda request: httpx.Response(
200,
content="".join(f"data: {json.dumps(event)}\n\n" for event in provider_events).encode(),
headers={"content-type": "text/event-stream"},
)
)
),
)
async def route_to_the_sdk(
data: Mapping[str, object], route_type: str, llm_router: object, user_model: object
) -> object:
return litellm.atranscription(
model="openai/gpt-transcribe", file=data["file"], stream=data["stream"], client=upstream
)
monkeypatch.setattr(proxy_server, "route_request", route_to_the_sdk)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert response.status_code == 200
assert _sse_events(response.text) == provider_events
class _FirstDeltaThenReset(httpx.AsyncByteStream):
async def __aiter__(self) -> AsyncIterator[bytes]:
yield b'data: {"type":"transcript.text.delta","delta":"Good"}\n\n'
raise httpx.ReadError("connection reset")
def _silent_wav(seconds: int) -> bytes:
buffer: Final = io.BytesIO()
with wave.open(buffer, "wb") as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(16_000)
wav.writeframes(b"\x00\x00" * 16_000 * seconds)
return buffer.getvalue()
@pytest.mark.usefixtures("patched_transcription")
def test_a_streamed_transcription_that_fails_mid_transcript_bills_the_uploaded_seconds(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
) -> None:
upstream: Final = AsyncOpenAI(
api_key="sk-test",
base_url="https://upstream.example/v1",
http_client=httpx.AsyncClient(
transport=httpx.MockTransport(
lambda request: httpx.Response(
200, headers={"content-type": "text/event-stream"}, stream=_FirstDeltaThenReset()
)
)
),
)
async def route_to_the_sdk(
data: Mapping[str, object], route_type: str, llm_router: object, user_model: object
) -> object:
return litellm.atranscription(model="openai/gpt-transcribe", file=data["file"], stream=True, client=upstream)
four_second_upload: Final = _silent_wav(4)
async def stream_form(request: object) -> FormData:
return FormData(
[
("file", UploadFile(filename="sample.wav", file=io.BytesIO(four_second_upload))),
("model", "gpt-transcribe"),
("stream", "true"),
]
)
monkeypatch.setattr(proxy_server, "get_form_data", stream_form)
monkeypatch.setattr(proxy_server, "route_request", route_to_the_sdk)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", four_second_upload, "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert response.status_code == 200
assert '"error"' in response.text
failure_request: Final = proxy_server.proxy_logging_obj.post_call_failure_hook.await_args.kwargs["request_data"]
billed_cost: Final = _failure_fields_to_lift(failure_request)["response_cost"]
assert isinstance(billed_cost, float)
assert math.isclose(billed_cost, 4 * litellm.model_cost["gpt-transcribe"]["input_cost_per_second"])
async def _scribe_stream_form(request: object) -> FormData:
return FormData(
[
("file", UploadFile(filename="sample.wav", file=io.BytesIO(b"audio"))),
("model", "scribe"),
("stream", "true"),
]
)
@pytest.mark.usefixtures("patched_transcription")
def test_transcription_stream_form_field_reaches_other_providers_as_sent(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
upstream: Final = respx_mock.post("https://elevenlabs.example/v1/speech-to-text").mock(
return_value=httpx.Response(200, json={"text": "hello world", "language_code": "en"})
)
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{
"model_name": "scribe",
"litellm_params": {
"model": "elevenlabs/scribe_v1",
"api_key": "sk-test",
"api_base": "https://elevenlabs.example",
},
}
]
),
)
monkeypatch.setattr(proxy_server, "route_request", route_request)
monkeypatch.setattr(proxy_server, "get_form_data", _scribe_stream_form)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "scribe", "stream": "true"},
)
assert response.status_code == 200
assert response.json()["text"] == "hello world"
assert b'name="stream"\r\n\r\ntrue\r\n' in upstream.calls.last.request.content
def _streams_chat_and_transcription(request: httpx.Request) -> httpx.Response:
sse_body: Final = (
b'data: {"id":"chat-1","object":"chat.completion.chunk","created":0,"model":"gpt-4o",'
b'"choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
if request.url.path.endswith("/chat/completions")
else b'data: {"type":"transcript.text.done","text":"hello world"}\n\n'
)
return httpx.Response(
200,
content=sse_body,
headers={"content-type": "text/event-stream", "x-ratelimit-remaining-requests": "99"},
)
def _litellm_header_names(response: httpx.Response) -> frozenset[str]:
return frozenset(
name
for name in response.headers
if name.startswith("x-litellm-") and not name.startswith("x-litellm-response-cost")
)
def test_streamed_transcription_carries_the_headers_a_streamed_chat_completion_carries(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
respx_mock.route(host="stream-upstream.example").mock(side_effect=_streams_chat_and_transcription)
upstream: Final = {"api_key": "sk-test", "api_base": "https://stream-upstream.example/v1"}
monkeypatch.setattr(
proxy_server,
"llm_router",
litellm.Router(
model_list=[
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o", **upstream}},
{"model_name": "gpt-transcribe", "litellm_params": {"model": "openai/gpt-transcribe", **upstream}},
]
),
)
with auth_as():
chat: Final = client.post(
"/v1/chat/completions",
json={"model": "gpt-4o", "stream": True, "messages": [{"role": "user", "content": "hi"}]},
)
transcription: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
assert chat.status_code == 200
assert transcription.status_code == 200
assert transcription.headers["content-type"].startswith("text/event-stream")
assert _litellm_header_names(chat) - _litellm_header_names(transcription) <= {"x-litellm-overhead-duration-ms"}
assert transcription.headers["x-litellm-model-name"] == "openai/gpt-transcribe"
assert transcription.headers["llm_provider-x-ratelimit-remaining-requests"] == "99"
assert [name for name in transcription.headers if name.startswith("x-litellm-response-cost")] == []
class _FailsAfterTwoDeltas(httpx.AsyncByteStream):
async def __aiter__(self) -> AsyncIterator[bytes]:
yield b'data: {"type":"transcript.text.delta","delta":"hello "}\n\n'
yield b'data: {"type":"transcript.text.delta","delta":"world"}\n\n'
raise httpx.ReadError("upstream connection dropped at /srv/app/stt_stream.py via stt.internal.corp")
@pytest.mark.usefixtures("patched_transcription_stream")
def test_streamed_transcription_error_mid_stream_ends_with_an_error_event_and_runs_the_failure_hook(
client: TestClient,
auth_as: Callable[[], AbstractContextManager[None]],
monkeypatch: pytest.MonkeyPatch,
) -> None:
upstream: Final = AsyncOpenAI(
api_key="sk-test",
base_url="https://upstream.example/v1",
http_client=httpx.AsyncClient(
transport=httpx.MockTransport(
lambda request: httpx.Response(
200, stream=_FailsAfterTwoDeltas(), headers={"content-type": "text/event-stream"}
)
)
),
)
async def route_to_the_sdk(
data: Mapping[str, object], route_type: str, llm_router: object, user_model: object
) -> object:
return litellm.atranscription(model="openai/gpt-transcribe", file=data["file"], stream=True, client=upstream)
failure_hook: Final = AsyncMock(return_value=None)
monkeypatch.setattr(proxy_server.proxy_logging_obj, "post_call_failure_hook", failure_hook)
monkeypatch.setattr(proxy_server, "route_request", route_to_the_sdk)
with auth_as():
response: Final = client.post(
"/v1/audio/transcriptions",
files={"file": ("sample.wav", b"audio", "audio/wav")},
data={"model": "gpt-transcribe", "stream": "true"},
)
data_lines: Final = tuple(
line.removeprefix("data: ") for line in response.text.splitlines() if line.startswith("data: ")
)
frames: Final = tuple(json.loads(line) for line in data_lines[:-1])
assert response.status_code == 200
assert tuple(frame.get("delta") for frame in frames[:2]) == ("hello ", "world")
assert len(frames) == 3
assert data_lines[-1] == "[DONE]"
assert "upstream connection dropped" in frames[2]["error"]["message"]
assert "/srv/app/stt_stream.py" not in frames[2]["error"]["message"]
assert "stt.internal.corp" not in frames[2]["error"]["message"]
assert set(frames[2]["error"]) == {"message", "type", "param", "code"}
assert (frames[2]["error"]["type"], frames[2]["error"]["param"], frames[2]["error"]["code"]) == (
"internal_server_error",
None,
"500",
)
failure_hook.assert_awaited_once()
assert isinstance(failure_hook.await_args.kwargs["original_exception"], litellm.APIConnectionError)
def _post_speech(client: TestClient) -> httpx.Response:
return client.post("/v1/audio/speech", json={"model": "tts-1", "input": "Hi", "voice": "alloy"})
def _post_transcription(client: TestClient) -> httpx.Response:
return client.post(
"/v1/audio/transcriptions",
files={"file": ("audio.mp3", b"\x00\x01\x02", "audio/mpeg")},
data={"model": "whisper-1"},
)
@pytest.mark.parametrize(
("send", "call_type"),
[(_post_speech, "aspeech"), (_post_transcription, "atranscription")],
)
def test_a_provider_rejection_logs_the_audio_endpoint_call_type(
client: TestClient,
auth_as: Callable[..., contextlib.AbstractContextManager[object]],
failed_spend_row: Callable[[], tuple[str, str]],
send: Callable[[TestClient], httpx.Response],
call_type: str,
) -> None:
with auth_as():
response: Final = send(client)
assert response.status_code == 400, response.text
assert failed_spend_row() == ("failure", call_type)