litellm/tests/e2e/llm_translation/test_audio_speech_e2e.py
yuneng-jiang 7d50a31eb5
test(e2e): move live-provider legacy tests into tests/e2e (#44120)
* test(e2e): move live-provider legacy tests into tests/e2e

Port legacy tests that exercise real providers into the tests/e2e suites that own them, using the harness (/model/new plus deferred cleanup) and asserting on what the caller receives. Delete legacy tests already covered at equal or stronger strength by e2e, integration or unit tests, and drop the now empty ocr_testing CircleCI job

* test(e2e): address review on the live-provider test move

Assert the SSE error frame a client actually receives when a post_call guardrail blocks a stream, and require a tool call for every requested city before checking the answer. Restore the OCR matrix and its CircleCI job, the Claude Agent SDK streaming test, and test_async_create_batch, since their SDK-level and callback assertions have no equivalent in tests/e2e

* test(e2e): accept both guardrail block shapes on a blocked stream

A post_call block before the first chunk reaches the client as HTTP 400 with either a JSON error body or a single SSE error frame, depending on whether the block surfaced as an exception or an error chunk. Assert the policy message is present and the blocked output is absent in both

* test(realtime): restore direct SDK realtime tests against OpenAI

The e2e realtime tests go through the proxy and the remaining SDK tests either mock the upstream or assert less, so keep the direct litellm._arealtime tests with and without intent, and TestOpenAIRealtime::test_realtime_connection, in place

* test: make realtime and Nova stream checks deterministic

The direct SDK realtime tests now fail on a refused connection instead of skipping. The with-intent test asserts OpenAI rejects the exact intent value sent, which only happens when the intent is forwarded. The Nova /v1/messages stream test asserts stream structure, stop reason and usage instead of model wording

* test(realtime): own intent forwarding with a unit test instead of a live rejection

Assert litellm._arealtime passes the intent query param into the OpenAI realtime websocket URL, which is the behavior LiteLLM owns, and drop the live test that depended on OpenAI's rejection wording
2026-10-02 00:02:18 -07:00

171 lines
7.4 KiB
Python

"""Live e2e: POST /v1/audio/speech returns audio, non-streamed and streamed.
Both positive calls go through the real OpenAI SDK (LIT-4577). The non-streamed
call asserts an audio (not JSON) body. The streamed call consumes the response
the way a player would and asserts customer-observable streaming: chunked
transfer encoding (a buffered body would carry a content-length) with non-zero
audio bytes. The malformed-body negatives stay on the shared transport because
the SDK refuses to send a request missing its required fields.
"""
from __future__ import annotations
import pytest
from e2e_config import unique_marker
from e2e_http import assert_client_error
from lifecycle import ResourceManager
from models import LiteLLMParamsBody
from proxy_client import ProxyClient
from pydantic import BaseModel
from sdk_clients import SdkClients, response_header
pytestmark = pytest.mark.e2e
class _OptionalSpeechBody(BaseModel):
model: str | None = None
input: str | None = None
voice: str | None = None
def _register_tts(proxy: ProxyClient, resources: ResourceManager) -> tuple[str, str]:
model = f"e2e-speech-{unique_marker()}"
model_id = proxy.create_model(
model,
LiteLLMParamsBody(model="openai/gpt-4o-mini-tts", api_key="os.environ/OPENAI_API_KEY"),
)
resources.defer(lambda: proxy.delete_model(model_id))
return model, resources.key()
class TestAudioSpeech:
@pytest.mark.covers("llm.audio_speech.openai.basic.nonstream.works")
def test_audio_speech_returns_audio(
self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
) -> None:
model, key = _register_tts(proxy, resources)
client = sdk.openai(key)
response = client.audio.speech.with_raw_response.create(
model=model, voice="alloy", input="Hello!"
)
content_type = response_header(response.headers, "content-type")
assert "audio" in (content_type or ""), (
f"/audio/speech content-type is not audio: {content_type!r}"
)
assert response.content, "/audio/speech returned an empty body"
@pytest.mark.covers("llm.audio_speech.openai.basic.stream.works")
def test_audio_speech_streams_audio_chunks(
self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
) -> None:
model, key = _register_tts(proxy, resources)
client = sdk.openai(key)
with client.audio.speech.with_streaming_response.create(
model=model,
voice="alloy",
input=(
"Streaming speech should arrive in several audio chunks so a client can "
"begin playback well before the whole clip has finished generating."
),
) as response:
content_type = response_header(response.headers, "content-type")
transfer_encoding = response_header(response.headers, "transfer-encoding")
content_length = response_header(response.headers, "content-length")
total_bytes = sum(len(chunk) for chunk in response.iter_bytes(chunk_size=8192))
assert "audio" in (content_type or ""), (
f"/audio/speech content-type is not audio: {content_type!r}"
)
assert "chunked" in (transfer_encoding or ""), (
f"/audio/speech did not stream: transfer-encoding={transfer_encoding!r}, "
f"content-length={content_length!r} (a buffered body is not a stream)"
)
assert content_length is None, (
f"/audio/speech advertised content-length={content_length!r} on a "
f"streamed response (a buffered body is not a stream)"
)
assert total_bytes > 0, "/audio/speech stream returned no audio bytes"
@pytest.mark.skip(reason="stage red: product gap, /v1/audio/speech 500s on missing input instead of 400")
@pytest.mark.covers("llm.audio_speech.openai.input_validation.nonstream.works")
def test_missing_input_returns_error(
self, proxy: ProxyClient, resources: ResourceManager
) -> None:
model, key = _register_tts(proxy, resources)
result = proxy.transport.send(
"/v1/audio/speech",
headers=proxy.transport.bearer(key),
json=_OptionalSpeechBody(model=model, voice="alloy"),
)
assert_client_error(result, "speech missing input")
@pytest.mark.skip(reason="stage red: product gap, /v1/audio/speech 500s on missing model instead of 400")
@pytest.mark.covers("llm.audio_speech.openai.input_validation.nonstream.works")
def test_missing_model_returns_error(
self, proxy: ProxyClient, resources: ResourceManager
) -> None:
_, key = _register_tts(proxy, resources)
result = proxy.transport.send(
"/v1/audio/speech",
headers=proxy.transport.bearer(key),
json=_OptionalSpeechBody(input="hello", voice="alloy"),
)
assert_client_error(result, "speech missing model")
@pytest.mark.skip(reason="stage red: product gap, /v1/audio/speech 500s on invalid voice instead of surfacing the provider 4xx")
@pytest.mark.covers("llm.audio_speech.openai.input_validation.nonstream.works")
def test_invalid_voice_returns_error(
self, proxy: ProxyClient, resources: ResourceManager
) -> None:
model, key = _register_tts(proxy, resources)
result = proxy.transport.send(
"/v1/audio/speech",
headers=proxy.transport.bearer(key),
json=_OptionalSpeechBody(model=model, input="hello", voice="invalid_voice_xyz"),
)
assert_client_error(result, "speech invalid voice")
@pytest.mark.skip(reason="stage red: product gap, /v1/audio/speech 500s on empty input instead of surfacing the provider 4xx")
@pytest.mark.covers("llm.audio_speech.openai.input_validation.nonstream.works")
def test_empty_input_returns_error(
self, proxy: ProxyClient, resources: ResourceManager
) -> None:
model, key = _register_tts(proxy, resources)
result = proxy.transport.send(
"/v1/audio/speech",
headers=proxy.transport.bearer(key),
json=_OptionalSpeechBody(model=model, input="", voice="alloy"),
)
assert_client_error(result, "speech empty input")
MP3_PREFIXES = (b"ID3", b"\xff\xfb", b"\xff\xf3", b"\xff\xf2")
class TestAwsPollySpeech:
def test_polly_generative_voice_returns_mp3(
self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
) -> None:
model = f"e2e-speech-polly-{unique_marker()}"
model_id = proxy.create_model(
model,
LiteLLMParamsBody(
model="aws_polly/generative",
aws_access_key_id="os.environ/AWS_ACCESS_KEY_ID",
aws_secret_access_key="os.environ/AWS_SECRET_ACCESS_KEY",
aws_region_name="os.environ/AWS_REGION",
),
)
resources.defer(lambda: proxy.delete_model(model_id))
client = sdk.openai(resources.key())
response = client.audio.speech.with_raw_response.create(
model=model, voice="alloy", input="Hello from the gateway.", response_format="mp3"
)
content_type = response_header(response.headers, "content-type")
assert "audio" in (content_type or ""), f"polly speech content-type is not audio: {content_type!r}"
assert response.content.startswith(MP3_PREFIXES), (
f"polly speech body is not MP3 audio: {response.content[:16]!r}"
)