litellm/tests/integration/compatibility/test_audio_transcriptions_wire.py
Mateo Wang b39eadcb82
feat(openai)!: honor per-deployment workload identity on non-chat routes (#39966)
* feat(anthropic): workload identity federation and pluggable identity sources

Backend half of #38818 (internal copy of the fork PR #38013), rebuilt as one
commit on top of litellm_internal_staging without the dashboard changes.

Deployments on anthropic/ without a static api_key can exchange an OIDC
workload assertion for a short-lived sk-ant-oat01 token through a shared
RFC 7523 JWT-bearer engine. The assertion comes from a mounted token file,
an env token, a LiteLLM-signed issuer, or Keycloak, chosen per deployment,
per named credential, or through ANTHROPIC_IDENTITY_SOURCE. The federation
fields are server-owned: refused inline in request bodies and on
POST /model/new, proxy-admin only on credentials, and the token exchange
is pinned to api.anthropic.com unless LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS
adds a host. GET /credentials/{name}/jwks exports the public key set of a
LiteLLM-signed credential for the Claude Console.

The OpenAI federation trio from #39613 rides along on the backend side with
the same server-owned handling.

Fixes #28607
Resolves LIT-6107

Co-authored-by: derhornspieler <15236687+derhornspieler@users.noreply.github.com>

* fix(anthropic): let batch-result downloads mint from deployment params and accept host:port allowlist entries

The files handler enabled workload identity on batch-result downloads but never received the
deployment's litellm_params, so a deployment authenticating through a named credential could only
mint from process-wide env vars. It now threads litellm_params through to the auth header the way
the batch retrieve path already does.

LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS entries written as host:port were read by urlsplit as a scheme,
so the allowlist kept the raw entry while the exchange compared bare hostnames and refused the
gateway. Entries are now parsed as network locations whether or not they carry a scheme.

* fix(types): move the WIF kwargs key sets to a leaf module so the kwargs funnel imports without a cycle

* test(anthropic): pin case-insensitive matching of WIF exchange-host allowlist entries

* fix(anthropic): end workload identity federation errors without a period so the router suffix reads cleanly

* fix(proxy): decrypt stored litellm_params before the WIF write gate

* fix(proxy): hide WIF secret references from /health output

* fix(proxy): keep the proxy error shape on credential endpoint refusals

* fix(proxy): hide identity token file paths from /health output

* feat(openai): honor per-deployment workload identity on non-chat routes

Image generation, audio speech and transcription, moderation, text
completions, files, batches, and fine-tuning built their OpenAI client from
the env vars alone, so a keyless deployment carrying
openai_identity_provider_id, openai_service_account_id, and
openai_identity_token_file failed with the api_key error on every route
other than chat, Responses, and embeddings. Those handlers now build their
client through the shared workload identity resolver, and the managed batch
file-access credential extraction keeps the trio

* fix(openai): cache workload identity clients and let deployment identity outrank process-wide keys

Non-chat handlers built a fresh SDK client per call, so files, batches,
fine-tuning, text completion and sync moderation exchanged the OIDC token on
every request. The builders now reuse the cached workload identity client per
identity, base, timeout, retries and organization; static-key clients are
still built fresh.

A process-wide OPENAI_API_KEY (or litellm.api_key / litellm.openai_key) no
longer silently overrides a deployment's openai_identity_* params. An explicit
distinct api_key on the deployment or call still wins.

* fix(openai): keep shared workload identity clients on the process http session

The workload identity client is cached and shared across requests, so it must
never borrow the transport a single caller passed in. Static-key clients keep
using the caller's http client; federated clients always ride the process-wide
session. Also makes the http client helpers on BaseOpenAILLM public since the
builder now needs them from outside the class.

* test(openai): pin partial deployment identity beating the process key

* test(openai): type the managed-object invoke callback

* fix(anthropic): rename the federation workspace param so Bedrock's anthropic_workspace_id keeps working

The Bedrock Claude Platform route already reads anthropic_workspace_id from
optional_params, so banning that spelling as a server-owned federation
parameter broke a pre-existing client capability. The federation field is now
anthropic_federation_workspace_id (env ANTHROPIC_FEDERATION_WORKSPACE_ID),
which restores the base branch's behavior for Bedrock callers, drops the
Bedrock-specific hint from the refusal message, and deletes the unconditional
ban constant that no longer had a reader

* fix(auth): share one exchanged token across workers reading the same assertion

Anthropic accepts each identity assertion exactly once, so two uvicorn
workers reading the same token file both minting from it means the second
exchange is denied with jti_reused. Minted tokens now land in a per-user
0700 cache directory guarded by a file lock, so workers on the same host
reuse one exchange until the token expires or the assertion rotates. A 401
is only retried when the re-read assertion actually differs, and the denial
hint explains jti_reused. LITELLM_TOKEN_EXCHANGE_CACHE_DIR moves the cache
and an empty value disables it

* feat(openai): honor deployment workload identity on image edits, vector stores, containers, videos, and evals

The httpx-based OpenAI handlers built their Authorization header from the
deployment api_key or OPENAI_API_KEY alone, so a keyless deployment carrying
openai_identity_provider_id, openai_service_account_id, and
openai_identity_token_file sent "Bearer None". They now resolve the bearer
through resolve_openai_bearer_token, which exchanges the deployment's
identity token the same way the SDK-based routes do.

A deployment that carries only part of the identity trio now raises a 500
naming the missing field instead of silently falling back to the process
key, and the SDK client builders take a factory for the static-key http
client so the federated branch never touches litellm.client_session.

* fix: keep anthropic federation from being shadowed or leaked

An empty or whitespace-only ANTHROPIC_API_KEY counted as set, so a federated
deployment sent an empty x-api-key on every call instead of minting a token.
Blank values now read as unset, and a real static key on a federated deployment
logs once that it outranks federation and nothing is being federated.

The exchange-host allowlist matched hostnames only, so a second process on
another port of an allowed host was trusted with the workload's identity token.
An entry that names a port now trusts that port alone, while a bare host still
trusts every port.

The shared token store exists so the workers reading one projected token file do
not each spend its single-use jti. A source that mints its own assertion per
exchange shares nothing with another worker, so it no longer writes a live token
to disk for a lookup that can never hit.

* feat(openai): resolve per-deployment workload identity for the assistants routes

OpenAIAssistantsAPI built its OpenAI and AsyncOpenAI clients from a locals() scrape, so a deployment carrying openai_identity_provider_id, openai_service_account_id and openai_identity_token_file never exchanged a token. Thread litellm_params through the class and build both clients with the shared workload identity builders.

* fix: unlink a staged token file a failed write leaves behind

The 401 denial hint now also says federation ignores ANTHROPIC_WORKSPACE_ID, which the Bedrock Claude platform provider already reads.

* refactor: move anthropic jwks derivation behind a provider-owned tagged union

* feat(openai): resolve per-deployment workload identity for image variations

Build the image variations client through the shared workload identity
builders instead of scraping locals(), and give the async path the async
http session it was missing. Let a federation trio serve the model-less
batch fallback, and hold the standard LRU size of exchanged tokens.

* fix: unlink the staged token file when its write fails at close

A buffered write only reaches the disk when the handle closes, so a full disk surfaces at close and left the staging file behind holding a usable token.

* fix(anthropic): close the staging descriptor before writing the shared token file

* fix(wif): judge federation writes by what they set, not what is stored

The admin gate read the stored deployment, so a team admin lost edit, delete
and Test Connection on any deployment carrying federation params. It now
returns early unless the submitted fields touch the federation surface, and a
Test Connection probe that points the deployment at its own api_base is still
refused, with the 403 no longer wrapped into a 500

The rest of the same review pass: POST /model/new refuses only a blocking
value of `blocked`, so a client that always sends `blocked: false` is not
turned away; a request body can no longer pick which federated identity to
mint as by naming a stored credential; an advisory refresh the executor
refuses disarms the entry instead of wedging the identity until the follower
timeout; the static-key shadow warning resolves its env fallback inside the
cache instead of once per request; credential writes drop nulls before
storing them; the token exchange validates the endpoint URL before reading an
assertion and keeps refusing redirects across a client heal; /health hides
every server-owned federation field from non-admins; and the async create_file
and create_batch paths say which setting is missing when the provider resolves
no URL

* style(tests): keep two blank lines before the batch_cost_is_final block

* test(batches): type the workload identity fallback test fixtures

* fix(moderation): stop validating unrelated kwargs on the sync workload identity path

* fix(auth): refuse server-owned workload identity keys in array form and inside a client router config

* fix(openai): keep a deployment's own key ahead of workload identity on every route

Thread the caller's own api_key into the identity resolver for embedding, transcription, moderation,
image generation, and assistants so a deployment key equal to the process key still wins. A partial
identity trio now falls back to a configured static key, unset max_retries keeps the SDK default, and
/v1/decisions plus OpenAI count_tokens resolve the exchanged bearer for identity-only deployments

* fix(containers): pass deployment params to container file auth headers

Container file routes built their auth headers without the deployment's
litellm_params, so a keyless deployment with an OpenAI workload identity
sent Bearer None on upload, list, retrieve, delete and content calls

* fix(openai): keep process-wide key precedence and move token exchange off the event loop

Restore the merge base order where an explicit or process-wide OpenAI key wins over
federation, so exporting OPENAI_API_KEY keeps working the way it did before.

Run the header-building validate_environment calls (image edit, videos, containers,
vector stores, vector store files) and the decisions and System One request
preparation in a worker thread, so a slow token exchange no longer stalls the loop.

Federate realtime websocket sessions, live sessions, realtime client secrets and
transcription sessions, and the OpenAI moderation guardrail when no key is set.

* fix(openai): answer 401 for a partial identity trio and federate realtime health checks

A deployment with part of the workload identity trio and no static key now
fails as an auth error naming the missing params, so the router does not retry
it as a server error. The realtime health check sends the exchanged bearer for
a keyless federated OpenAI deployment, and API token counting reads the same
static keys as every other route before minting.

* fix(openai): keep local token counting without an identity trio and federate the moderation guardrail from its own config

* fix(openai): skip a partial-trio moderation guardrail and let vault keys win in token counting

* test(integration): refuse caller-chosen identity params in user_config and bracketed form keys

* fix(openai): send the honored secret manager key from SDK client builders

A deployment whose OPENAI_API_KEY lives only in the secret manager now sends that key
on image, assistants, files, batches, and other SDK-built routes instead of failing with
no api_key, matching what the workload identity resolver already treats as a static key

* test(openai): check the token exchange runs off the event loop instead of timing the loop

---------

Co-authored-by: derhornspieler <15236687+derhornspieler@users.noreply.github.com>
2026-10-16 22:25:15 +00:00

870 lines
36 KiB
Python

import io
import json
import math
import mimetypes
import uuid
import wave
from email.message import Message
from email.parser import BytesParser
from email.policy import HTTP
from hashlib import sha256
from typing import Final, cast
import httpx
import pytest
from integration._support.client import JSON_OBJECT, Gateway, eventually, object_value
from integration._support.database import read_rows
from integration._support.wire import Reply, Request, wire_server
from openai import OpenAI
from pydantic import BaseModel, ConfigDict
_WAV_BYTES: Final = (
b"RIFF\x30\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00"
b"\x40\x1f\x00\x00\x40\x1f\x00\x00\x01\x00\x08\x00data\x08\x00\x00\x00"
b"\x00\x01\x02\x03\x04\x05\x06\x07"
)
_WAV_CONTENT_TYPE: Final = mimetypes.guess_type("a.wav")[0] or "application/octet-stream"
_VERBOSE_TEXT_PARTS: Final = (
("include[]", "logprobs"),
("language", "en"),
("model", "whisper-1"),
("prompt", "hi"),
("response_format", "verbose_json"),
("temperature", "0.2"),
("timestamp_granularities[]", "word"),
("timestamp_granularities[]", "segment"),
)
_VERBOSE_TRANSCRIPT: Final = {
"task": "transcribe",
"language": "english",
"duration": 1.5,
"text": "hello world",
"words": [
{"word": "hello", "start": 0.0, "end": 0.5},
{"word": "world", "start": 0.6, "end": 1.0},
],
"segments": [
{
"id": 0,
"seek": 0,
"start": 0.0,
"end": 1.0,
"text": "hello world",
"tokens": [1, 2],
"temperature": 0.2,
"avg_logprob": -0.1,
"compression_ratio": 1.0,
"no_speech_prob": 0.01,
}
],
"usage": {
"type": "tokens",
"input_tokens": 12,
"output_tokens": 4,
"total_tokens": 16,
"input_token_details": {"text_tokens": 2, "audio_tokens": 10},
},
}
_JSON_TRANSCRIPT: Final = {"text": "hello world", "usage": _VERBOSE_TRANSCRIPT["usage"]}
_PLAIN_TRANSCRIPTS: Final = {
"text": "hello world\n",
"srt": "1\n00:00:00,000 --> 00:00:01,000\nhello world\n",
"vtt": "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nhello world\n",
}
class _Word(BaseModel):
model_config = ConfigDict(extra="forbid")
word: str
start: float
end: float
class _Segment(BaseModel):
model_config = ConfigDict(extra="forbid")
id: int
seek: int
start: float
end: float
text: str
tokens: tuple[int, ...]
temperature: float
avg_logprob: float
compression_ratio: float
no_speech_prob: float
class _InputTokenDetails(BaseModel):
model_config = ConfigDict(extra="forbid")
text_tokens: int
audio_tokens: int
class _Usage(BaseModel):
model_config = ConfigDict(extra="forbid")
type: str
input_tokens: int
output_tokens: int
total_tokens: int
input_token_details: _InputTokenDetails
class _VerboseTranscript(BaseModel):
model_config = ConfigDict(extra="forbid")
task: str
language: str
duration: float
text: str
words: tuple[_Word, ...]
segments: tuple[_Segment, ...]
usage: _Usage
def _multipart_parts(request: Request) -> tuple[Message, ...]:
envelope: Final = f"content-type: {request.headers['content-type']}\r\n\r\n".encode() + request.body
parsed: Final = BytesParser(policy=HTTP).parsebytes(envelope)
assert parsed.is_multipart(), request.headers["content-type"]
return tuple(parsed.iter_parts())
def _text_parts(parts: tuple[Message, ...]) -> tuple[tuple[str, str], ...]:
return tuple(
sorted(
(
(part.get_param("name", header="content-disposition"), part.get_payload(decode=True).decode())
for part in parts
if part.get_filename() is None
),
key=lambda field: field[0],
)
)
def _file_parts(parts: tuple[Message, ...]) -> tuple[tuple[str, str, str, bytes], ...]:
return tuple(
sorted(
(
(
part.get_param("name", header="content-disposition"),
part.get_filename(),
part.get_content_type(),
part.get_payload(decode=True),
)
for part in parts
if part.get_filename() is not None
),
key=lambda field: field[0],
)
)
def _sdk(gateway: Gateway, path: str, api_key: str) -> OpenAI:
return OpenAI(
base_url=str(gateway.client.base_url).rstrip("/") + path,
api_key=api_key,
max_retries=0,
http_client=httpx.Client(trust_env=False),
)
def test_audio_transcription_verbose_json_forwards_every_form_field_and_returns_the_transcript(
gateway: Gateway,
) -> None:
call_id: Final = f"audio-transcription-{uuid.uuid4().hex}"
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == _VERBOSE_TEXT_PARTS
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_VERBOSE_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1",
api_base=f"{wire.url}/v1",
api_key="synthetic-openai-key",
input_cost_per_token=0.000001,
input_cost_per_audio_token=0.000003,
output_cost_per_token=0.000002,
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
temperature=0.2,
language="en",
prompt="hi",
response_format="verbose_json",
timestamp_granularities=["word", "segment"],
include=["logprobs"],
extra_headers={"x-litellm-call-id": call_id},
)
assert response.status_code == 200, response.text
assert json.loads(response.content) == _VERBOSE_TRANSCRIPT, response.text
_VerboseTranscript.model_validate_json(response.content)
assert response.headers["x-litellm-call-id"] == call_id, response.text
usage: Final = cast("dict[str, object]", _VERBOSE_TRANSCRIPT["usage"])
token_details: Final = cast("dict[str, int]", usage["input_token_details"])
assert math.isclose(
float(response.headers["x-litellm-response-cost"]),
token_details["audio_tokens"] * 0.000003
+ token_details["text_tokens"] * 0.000001
+ cast(int, usage["output_tokens"]) * 0.000002,
rel_tol=1e-9,
), response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
def test_audio_transcription_reaches_upstream_identically_on_prefixed_and_unprefixed_routes(
gateway: Gateway,
) -> None:
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == _VERBOSE_TEXT_PARTS
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_VERBOSE_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
prefixed: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
unprefixed: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
key: Final = scenario.key(models=[prefixed, unprefixed])
def transcribe(base_path: str, model: str) -> dict[str, object]:
with _sdk(gateway, base_path, key) as sdk:
return json.loads(
sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
temperature=0.2,
language="en",
prompt="hi",
response_format="verbose_json",
timestamp_granularities=["word", "segment"],
include=["logprobs"],
).content
)
bodies: Final = tuple(
transcribe(base_path, model) for base_path, model in (("/v1", prefixed), ("", unprefixed))
)
assert bodies[0] == bodies[1] == _VERBOSE_TRANSCRIPT
assert [(request.method, request.target) for request in wire.drain()] == [
("POST", "/v1/audio/transcriptions"),
("POST", "/v1/audio/transcriptions"),
]
def test_audio_transcription_json_format_returns_text_and_usage(gateway: Gateway) -> None:
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", "json"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
response_format="json",
)
assert response.status_code == 200, response.text
assert json.loads(response.content) == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
def test_audio_transcription_forwards_nested_and_repeated_bracket_fields_verbatim(gateway: Gateway) -> None:
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (
("chunking_strategy[prefix_padding_ms]", "300"),
("chunking_strategy[silence_duration_ms]", "500"),
("chunking_strategy[threshold]", "0.5"),
("chunking_strategy[type]", "server_vad"),
("known_speaker_names[]", "alice"),
("known_speaker_names[]", "bob"),
("known_speaker_references[]", "data:audio/wav;base64,AAAA"),
("known_speaker_references[]", "data:audio/wav;base64,BBBB"),
("model", "gpt-4o-transcribe"),
("response_format", "json"),
)
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-4o-transcribe", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
known_speaker_names: Final = ("alice", "bob")
known_speaker_references: Final = (
"data:audio/wav;base64,AAAA",
"data:audio/wav;base64,BBBB",
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
response_format="json",
chunking_strategy={
"type": "server_vad",
"prefix_padding_ms": 300,
"silence_duration_ms": 500,
"threshold": 0.5,
},
known_speaker_names=known_speaker_names,
known_speaker_references=known_speaker_references,
)
assert response.status_code == 200, response.text
assert json.loads(response.content) == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
def test_audio_transcription_stream_returns_server_sent_events(gateway: Gateway) -> None:
pytest.skip(
"BUG: stream=true transcriptions reach the upstream but its SSE body comes back as application/json "
'{"text": "data: ..."}, so OpenAI SDK stream=True callers get no transcript events'
)
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "gpt-4o-transcribe"), ("stream", "true"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(
chunks=(
b'data: {"type":"transcript.text.delta","delta":"hello"}\n\n',
b'data: {"type":"transcript.text.delta","delta":" world"}\n\n',
b'data: {"type":"transcript.text.done","text":"hello world",'
b'"usage":{"type":"tokens","input_tokens":12,"output_tokens":4,"total_tokens":16}}\n\n',
),
content_type="text/event-stream",
)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/gpt-4o-transcribe", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
with sdk.audio.transcriptions.with_streaming_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
stream=True,
) as raw:
assert raw.headers["content-type"].startswith("text/event-stream")
events: Final = tuple(
event.to_dict()
for event in sdk.audio.transcriptions.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
stream=True,
)
)
assert events == (
{"type": "transcript.text.delta", "delta": "hello"},
{"type": "transcript.text.delta", "delta": " world"},
{
"type": "transcript.text.done",
"text": "hello world",
"usage": {
"type": "tokens",
"input_tokens": 12,
"output_tokens": 4,
"total_tokens": 16,
},
},
)
assert [(request.method, request.target) for request in wire.drain()] == [
("POST", "/v1/audio/transcriptions"),
("POST", "/v1/audio/transcriptions"),
]
def test_audio_transcription_applies_json_string_metadata_without_forwarding_it(gateway: Gateway) -> None:
tag: Final = f"audio-metadata-{uuid.uuid4().hex}"
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", "json"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{
"model": model,
"response_format": "json",
"metadata": json.dumps({"tags": [tag]}),
},
{"file": ("a.wav", wav, _WAV_CONTENT_TYPE)},
)
assert response.status_code == 200, response.text
assert response.json() == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
rows: Final = eventually(
lambda: read_rows(
'SELECT request_tags FROM "LiteLLM_SpendLogs" WHERE request_id = %s',
(response.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
request_tags: Final = rows[0]["request_tags"]
assert isinstance(request_tags, list), rows
assert tuple(
value for value in request_tags if isinstance(value, str) and not value.startswith("User-Agent: ")
) == (tag,), rows
@pytest.mark.parametrize("response_format", ("text", "srt", "vtt"))
def test_audio_transcription_forwards_plain_response_format_to_upstream(gateway: Gateway, response_format: str) -> None:
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", response_format))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=_PLAIN_TRANSCRIPTS[response_format].encode(), content_type="text/plain")
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
response_format=response_format,
)
assert response.status_code == 200, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
@pytest.mark.parametrize("response_format", ("text", "srt", "vtt"))
def test_audio_transcription_plain_response_format_returns_the_raw_transcript(
gateway: Gateway, response_format: str
) -> None:
pytest.skip(
"BUG: response_format=text/srt/vtt transcriptions return application/json "
'{"text": ...} instead of the raw transcript, so the OpenAI SDK returns a JSON string as the transcript'
)
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
return Reply(body=_PLAIN_TRANSCRIPTS[response_format].encode(), content_type="text/plain")
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
response_format=response_format,
)
assert response.headers["content-type"].startswith("text/plain"), response.text
transcript: Final = sdk.audio.transcriptions.create(
model=model,
file=("a.wav", wav, _WAV_CONTENT_TYPE),
response_format=response_format,
)
assert transcript == _PLAIN_TRANSCRIPTS[response_format]
def test_audio_transcription_applies_json_string_litellm_metadata_without_forwarding_it(
gateway: Gateway,
) -> None:
tag: Final = f"audio-metadata-{uuid.uuid4().hex}"
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
assert request.headers["authorization"] == "Bearer synthetic-openai-key"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", "json"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{
"model": model,
"response_format": "json",
"litellm_metadata": json.dumps({"tags": [tag]}),
},
{"file": ("a.wav", wav, _WAV_CONTENT_TYPE)},
)
assert response.status_code == 200, response.text
assert response.json() == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
rows: Final = eventually(
lambda: read_rows(
'SELECT request_tags FROM "LiteLLM_SpendLogs" WHERE request_id = %s',
(response.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
request_tags: Final = rows[0]["request_tags"]
assert isinstance(request_tags, list), rows
assert tuple(
value for value in request_tags if isinstance(value, str) and not value.startswith("User-Agent: ")
) == (tag,), rows
@pytest.mark.parametrize("metadata_field", ("metadata", "litellm_metadata"))
def test_audio_transcription_consumes_bracketed_form_metadata_tags(gateway: Gateway, metadata_field: str) -> None:
pytest.skip(
"BUG: multipart metadata[tags]/litellm_metadata[tags] on /v1/audio/transcriptions reaches the upstream "
"as an extra text part and the tag never lands in LiteLLM_SpendLogs.request_tags"
)
tag: Final = f"audio-metadata-{uuid.uuid4().hex}"
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", "json"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{
"model": model,
"response_format": "json",
f"{metadata_field}[tags]": tag,
},
{"file": ("a.wav", wav, _WAV_CONTENT_TYPE)},
)
assert response.status_code == 200, response.text
assert response.json() == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
rows: Final = eventually(
lambda: read_rows(
'SELECT request_tags FROM "LiteLLM_SpendLogs" WHERE request_id = %s',
(response.headers["x-litellm-call-id"],),
),
lambda values: len(values) == 1,
seconds=70,
)
request_tags: Final = rows[0]["request_tags"]
assert isinstance(request_tags, list), rows
assert tuple(
value for value in request_tags if isinstance(value, str) and not value.startswith("User-Agent: ")
) == (tag,), rows
@pytest.mark.parametrize(
"identity_param", ("openai_identity_provider_id", "openai_service_account_id", "openai_identity_token_file")
)
def test_audio_transcription_refuses_a_bracketed_identity_form_key_before_the_upstream(
gateway: Gateway, identity_param: str
) -> None:
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{"model": model, "response_format": "json", f"{identity_param}[]": "caller-chosen-" + uuid.uuid4().hex},
{"file": ("a.wav", wav, _WAV_CONTENT_TYPE)},
)
assert response.status_code == 401, response.text
assert identity_param in response.text, response.text
assert wire.drain() == ()
def test_audio_transcription_preserves_the_client_file_content_type(gateway: Gateway) -> None:
pytest.skip(
"BUG: the proxy discards the client's file part content type and re-derives it from the filename, "
"so file=('recording', ..., 'audio/wav') reaches the upstream as application/octet-stream"
)
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _file_parts(parts) == (("file", "recording", "audio/wav", wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{"model": model, "response_format": "json"},
{"file": ("recording", wav, "audio/wav")},
)
assert response.status_code == 200, response.text
assert response.json() == _JSON_TRANSCRIPT, response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
def test_audio_transcription_bills_audio_tokens_at_the_input_rate_when_no_audio_rate_is_set(
gateway: Gateway,
) -> None:
pytest.skip(
"BUG: with only input_cost_per_token set, transcription audio tokens are billed at 0, so "
"x-litellm-response-cost is 1e-05 instead of 2e-05 for 10 audio, 2 text and 4 output tokens"
)
wav: Final = _WAV_BYTES + uuid.uuid4().bytes
def respond(request: Request) -> Reply:
assert request.method == "POST"
assert request.target == "/v1/audio/transcriptions"
parts: Final = _multipart_parts(request)
assert _text_parts(parts) == (("model", "whisper-1"), ("response_format", "json"))
assert _file_parts(parts) == (("file", "a.wav", _WAV_CONTENT_TYPE, wav),)
return Reply(body=json.dumps(_JSON_TRANSCRIPT).encode())
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="openai/whisper-1",
api_base=f"{wire.url}/v1",
api_key="synthetic-openai-key",
input_cost_per_token=0.000001,
output_cost_per_token=0.000002,
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{"model": model, "response_format": "json"},
{"file": ("a.wav", wav, _WAV_CONTENT_TYPE)},
)
assert response.status_code == 200, response.text
assert response.json() == _JSON_TRANSCRIPT, response.text
usage: Final = cast("dict[str, object]", _JSON_TRANSCRIPT["usage"])
token_details: Final = cast("dict[str, int]", usage["input_token_details"])
assert math.isclose(
float(response.headers["x-litellm-response-cost"]),
token_details["audio_tokens"] * 0.000001
+ token_details["text_tokens"] * 0.000001
+ cast(int, usage["output_tokens"]) * 0.000002,
rel_tol=1e-9,
), response.text
assert [(request.method, request.target) for request in wire.drain()] == [("POST", "/v1/audio/transcriptions")]
_GPT_TRANSCRIBE_VERBOSE_JSON_REFUSAL: Final = {
"error": {
"message": "response_format 'verbose_json' is not compatible with model 'gpt-transcribe'",
"type": "invalid_request_error",
"param": "response_format",
"code": "unsupported_value",
}
}
_UNDECODABLE_M4A_BYTES: Final = b"\x00\x00\x00\x20ftypM4A \x00\x00\x02\x00M4A isommp42\x00\x00\x00\x08free"
_DEPLOYMENT_COST_PER_SECOND: Final = 0.0003
def _openai_gpt_transcribe_reply(request: Request, reported_seconds: float) -> Reply:
fields: Final = dict(_text_parts(_multipart_parts(request)))
if fields.get("response_format") == "verbose_json":
return Reply(status=400, body=json.dumps(_GPT_TRANSCRIBE_VERBOSE_JSON_REFUSAL).encode())
return Reply(
body=json.dumps({"text": "hello world", "usage": {"type": "duration", "seconds": reported_seconds}}).encode()
)
def _uploaded_files(request: Request) -> tuple[tuple[str, str, bytes], ...]:
return tuple((name, filename, payload) for name, filename, _, payload in _file_parts(_multipart_parts(request)))
def _silent_wav(seconds: float) -> bytes:
output: Final = io.BytesIO()
with wave.open(output, "wb") as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(16000)
wav.writeframes(b"\x00\x00" * round(16000 * seconds))
return output.getvalue()
def _advertised_cost_per_second(gateway: Gateway, model: str) -> float:
entries: Final = gateway.get("/v2/model/info", {"model": model})["data"]
assert isinstance(entries, list) and len(entries) == 1, entries
price: Final = object_value(object_value(entries[0])["model_info"])["input_cost_per_second"]
assert isinstance(price, float) and price > 0, entries
return price
def _response_cost(response: httpx.Response) -> float:
return float(response.headers["x-litellm-response-cost"]) if "x-litellm-response-cost" in response.headers else 0.0
def _logged_spend(call_id: str) -> float:
rows: Final = eventually(
lambda: read_rows('SELECT spend FROM "LiteLLM_SpendLogs" WHERE request_id = %s', (call_id,)),
lambda values: len(values) == 1,
seconds=70,
)
return float(str(rows[0]["spend"]))
def test_gpt_transcribe_without_response_format_reaches_openai_without_verbose_json(gateway: Gateway) -> None:
upload: Final = _UNDECODABLE_M4A_BYTES + uuid.uuid4().bytes
with (
wire_server(lambda request: _openai_gpt_transcribe_reply(request, 2)) as wire,
gateway.scenario() as scenario,
):
model: Final = scenario.model(
model="openai/gpt-transcribe", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
with _sdk(gateway, "/v1", gateway.key) as sdk:
response: Final = sdk.audio.transcriptions.with_raw_response.create(
model=model, file=("a.m4a", upload, "audio/mp4")
)
requests: Final = wire.drain()
assert response.status_code == 200, response.text
assert JSON_OBJECT.validate_json(response.content)["text"] == "hello world", response.text
assert [(request.method, request.target) for request in requests] == [("POST", "/v1/audio/transcriptions")]
assert _text_parts(_multipart_parts(requests[0])) == (("model", "gpt-transcribe"),)
assert _uploaded_files(requests[0]) == (("file", "a.m4a", upload),)
@pytest.mark.parametrize(
"configured_cost_per_second",
(None, _DEPLOYMENT_COST_PER_SECOND),
ids=("cost_map_price", "deployment_price"),
)
def test_gpt_transcribe_unmeasurable_upload_bills_the_seconds_openai_reports(
gateway: Gateway, configured_cost_per_second: float | None
) -> None:
reported_seconds: Final = 2.0
upload: Final = _UNDECODABLE_M4A_BYTES + uuid.uuid4().bytes
with (
wire_server(lambda request: _openai_gpt_transcribe_reply(request, reported_seconds)) as wire,
gateway.scenario() as scenario,
):
model: Final = (
scenario.model(
model="openai/gpt-transcribe",
api_base=f"{wire.url}/v1",
api_key="synthetic-openai-key",
input_cost_per_second=configured_cost_per_second,
)
if configured_cost_per_second is not None
else scenario.model(
model="openai/gpt-transcribe", api_base=f"{wire.url}/v1", api_key="synthetic-openai-key"
)
)
key: Final = scenario.key(models=[model])
cost_per_second: Final = (
configured_cost_per_second
if configured_cost_per_second is not None
else _advertised_cost_per_second(gateway, model)
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{"model": model, "response_format": "json"},
{"file": ("a.m4a", upload, "audio/mp4")},
key=key,
)
expected_cost: Final = reported_seconds * cost_per_second
assert response.status_code == 200, response.text
assert JSON_OBJECT.validate_json(response.content)["text"] == "hello world", response.text
assert math.isclose(_response_cost(response), expected_cost), (_response_cost(response), expected_cost)
assert math.isclose(_logged_spend(response.headers["x-litellm-call-id"]), expected_cost)
key_rows: Final = eventually(
lambda: read_rows(
'SELECT spend FROM "LiteLLM_VerificationToken" WHERE token = %s',
(sha256(key.encode()).hexdigest(),),
),
lambda rows: len(rows) == 1 and float(str(rows[0]["spend"])) > 0,
seconds=70,
)
assert math.isclose(float(str(key_rows[0]["spend"])), expected_cost), key_rows
requests: Final = wire.drain()
assert [(request.method, request.target) for request in requests] == [("POST", "/v1/audio/transcriptions")]
assert _uploaded_files(requests[0]) == (("file", "a.m4a", upload),)
def test_gpt_transcribe_measured_upload_bills_the_measured_length_over_reported_seconds(gateway: Gateway) -> None:
measured_seconds: Final = 1.0
upload: Final = _silent_wav(measured_seconds)
with (
wire_server(lambda request: _openai_gpt_transcribe_reply(request, 7)) as wire,
gateway.scenario() as scenario,
):
model: Final = scenario.model(
model="openai/gpt-transcribe",
api_base=f"{wire.url}/v1",
api_key="synthetic-openai-key",
input_cost_per_second=_DEPLOYMENT_COST_PER_SECOND,
)
response: Final = gateway.request_multipart(
"/v1/audio/transcriptions",
{"model": model, "response_format": "json"},
{"file": ("a.wav", upload, _WAV_CONTENT_TYPE)},
)
expected_cost: Final = measured_seconds * _DEPLOYMENT_COST_PER_SECOND
assert response.status_code == 200, response.text
assert math.isclose(_response_cost(response), expected_cost), (_response_cost(response), expected_cost)
assert math.isclose(_logged_spend(response.headers["x-litellm-call-id"]), expected_cost)
requests: Final = wire.drain()
assert [(request.method, request.target) for request in requests] == [("POST", "/v1/audio/transcriptions")]
assert _uploaded_files(requests[0]) == (("file", "a.wav", upload),)