litellm/tests/unit/llms/gradium
Gabriel Creti 0946b7416c
feat(gradium): add text-to-speech support for /v1/audio/speech (#46427)
* feat(gradium): add text-to-speech support for /v1/audio/speech

Gradium (https://gradium.ai) is a voice AI provider whose TTS had no
LiteLLM route. Adds it as a first-class TTS provider behind the existing
BaseTextToSpeechConfig seam, so `litellm.speech(model="gradium/default")`
and the proxy's /v1/audio/speech both work.

The provider targets Gradium's REST TTS endpoint
(POST /api/post/speech/tts) rather than its Python SDK, so the call
flows through base_llm_http_handler and keeps retries, logging and cost
tracking that an SDK-internal HTTP client would bypass.

Notes on the mapping:
- `only_audio: true` is always sent. Without it Gradium answers a
  line-delimited JSON message stream instead of audio bytes, which
  /v1/audio/speech callers cannot read.
- The six canonical OpenAI voice names map to Gradium library voice IDs;
  anything else is forwarded verbatim, so raw Gradium voice IDs work.
- Gradium has no mp3/aac/flac encoder, so those response_formats raise
  UnsupportedParamsError instead of silently returning mislabeled audio.
  `litellm.drop_params` falls back to the default format.
- Pre-stream failures come back as HTTP 500 with a plain-text body, so
  the response transform raises rather than handing the caller an opaque
  binary payload.
- Cost is per character of input text (1 credit/char), priced from the
  published add-on credit rate.

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

* fix(gradium): satisfy CI gates and handle /api-suffixed api_base

- register the provider in provider_endpoints_support.json and claim
  tests/unit/llms/gradium in the llms-providers shard, so the provider
  documentation and CI/shard coverage gates see it
- rework the transformation to build its params and request body
  functionally rather than by mutation, so it clears the type-discipline
  rules; the suppressions that remain are the signatures
  BaseTextToSpeechConfig fixes and the dicts its return types require
- get_complete_url now tolerates an api_base that already ends in /api.
  Gradium documents its regional bases that way
  (https://eu.api.gradium.ai/api), and pasting one produced
  /api/api/post/speech/tts.

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

* test(gradium): cover the remaining branches, format under the pinned ruff

- ruff format with the pinned 0.15.3 (the suppression comments were long
  enough to push their lines over the limit and get them wrapped)
- cover get_supported_openai_params, get_error_class, the explicit
  api_base/api_key path, and both missing-voice paths, taking the
  provider to 100% patch coverage

The missing-voice tests record where each guard actually fires:
map_openai_params raises ValueError first (same as elevenlabs), so
speech()'s own BadRequestError is reachable only when the provider
config manager returns None.

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

* test(gradium): use the local_model_cost_map fixture for the cost test

The cost test wrote litellm.model_cost directly, which the test-quality
gate flags (TQ005): it is a process-wide global, and the leak is exactly
what the save/restore conftest exists to undo. The existing
local_model_cost_map fixture does the same setup with teardown, so the
suite no longer depends on LITELLM_LOCAL_MODEL_COST_MAP being exported.

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

* fix(gradium): clear the basedpyright and Add Model dropdown gates

- add a provider_create_fields.json entry so gradium appears in the
  proxy UI's Add Model dropdown; test_public_endpoints asserts every
  LlmProviders member is listed there
- parameterize the dict/list annotations copied from
  BaseTextToSpeechConfig and type gradium_models, which basedpyright
  counted as new Unknown-type errors against the merge-base ceiling
- suppress the untyped-argument errors on the shared TTS handler call;
  every provider branch of speech() carries the same ones
- keep extra_body support, matching elevenlabs and minimax, and cover it
  with a test

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

* refactor(gradium): take api_key and api_base from the model only

The API key and base are set by whoever creates the model, like any other
deployment credential. Drop the GRADIUM_API_KEY / GRADIUM_API_BASE
environment fallbacks, the litellm.api_key fallback (which could send an
unrelated provider's global key to Gradium) and the hardcoded default
base URL; a missing value now raises a clear error.

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

* feat(gradium): stream text-to-speech audio when stream is true

#46222 made streamed speech opt-in per provider through
supports_streaming. Gradium's REST TTS endpoint already sends audio as
it is synthesized, so the config opts in and speech() forwards
stream=True to the shared handler. Without stream the clip is still
read in full before it is returned.

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

* chore(gradium): regenerate provider endpoint support

provider_endpoints_support.json and its backup are now generated from
the cost map, so Gradium's hand-written entry was stale and missing
from the backup.

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

* fix(gradium): honor drop_params, reject speed and instructions, bill every character

Gradium counts every input character, spaces included, so the response reports billed_characters and the shared cost path no longer under-bills by the stripped whitespace

A deployment's drop_params now drops an unsupported response_format, speed or instructions. Without it those params answer 400, the litellm convention for a param the provider cannot honor

The Pro Voice Clone row is removed because Gradium has no such model name. The XS add-on rate is $7 per 100k credits, and the Add Model form now asks for the API base

* fix(gradium): accept native formats, map current OpenAI voices, answer 400 on a missing voice

Calls Gradium's documented /api/speech/tts path, passes ulaw_8000, alaw_8000 and the pcm_* rates through with audio content types, maps ash, ballad, coral, sage, verse, marin and cedar, prices gradium/gradium-tts-beta, and types the tests

* fix(gradium): map OpenAI voices to English voices, forward json_config, accept speed 1.0

Missing api_key now answers 401 and a missing api_base answers 500 without a traceback

* fix(gradium): ignore a non-object extra_body and keep a top-level json_config

A non-object extra_body no longer answers 500, and a null json_config in
extra_body no longer erases the top-level value. The cost map drops the advice
to override input_cost_per_character, which speech cost ignores today, and adds
gradium/gradium-tts-202602, Gradium's previous model, at the same rate. The
async speech test now checks the request body sent on both the streamed and
buffered paths.

* fix(gradium): read json_config from any mapping extra_body

A read-only mapping such as MappingProxyType passed as extra_body is now
read like a dict, as it was before the non-object check. The non-object
test also sends a top-level json_config so it fails if that check drops
every caller field

* fix(gradium): answer 502 for a 200 response that is not audio

Only a missing content type or application/octet-stream, which Gradium
sends for pcm, ulaw and alaw, is relabeled as audio. Any other 200, such
as JSON or HTML, now raises a 502 instead of reaching the caller labeled
audio/pcm and being billed. Tests also pin case-insensitive voice names,
trimmed voices and extra_body's json_config winning over the top level

* fix(gradium): release the upstream response when rejecting a non-audio reply

A streamed 200 that is not audio was raised as a 502 without closing the
response, so the sync and async speech paths could keep the connection.
Both paths now close it before raising

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-21 23:51:27 +00:00
..
text_to_speech feat(gradium): add text-to-speech support for /v1/audio/speech (#46427) 2026-11-21 23:51:27 +00:00
__init__.py feat(gradium): add text-to-speech support for /v1/audio/speech (#46427) 2026-11-21 23:51:27 +00:00