litellm/tests/integration/providers/test_audio_speech_stream_wire.py
Gabriel Creti 60d05972ae
feat(tts)!: stream audio from the provider as it arrives on /v1/audio/speech (#46222)
* feat(tts): stream audio from the provider as it arrives on /v1/audio/speech

The proxy read the whole audio body from the provider before sending the
first byte to the client, so time-to-first-byte was the full synthesis
time. Pass stream=True to get the bytes forwarded as they arrive.

The response type does not change: it is still HttpxBinaryResponseContent,
only unread, so every existing caller, logging path and spend hook works
the same. Providers opt in with supports_streaming on their config, the
others ignore the flag and buffer as before.

* fix(tts): keep streamed speech inside its concurrency and cleanup guarantees

Streaming the audio out moved two things that used to be covered by the
buffered response, and the review on #46222 caught both.

A deployment's max_parallel_requests counted syntheses that had started
rather than syntheses in flight, because aspeech now returns at the
response headers with the audio still arriving. The slot is held until
the response is closed, the way the chat streaming path already does it.

A client that walks away mid-clip leaves the body unread, and httpx only
releases the connection when it is read to the end. The body iterator
closes it on the way out, and the route arms the same close as a
background task for a response whose body is never read at all. These
are two different paths on purpose: Starlette raises ClientDisconnect
out of the response before background tasks run, so neither one covers
both cases.

Also pulls the speech body cast into one helper beside
speech_request_body, which drops a banned typing.cast import.

* test(tts): cover the slot-holding helper directly

router_code_coverage.py wants every function in router.py called by name
from a unit test, and _hold_slot_until_closed was only reached through
router.aspeech(). Test it on its own instead of adding it to the ignore
list: which responses keep their slot is the part worth pinning, so the
cases are buffered body, non-binary response and unread body.

* fix(tts): stream ElevenLabs speech from its /stream endpoint

ElevenLabs' convert endpoint synthesizes the whole clip before it sends the
first byte, so stream: true still waited for full synthesis. Requests with
stream: true now call /v1/text-to-speech/{voice_id}/stream.

* fix(tts): log streamed speech like buffered speech

A deployment-hook sync copied stream=True onto the logging object for
speech, so the success handler waited for an assembled chat stream that
never comes and streamed speech never reached spend logs or callbacks.

* fix(tts): forward streamed speech audio as it arrives

* fix(tts): log streamed speech when the audio ends

Streamed speech now logs success when the caller reads the audio to the end or closes it, and logs a failure when the upstream breaks mid-clip. Key max_parallel_requests slots stay held for the whole stream, the logged duration and bytes match what was sent, and the router slot is no longer pinned by a pending logging task.

* fix(tts): bill and map streamed speech failures, defer logging only when streaming

A streamed clip that breaks mid-way is now logged as a failure at the clip's cost, since speech is priced per input character and the provider bills the whole request. Transport errors map to litellm.Timeout or litellm.APIConnectionError, and the proxy runs post_call_failure_hook with the clip's cost. Success logging waits for the stream end only when the caller asked for stream=True.

* fix(tts): stream only on a literal true, mark hang-ups, and cut off without a fake DB outage

* fix(tts): put the 499 hang-up marker on the spend row

* fix(tts): keep the caller's metadata and positional base_url intact, add speech stream integration cells

* fix(speech): keep stream keyword-only and buffer the speech health check

Passing stream positionally skipped the logging wrapper, and a deployment
with stream: true left the health check's provider stream open. Also gives
each scripted chat reply its own id so the non-boolean stream cases do not
collide on the spend log key, and adds the audit's round-2 cells

* test(speech): check the health check calls the buffered path

* test(integration): read the speech chaos spend rows from the worker database

Main now gives each owned proxy a private database unless the test file opts
into the worker's state. The chaos tests read billed rows from the worker
database, so they opt in with the shares_worker_state marker like the other
providers chaos files

* fix(tts): release the router slot when a streamed read fails

The deployment slot was only released through Response.aclose, which httpx
does not call when the body read raises. A provider cutting a clip off
mid-read left the slot held, so later Router.aspeech calls got 429.

Release the slot on any stream end, and close the upstream stream when a
read fails.

Co-Authored-By: Claude <noreply@anthropic.com>

* test(tts): drive speech stream tests with events instead of sleeps

Co-Authored-By: Claude <noreply@anthropic.com>

* test(utils): capture streamed sync speech usage when the audio ends, not at the headers

* fix(proxy): close streamed speech when the route fails before responding

A response headers hook, or any other step between aspeech returning an unread
audio stream and the route returning its StreamingResponse, could raise and leave
that stream open. The upstream connection and the deployment's
max_parallel_requests slot then stayed held, so later clips got 429s. The route
now closes the audio on any error before it returns

* fix(proxy): end streamed speech as failed when the route errors before responding

Closing the unread audio on a pre-response error went through aclose, which always
ends as StreamClosedEarly: the clip was success-logged with a 499 ClientDisconnected
marker while the route returned 500. The route now ends the audio with the error,
through every OnEndAsyncByteStream wrapper, so it is logged as a failure. A
cancellation still closes it as a hang-up.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(proxy): close unread speech audio when a deployment guardrail rejects it

A guardrail's post-call deployment hook runs before the audio stream's end logger is attached, so a rejection left the unread provider response open and held its connection. The failure path of the async wrapper now closes it, and a close that fails never replaces the rejection

Also annotates the new test locals in test_routes_audio.py with Final

* fix(proxy): free the key's parallel slot before a streamed speech clip ends

A streamed clip released its key's max_parallel_requests slot only from the
success logging task, which defers the Redis write until after the caller has
seen the end of the audio. A next clip sent to another instance at that moment
could still be counted against the key and get 429. The speech route now
releases the slot when the audio finishes, before the response ends, the same
way chat streams do.

* fix(speech): log a failed streamed clip once per failure hook

A logger with both failure hooks got both for one failed aspeech stream. The
stream's failure path now marks its sync run as part of an async request, as the
non-stream path does. Also type the route test helpers and check that the speech
health check leaves no provider connection open.

* test(speech): expect one async failure event from a cut-off aspeech stream

* fix(proxy): shield the speech upstream close and tag cut-off rows as aspeech

A hang-up while the provider is silent no longer lets the anyio cancel abort the upstream close
on servers that cancel the stream on disconnect. Mid-clip cut-off failure rows now store the
aspeech call type instead of an empty one.

* refactor(logging): record a speech client disconnect through the logging object

* test(speech): hold the provider mid-clip in the guardrail-rejection wire test

The test sent the whole short clip at once on a keep-alive connection, so when the clip reached the HTTP client before the guardrail rejected it, closing the response handed the finished connection back to the pool and the test timed out waiting for a close. The peer now holds after the first audio chunk until the rejection is raised, as a real provider still sending a long clip would

* fix(speech): close the unread audio when a sync speech call fails after its headers

The async wrapper already closed it. The sync wrapper left the provider
connection open until garbage collection when post-call processing raised.

* fix(router): keep hung-up speech clips out of latency samples

A streamed clip the caller hangs up on ends early, so its short duration made
the deployment look fast to latency-based routing. The clip still counts as a
request.

* test(proxy): type the speech request helper in the audio route tests

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-11-17 15:03:12 +00:00

742 lines
29 KiB
Python

import asyncio
import base64
import json
import threading
import uuid
from collections.abc import AsyncIterator, Callable, Iterator, Mapping
from dataclasses import dataclass
from itertools import accumulate
from types import MappingProxyType
from typing import Final
from urllib.parse import urlsplit
import httpx
import openai
import psutil
import pytest
from integration._support.client import (
JSON_OBJECT,
Gateway,
Scenario,
eventually,
object_value,
string_value,
)
from integration._support.database import read_rows
from integration._support.wire import Reply, Request, Wire, wire_server
from pydantic import JsonValue
_PROVIDER_KEY: Final = "synthetic-tts-key"
_FIRST: Final = b"audio-frame-0:" + bytes(range(64))
_REST: Final = (
b"audio-frame-1:" + bytes(range(64, 192)),
b"audio-frame-2:" + bytes(range(192, 256)),
)
_CLIP: Final = _FIRST + b"".join(_REST)
_FIRST_BYTE_SECONDS: Final = 20
_SPEECH: Final = "/v1/audio/speech"
_SPEND_SQL: Final = (
'SELECT status, call_type, model_group, spend, metadata FROM "LiteLLM_SpendLogs" WHERE litellm_call_id = %s'
)
_ELEVENLABS_VOICE: Final = "21m00Tcm4TlvDq8ikWAM"
_AZURE_DEPLOYMENT: Final = "gpt-4o-mini-tts"
@dataclass(frozen=True, slots=True)
class Provider:
model: str
parameters: Mapping[str, JsonValue]
streamed_path: str
buffered_path: str
PROVIDERS: Final = MappingProxyType(
{
"openai": Provider(
"openai/gpt-4o-mini-tts",
{"api_key": _PROVIDER_KEY},
"/audio/speech",
"/audio/speech",
),
"azure": Provider(
f"azure/{_AZURE_DEPLOYMENT}",
{"api_key": _PROVIDER_KEY, "api_version": "2025-04-01-preview"},
f"/openai/deployments/{_AZURE_DEPLOYMENT}/audio/speech",
f"/openai/deployments/{_AZURE_DEPLOYMENT}/audio/speech",
),
"elevenlabs": Provider(
"elevenlabs/eleven_v3",
{"api_key": _PROVIDER_KEY},
f"/v1/text-to-speech/{_ELEVENLABS_VOICE}/stream",
f"/v1/text-to-speech/{_ELEVENLABS_VOICE}",
),
"aws_polly": Provider(
"aws_polly/neural",
{
"aws_access_key_id": "AKIASCRIPTEDPROVIDER",
"aws_secret_access_key": "scripted-secret",
"aws_region_name": "us-east-1",
},
"/v1/speech",
"/v1/speech",
),
"azure_speech": Provider(
"azure/speech/azure-tts",
{"api_key": _PROVIDER_KEY},
"/cognitiveservices/v1",
"/cognitiveservices/v1",
),
}
)
@dataclass(frozen=True, slots=True)
class Heard:
status: int
content_type: str
body: bytes
def _text() -> str:
return f"streamed speech {uuid.uuid4().hex}"
def _speech(model: str, text: str, **extra: JsonValue) -> dict[str, JsonValue]:
return {"model": model, "input": text, "voice": "alloy", **extra}
def _audio_peer(release: threading.Event | None = None, abort_after: int | None = None) -> Callable[[Request], Reply]:
def respond(request: Request) -> Reply:
return Reply(
content_type="audio/mpeg",
chunks=(_FIRST, *_REST),
gate_after_first=release,
abort_after=abort_after,
)
return respond
def _deployment(scenario: Scenario, wire: Wire, provider: Provider, **extra: JsonValue) -> str:
return scenario.model(
model=provider.model,
api_base=wire.url,
model_info={"mode": "audio_speech"},
**provider.parameters,
**extra,
)
def _headers(gateway: Gateway, call_id: str, key: str | None = None) -> dict[str, str]:
return {
"Authorization": f"Bearer {gateway.key if key is None else key}",
"x-litellm-call-id": call_id,
}
def _first_bytes(chunks: Iterator[bytes], size: int) -> bytes:
return next(heard for heard in accumulate(chunks) if len(heard) >= size)
async def _afirst_bytes(chunks: AsyncIterator[bytes], size: int, heard: bytes = b"") -> bytes:
if len(heard) >= size:
return heard
return await _afirst_bytes(chunks, size, heard + await anext(chunks))
def _listen(
gateway: Gateway,
body: Mapping[str, JsonValue],
call_id: str,
release: threading.Event,
) -> Heard:
with gateway.client.stream(
"POST",
_SPEECH,
json=dict(body),
headers=_headers(gateway, call_id),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as response:
chunks: Final = response.iter_raw()
first: Final = _first_bytes(chunks, len(_FIRST))
release.set()
rest: Final = b"".join(chunks)
return Heard(response.status_code, response.headers.get("content-type", ""), first + rest)
def _spend_row(call_id: str) -> dict[str, JsonValue]:
rows: Final = eventually(
lambda: read_rows(_SPEND_SQL, (call_id,)),
lambda found: len(found) >= 1,
seconds=70,
)
assert len(rows) == 1, rows
return rows[0]
def _buffered_spend(gateway: Gateway, model: str, text: str) -> float:
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request("POST", _SPEECH, _speech(model, text), headers={"x-litellm-call-id": call_id})
assert response.status_code == 200, response.text[:400]
spend: Final = _spend_row(call_id)["spend"]
assert isinstance(spend, float) and spend > 0, spend
return spend
def _assert_billed(gateway: Gateway, model: str, call_id: str, text: str) -> dict[str, JsonValue]:
row: Final = _spend_row(call_id)
assert (row["status"], row["call_type"], row["model_group"]) == ("success", "aspeech", model), row
assert row["spend"] == pytest.approx(_buffered_spend(gateway, model, text)), row
return object_value(row["metadata"])
def _failure_spend(model: str, call_id: str) -> JsonValue:
row: Final = _spend_row(call_id)
assert (row["status"], row["call_type"], row["model_group"]) == ("failure", "aspeech", model), row
return row["spend"]
def _only_request(wire: Wire) -> Request:
requests: Final = wire.drain()
assert len(requests) == 1, [request.target for request in requests]
return requests[0]
def _assert_reached(wire: Wire, path: str, text: str) -> Request:
request: Final = _only_request(wire)
assert (request.method, urlsplit(request.target).path) == ("POST", path), request.target
assert text.encode() in request.body, request.body[:400]
return request
def _held_upstream_connections(wire: Wire) -> int:
port: Final = urlsplit(wire.url).port
return sum(
1
for connection in psutil.Process().net_connections(kind="tcp")
if connection.status == psutil.CONN_ESTABLISHED and connection.laddr and connection.laddr.port == port
)
@pytest.mark.parametrize("provider", tuple(PROVIDERS))
def test_streamed_speech_reaches_the_caller_before_the_clip_ends_and_bills_it_once(
gateway: Gateway, provider: str
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS[provider])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
heard: Final = _listen(gateway, _speech(model, text, stream=True), call_id, release)
assert (heard.status, heard.body) == (200, _CLIP), heard
assert heard.content_type.startswith("audio/"), heard.content_type
_assert_reached(wire, PROVIDERS[provider].streamed_path, text)
_assert_billed(gateway, model, call_id, text)
def test_openai_sdk_sync_client_hears_streamed_speech_before_the_clip_ends(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
client: Final = openai.OpenAI(
base_url=f"{gateway.client.base_url}/v1",
api_key=gateway.key,
max_retries=0,
http_client=httpx.Client(trust_env=False, timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS)),
)
with client.audio.speech.with_streaming_response.create(
model=model,
input=text,
voice="alloy",
extra_body={"stream": True},
extra_headers={"x-litellm-call-id": call_id},
) as response:
chunks: Final = response.iter_bytes()
first: Final = _first_bytes(chunks, len(_FIRST))
release.set()
assert first + b"".join(chunks) == _CLIP
client.close()
_assert_reached(wire, PROVIDERS["openai"].streamed_path, text)
_assert_billed(gateway, model, call_id, text)
async def _async_sdk_clip(gateway: Gateway, model: str, text: str, call_id: str, release: threading.Event) -> bytes:
client: Final = openai.AsyncOpenAI(
base_url=f"{gateway.client.base_url}/v1",
api_key=gateway.key,
max_retries=0,
http_client=httpx.AsyncClient(trust_env=False, timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS)),
)
try:
async with client.audio.speech.with_streaming_response.create(
model=model,
input=text,
voice="alloy",
extra_body={"stream": True},
extra_headers={"x-litellm-call-id": call_id},
) as response:
chunks: Final = response.iter_bytes()
first: Final = await _afirst_bytes(chunks, len(_FIRST))
release.set()
return first + b"".join([chunk async for chunk in chunks])
finally:
await client.close()
def test_openai_sdk_async_client_hears_streamed_speech_before_the_clip_ends(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
assert asyncio.run(_async_sdk_clip(gateway, model, text, call_id, release)) == _CLIP
_assert_reached(wire, PROVIDERS["openai"].streamed_path, text)
_assert_billed(gateway, model, call_id, text)
@pytest.mark.parametrize("stream", [None, False], ids=["omitted", "false"])
@pytest.mark.parametrize("provider", ["openai", "elevenlabs"])
def test_buffered_speech_is_unchanged(gateway: Gateway, provider: str, stream: bool | None) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS[provider])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
extra: Final[dict[str, JsonValue]] = {} if stream is None else {"stream": stream}
response: Final = gateway.request(
"POST",
_SPEECH,
_speech(model, text, **extra),
headers={"x-litellm-call-id": call_id},
)
assert (response.status_code, response.content) == (200, _CLIP), response.text[:400]
_assert_reached(wire, PROVIDERS[provider].buffered_path, text)
_assert_billed(gateway, model, call_id, text)
def test_a_deployment_level_stream_true_streams_speech_for_a_caller_that_omits_stream(gateway: Gateway) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["elevenlabs"], stream=True)
text: Final = _text()
call_id: Final = str(uuid.uuid4())
heard: Final = _listen(gateway, _speech(model, text), call_id, release)
assert (heard.status, heard.body) == (200, _CLIP), heard
_assert_reached(wire, PROVIDERS["elevenlabs"].streamed_path, text)
_assert_billed(gateway, model, call_id, text)
def test_a_caller_stream_false_overrides_a_deployment_level_stream_true(gateway: Gateway) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["elevenlabs"], stream=True)
buffered_twin: Final = _deployment(scenario, wire, PROVIDERS["elevenlabs"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request(
"POST",
_SPEECH,
_speech(model, text, stream=False),
headers={"x-litellm-call-id": call_id},
)
assert (response.status_code, response.content) == (200, _CLIP), response.text[:400]
_assert_reached(wire, PROVIDERS["elevenlabs"].buffered_path, text)
row: Final = _spend_row(call_id)
assert (row["status"], row["call_type"], row["model_group"]) == ("success", "aspeech", model), row
assert row["spend"] == pytest.approx(_buffered_spend(gateway, buffered_twin, text)), row
@pytest.mark.parametrize("provider", ["openai", "elevenlabs"])
def test_a_health_check_on_a_deployment_level_streamed_speech_model_reports_it_healthy(
gateway: Gateway, provider: str
) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS[provider], stream=True)
response: Final = gateway.request("GET", f"/health?model={model}", None)
assert response.status_code == 200, response.text[:400]
report: Final = object_value(response.json())
assert (report["healthy_count"], report["unhealthy_count"]) == (1, 0), report
assert [(request.method, urlsplit(request.target).path) for request in wire.drain()] == [
("POST", PROVIDERS[provider].buffered_path)
]
def _long_audio(request: Request) -> Reply:
return Reply(content_type="audio/mpeg", chunks=(bytes(512 * 1024),) * 128)
@pytest.mark.parametrize("provider", ["openai", "elevenlabs"])
def test_a_health_check_on_a_deployment_level_streamed_speech_model_leaves_no_provider_stream_open(
gateway: Gateway, provider: str
) -> None:
with wire_server(_long_audio) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS[provider], stream=True)
response: Final = gateway.request("GET", f"/health?model={model}", None)
assert response.status_code == 200, response.text[:400]
report: Final = object_value(response.json())
assert (report["healthy_count"], report["unhealthy_count"]) == (1, 0), report
eventually(lambda: _held_upstream_connections(wire), lambda held: held == 0)
def _mistral_peer(request: Request) -> Reply:
return Reply(body=json.dumps({"audio_data": base64.b64encode(_CLIP).decode()}).encode())
def test_stream_true_on_a_provider_without_audio_streaming_answers_buffered_and_bills_it(
gateway: Gateway,
) -> None:
with wire_server(_mistral_peer) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="mistral/voxtral-mini-tts-latest",
api_base=wire.url,
api_key=_PROVIDER_KEY,
model_info={"mode": "audio_speech"},
)
text: Final = _text()
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request(
"POST",
_SPEECH,
_speech(model, text, stream=True),
headers={"x-litellm-call-id": call_id},
)
assert (response.status_code, response.content) == (200, _CLIP), response.text[:400]
_assert_reached(wire, "/v1/audio/speech", text)
_assert_billed(gateway, model, call_id, text)
@pytest.mark.parametrize(
"stream",
["true", 1, None, [], {}, "s" * 5120],
ids=["string-true", "int-1", "null", "empty-list", "empty-object", "5kb-string"],
)
def test_a_stream_value_other_than_literal_true_answers_buffered(gateway: Gateway, stream: JsonValue) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["elevenlabs"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request(
"POST",
_SPEECH,
_speech(model, text, stream=stream),
headers={"x-litellm-call-id": call_id},
)
assert (response.status_code, response.content) == (200, _CLIP), response.text[:400]
request: Final = _assert_reached(wire, PROVIDERS["elevenlabs"].buffered_path, text)
assert "stream" not in object_value(json.loads(request.body)), request.body[:400]
_assert_billed(gateway, model, call_id, text)
def test_a_duplicated_stream_true_key_streams(gateway: Gateway) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["elevenlabs"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
body: Final = json.dumps(_speech(model, text, stream=True))[:-1] + ', "stream": true}'
response: Final = gateway.client.post(
_SPEECH,
content=body.encode(),
headers={**_headers(gateway, call_id), "content-type": "application/json"},
)
assert (response.status_code, response.content) == (200, _CLIP), response.text[:400]
_assert_reached(wire, PROVIDERS["elevenlabs"].streamed_path, text)
_assert_billed(gateway, model, call_id, text)
def test_a_hang_up_mid_clip_closes_the_provider_stream_and_bills_the_clip_with_a_499_marker(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, text, stream=True),
headers=_headers(gateway, call_id),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as response:
assert response.status_code == 200
assert _first_bytes(response.iter_raw(), len(_FIRST)) == _FIRST
eventually(lambda: _held_upstream_connections(wire), lambda held: held == 0)
release.set()
_assert_reached(wire, PROVIDERS["openai"].streamed_path, text)
metadata: Final = _assert_billed(gateway, model, call_id, text)
assert object_value(metadata["error_information"])["error_code"] == "499", metadata
def test_a_provider_cut_off_mid_clip_cuts_the_caller_off_and_bills_a_failure_at_full_cost(
gateway: Gateway,
) -> None:
with (
wire_server(_audio_peer(abort_after=1)) as wire,
wire_server(_audio_peer()) as healthy,
gateway.scenario() as scenario,
):
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
twin: Final = _deployment(scenario, healthy, PROVIDERS["openai"])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, text, stream=True),
headers=_headers(gateway, call_id),
) as response:
assert response.status_code == 200
chunks: Final = response.iter_raw()
assert _first_bytes(chunks, len(_FIRST)) == _FIRST
with pytest.raises(httpx.RemoteProtocolError):
b"".join(chunks)
_assert_reached(wire, PROVIDERS["openai"].streamed_path, text)
assert _failure_spend(model, call_id) == pytest.approx(_buffered_spend(gateway, twin, text))
_REJECTIONS: Final = MappingProxyType(
{
"openai": json.dumps(
{
"error": {
"message": "Incorrect API key provided: synthetic",
"type": "invalid_request_error",
"code": "invalid_api_key",
}
}
),
"elevenlabs": json.dumps(
{
"detail": {
"status": "invalid_api_key",
"message": "Invalid API key: synthetic",
}
}
),
}
)
@pytest.mark.parametrize("provider", tuple(_REJECTIONS))
def test_a_provider_rejection_before_any_audio_reaches_the_caller_with_its_status_and_message(
gateway: Gateway, provider: str
) -> None:
def respond(request: Request) -> Reply:
return Reply(status=401, body=_REJECTIONS[provider].encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS[provider])
text: Final = _text()
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request(
"POST",
_SPEECH,
_speech(model, text, stream=True),
headers={"x-litellm-call-id": call_id},
)
assert response.status_code == 401, response.text[:400]
assert "API key" in string_value(object_value(JSON_OBJECT.validate_json(response.content)["error"])["message"])
_assert_reached(wire, PROVIDERS[provider].streamed_path, text)
assert _failure_spend(model, call_id) == 0.0
def test_a_repeated_identical_streamed_request_reaches_the_provider_and_bills_each_call_once(
gateway: Gateway,
) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
text: Final = _text()
call_ids: Final = (str(uuid.uuid4()), str(uuid.uuid4()))
responses: Final = tuple(
gateway.request(
"POST",
_SPEECH,
_speech(model, text, stream=True),
headers={"x-litellm-call-id": call_id},
)
for call_id in call_ids
)
assert [(response.status_code, response.content) for response in responses] == [(200, _CLIP)] * 2
received: Final = wire.drain()
assert [urlsplit(request.target).path for request in received] == ["/audio/speech"] * 2
for call_id in call_ids:
_assert_billed(gateway, model, call_id, text)
def _admitted(gateway: Gateway, model: str, key: str) -> int:
return gateway.request("POST", _SPEECH, _speech(model, _text(), stream=True), key=key).status_code
def test_a_key_max_parallel_requests_slot_is_held_until_the_streamed_clip_ends(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
key: Final = scenario.key(max_parallel_requests=1)
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, _text(), stream=True),
headers=_headers(gateway, str(uuid.uuid4()), key),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as held:
chunks: Final = held.iter_raw()
first: Final = _first_bytes(chunks, len(_FIRST))
blocked: Final = gateway.request("POST", _SPEECH, _speech(model, _text(), stream=True), key=key)
release.set()
assert first + b"".join(chunks) == _CLIP
assert blocked.status_code == 429, blocked.text[:400]
eventually(lambda: _admitted(gateway, model, key), lambda status: status == 200)
assert len(wire.drain()) == 2
def test_a_key_max_parallel_requests_slot_is_freed_after_each_completed_clip(
gateway: Gateway,
) -> None:
with wire_server(_audio_peer()) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
key: Final = scenario.key(max_parallel_requests=1)
assert [_admitted(gateway, model, key) for _ in range(3)] == [200, 200, 200]
assert len(wire.drain()) == 3
def test_a_key_max_parallel_requests_slot_is_freed_by_a_hang_up(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"])
key: Final = scenario.key(max_parallel_requests=1)
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, _text(), stream=True),
headers=_headers(gateway, str(uuid.uuid4()), key),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as held:
assert _first_bytes(held.iter_raw(), len(_FIRST)) == _FIRST
eventually(lambda: _held_upstream_connections(wire), lambda count: count == 0)
release.set()
eventually(lambda: _admitted(gateway, model, key), lambda status: status == 200)
assert len(wire.drain()) == 2
def test_a_deployment_max_parallel_requests_slot_is_held_until_the_streamed_clip_ends(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"], max_parallel_requests=1)
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, _text(), stream=True),
headers=_headers(gateway, str(uuid.uuid4())),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as held:
chunks: Final = held.iter_raw()
first: Final = _first_bytes(chunks, len(_FIRST))
blocked: Final = gateway.request("POST", _SPEECH, _speech(model, _text(), stream=True))
release.set()
assert first + b"".join(chunks) == _CLIP
assert blocked.status_code == 429, blocked.text[:400]
eventually(lambda: _admitted(gateway, model, gateway.key), lambda status: status == 200)
assert len(wire.drain()) == 2
def test_a_deployment_max_parallel_requests_slot_is_freed_by_a_hang_up(
gateway: Gateway,
) -> None:
release: Final = threading.Event()
with wire_server(_audio_peer(release)) as wire, gateway.scenario() as scenario:
model: Final = _deployment(scenario, wire, PROVIDERS["openai"], max_parallel_requests=1)
with gateway.client.stream(
"POST",
_SPEECH,
json=_speech(model, _text(), stream=True),
headers=_headers(gateway, str(uuid.uuid4())),
timeout=httpx.Timeout(60, read=_FIRST_BYTE_SECONDS),
) as held:
assert _first_bytes(held.iter_raw(), len(_FIRST)) == _FIRST
eventually(lambda: _held_upstream_connections(wire), lambda count: count == 0)
release.set()
eventually(lambda: _admitted(gateway, model, gateway.key), lambda status: status == 200)
assert len(wire.drain()) == 2
_CHAT_USAGE: Final = {"prompt_tokens": 5, "completion_tokens": 1, "total_tokens": 6}
def _chat_chunk(
chat_id: str, delta: Mapping[str, JsonValue], finish_reason: str | None, **extra: JsonValue
) -> bytes:
chunk: Final = {
"id": chat_id,
"object": "chat.completion.chunk",
"created": 1,
"model": "gpt-4o-mini",
"choices": [{"index": 0, "delta": dict(delta), "finish_reason": finish_reason}],
**extra,
}
return f"data: {json.dumps(chunk)}\n\n".encode()
def _chat_peer(request: Request) -> Reply:
chat_id: Final = f"chatcmpl-{uuid.uuid4().hex}"
if object_value(JSON_OBJECT.validate_json(request.body)).get("stream"):
return Reply(
content_type="text/event-stream",
chunks=(
_chat_chunk(chat_id, {"role": "assistant", "content": "hello"}, None),
_chat_chunk(chat_id, {}, "stop", usage=_CHAT_USAGE),
b"data: [DONE]\n\n",
),
)
completion: Final = {
"id": chat_id,
"object": "chat.completion",
"created": 1,
"model": "gpt-4o-mini",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "hello"},
"finish_reason": "stop",
}
],
"usage": _CHAT_USAGE,
}
return Reply(body=json.dumps(completion).encode())
@pytest.mark.parametrize("stream", ["true", 1], ids=["string-true", "int-1"])
def test_a_chat_completion_with_a_non_boolean_stream_value_answers_and_lands_one_success_row(
gateway: Gateway, stream: JsonValue
) -> None:
with wire_server(_chat_peer) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(api_base=f"{wire.url}/v1")
call_id: Final = str(uuid.uuid4())
response: Final = gateway.request(
"POST",
"/v1/chat/completions",
{
"model": model,
"messages": [{"role": "user", "content": f"say hello {call_id}"}],
"stream": stream,
},
headers={"x-litellm-call-id": call_id},
)
assert response.status_code == 200, response.text[:400]
assert "hello" in response.text, response.text[:400]
row: Final = _spend_row(call_id)
assert (row["status"], row["call_type"], row["model_group"]) == (
"success",
"acompletion",
model,
), row
assert isinstance(row["spend"], float) and row["spend"] > 0, row