Repository navigation
fix(gemini): return raw pcm16 when Gemini TTS answers WAV audio - #51145
Conversation
[Medium impact] The PR appears safe to merge, with no new actionable issues found.
|
|
bugbot run |
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Closed to free an open-PR slot. Branch kept, and it reopens for final CI and bot reviews once ready |
…v_to_pcm16 # Conflicts: # tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
…annot read Lyria music asked for as WAV is 48 kHz stereo, so stripping its header lost the rate and channels. Only a 24 kHz mono 16-bit WAV is unwrapped now, and audio that fails to decode or parse comes back unchanged instead of raising.
…n speech Pass through audio/wav base64 that is not strict base64, including non-ASCII and URL-safe input, unwrap WAV sent as a data URI text part, and return audio that is already a WAV container from /v1/audio/speech as is
…ers from a prefix A responseFormat.audio.mimeType request now returns Gemini's audio as is, audio/x-wav, audio/wave and audio/vnd.wave labels unwrap like audio/wav, and a WAV of another shape is no longer decoded in full.
… mime_type key A responseFormat.audio.mime_type, or a responseFormat sent through extra_body.generationConfig, now counts as an explicit ask the same as mimeType. A responseFormat whose audio names no mime type still returns raw PCM
…i receives An extra_body.generationConfig responseFormat, or response_format, replaces the top-level responseFormat on the request, so the response side now reads that same value before deciding to keep Gemini's audio as is
…i receives The keep-or-unwrap choice for Gemini TTS chat audio now reads generationConfig.responseFormat (or response_format) from the body sent to Gemini, so a dropped or overridden extra_body value no longer flips it, and a mime type given as its enum number counts as an ask
…, and keep the audio Google sent on batch, native and pass-through logs
… is null Gemini accepts a null generationConfig next to a snake_case generation_config and answers with the audio format the snake_case key names, so the keep-or-unwrap check now falls through to it.
Whether Gemini TTS audio comes back as raw PCM now depends on the audio mime type a request names under responseFormat, generationConfig, or generation_config. The response cache key left those out, so a cached PCM answer could serve a later AUDIO_WAV ask, and a cached WAV answer could serve a call that named no format. The cache key now includes them, top level and under extra_body. A float mime type such as 4.0 is matched like its enum number, and the native generateContent pricing copy keeps Google's audio, since it only reads usage.
|
bugbot run |
|
bugbot run |
…v_to_pcm16 # Conflicts: # litellm/llms/vertex_ai/files/transformation.py # litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py # tests/unit/caching/test_caching.py # tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_gemini_passthrough_logging_handler.py # tests/unit/proxy/pass_through_endpoints/llm_provider_handlers/test_vertex_passthrough_logging_handler.py
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit d875f7d. Configure here.
…ini route Picked from BerriAI#51145 commits 1ba64fb, 6d57373, efcc5d4, 1f5446e, d34fc9c, bd5452a, fd0ef57, c703997 and d875f7d, applied as one change since their diffs touch main-only context LTS adaptations: no thought_signature_in_tool_call_id or speech-config normalization here, the speech bridge takes no model argument, the cache key leaves out extra_body as LTS always has, streamed chat audio is not surfaced on LTS so the streamed tests are dropped, and the native logging test calls the LTS success-logging entry point
Gemini 3.8 Flash TTS sends back a WAV file where older voices sent raw audio, so chat
pcm16audio came back as WAV and/v1/audio/speechplayed a click before the voice. This strips that WAV wrapper, while Lyria music and WAV a caller asked for by name come back as beforeDetails
TLDR
Problem this solves:
audio/wav, older ones raw PCMformat: pcm16comes back as WAV bytes/v1/audio/speechputs a second WAV header around that WAVHow it solves it:
audio/wavholding 24 kHz mono 16-bit samples to raw PCMUser Flow
Before
"model": "gemini-3.8-flash-tts","modalities": ["audio"], and"audio": {"voice": "Kore", "format": "pcm16"}RIFFWAV header"response_format": "wav"returns a WAV with a secondRIFFheader inside, and"pcm"returns WAV bytesAfter
RIFFheader"pcm"returns raw PCMRelevant issues
Fixes #44541
Supersedes #44545 and #44548, which fix only
/v1/audio/speechDecisions
audio/wav, or its aliasesaudio/x-wav,audio/waveandaudio/vnd.wave, in aninlineDatapart or adata:audio/...;base64,text partRIFF....WAVE, as fix(speech): do not wrap Gemini TTS audio that is already a WAV #44548 doespcm16names, is unwrappedaudio/wavthat is not strict base64, does not parse as a WAV of that shape, or holds no samples passes through unchangedAUDIO_WAVrequest still returns WAVresponseFormatorresponse_format, undergenerationConfigor the snake_casegeneration_configGemini also takes, withmimeTypeormime_typeAUDIO_WAVor a nonzero enum number, integer or float, counts.0is Gemini's unspecified value, so it counts as no askgenerationConfigcounts as absent, so its snake_case twin is read. Gemini answers that body with the WAV it namesresponseFormat,generationConfigandgeneration_configfor every provider. Main keysextra_bodysince fix(caching): key the response cache on extra_body by default #53898generateContentand the Gemini and Vertex pass-through routes log Google's audio as isAUDIO_L16to get raw PCM at the sourceresponseFormat.audio.mimeType: AUDIO_L16on every call. 3.8 answers it withaudio/l16, but 2.5 rejects it, so this needs a per-model list/v1/audio/speechreturns chat audio that is already a WAV as is, and wraps only raw PCMaudio.format, or drops it underdrop_params, also gets raw PCMpcm16is the only chat audio format Gemini takes, per fix(gemini)!: answer 400 for an unsupported chat audio format, drop it under drop_params #49423's error messagepcm16has no container to hold it, and the speech route builds its WAV from the chat audiowavalready hid the chunk inside its own data chunk, where a C2PA reader does not look. Only the routes that broke thepcm16contract handed it back/v1/audio/speech, which needs the speech route to see the provider's container and makes its output differ per modelPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Each leg is a 2-worker proxy with its own Postgres, calling the real Gemini API and Vertex. Cache cases run two such proxies on one Redis
Last updated: d875f7d. QA ran on d875f7d, with the before leg on the merge base 25b010e
Proxy config
Each proxy started from its own tree, with
DATABASE_URLnaming its own Postgres databaseThe cache proxies add the
litellm_settingsblock under Cache and float cases, all on one Redis at 127.0.0.1:28061responseFormatWAVmime_typekey WAVextra_bodyWAVgenerationConfigresponse_formatWAVgeneration_configWAVgenerateContentBefore (25b010e)
Chat audio pcm16, gemini-3.8-flash-tts
After (d875f7d)
Chat audio pcm16, gemini-3.8-flash-tts
Other cases
Before, Streaming chat audio pcm16, gemini-3.8-flash-tts
After, Streaming chat audio pcm16, gemini-3.8-flash-tts
Before, Chat audio pcm16, vertex-gemini-3.8-flash-tts
After, Chat audio pcm16, vertex-gemini-3.8-flash-tts
Before, Streaming chat audio pcm16, vertex-gemini-3.8-flash-tts
After, Streaming chat audio pcm16, vertex-gemini-3.8-flash-tts
Before, Speech wav, gemini-3.8-flash-tts
After, Speech wav, gemini-3.8-flash-tts
Before, Speech pcm, gemini-3.8-flash-tts
After, Speech pcm, gemini-3.8-flash-tts
Before, Speech wav, vertex-gemini-3.8-flash-tts
After, Speech wav, vertex-gemini-3.8-flash-tts
Before, Chat audio pcm16, gemini-3.8-flash-lite-tts
After, Chat audio pcm16, gemini-3.8-flash-lite-tts
Before, Chat audio pcm16, gemini-2.5-flash-preview-tts
After, Chat audio pcm16, gemini-2.5-flash-preview-tts
Before, Speech wav, gemini-2.5-flash-preview-tts
After, Speech wav, gemini-2.5-flash-preview-tts
Before, Chat music WAV, lyria-3.5
After, Chat music WAV, lyria-3.5
Before, SDK litellm.speech, gemini-3.8-flash-tts, no response_format
After, SDK litellm.speech, gemini-3.8-flash-tts, no response_format
Before, Chat audio with responseFormat AUDIO_WAV, gemini-3.8-flash-tts
After, Chat audio with responseFormat AUDIO_WAV, gemini-3.8-flash-tts
Before, Chat audio asking for AUDIO_WAV with the mime_type key, gemini-3.8-flash-tts
After, Chat audio asking for AUDIO_WAV with the mime_type key, gemini-3.8-flash-tts
Before, Chat audio asking for AUDIO_WAV through extra_body.generationConfig, gemini-3.8-flash-tts
After, Chat audio asking for AUDIO_WAV through extra_body.generationConfig, gemini-3.8-flash-tts
Before, Chat audio with a top-level generationConfig, as the OpenAI SDK's extra_body sends it, gemini-3.8-flash-tts
After, Chat audio with a top-level generationConfig, as the OpenAI SDK's extra_body sends it, gemini-3.8-flash-tts
Before, Chat audio asking for AUDIO_WAV through extra_body.generationConfig.response_format, gemini-3.8-flash-tts
After, Chat audio asking for AUDIO_WAV through extra_body.generationConfig.response_format, gemini-3.8-flash-tts
Before, Chat audio naming WAV by its enum number 4, gemini-3.8-flash-tts
After, Chat audio naming WAV by its enum number 4, gemini-3.8-flash-tts
Before, Chat audio naming the unspecified enum number 0, gemini-3.8-flash-tts
After, Chat audio naming the unspecified enum number 0, gemini-3.8-flash-tts
Before, Chat audio asking for AUDIO_WAV through a snake_case extra_body.generation_config, gemini-3.8-flash-tts
After, Chat audio asking for AUDIO_WAV through a snake_case extra_body.generation_config, gemini-3.8-flash-tts
Before, Native generateContent asking for AUDIO_WAV, gemini-3.8-flash-tts
After, Native generateContent asking for AUDIO_WAV, gemini-3.8-flash-tts
Before, Gemini pass-through generateContent asking for AUDIO_WAV, gemini-3.8-flash-tts
After, Gemini pass-through generateContent asking for AUDIO_WAV, gemini-3.8-flash-tts
Before, The logged response the Admin UI Logs page shows for each call
The Logs page reads the
/spend/logs/ui/<request id>routeAfter, The logged response the Admin UI Logs page shows for each call
Cache and float cases
The same proxy config plus this block, with namespace
r12-baseon the base proxies. Proxy 1 and proxy 2 are two processes of the same treeMain has keyed the cache on a literal
extra_bodysince #53898, so in the first two cache cases the base proxies no longer share one cache entry between the two asks. The third case sends the WAV ask as a top-levelresponseFormat, which base leaves out of the cache key and this PR adds to itBefore (25b010e), Cache, no ask first on proxy 1, then the AUDIO_WAV ask on proxy 2, then the no ask again on proxy 1
After (d875f7d), Cache, no ask first on proxy 1, then the AUDIO_WAV ask on proxy 2, then the no ask again on proxy 1
Before (25b010e), Cache, the AUDIO_WAV ask first on proxy 2, then no ask on proxy 1
After (d875f7d), Cache, the AUDIO_WAV ask first on proxy 2, then no ask on proxy 1
Before (25b010e), Cache, no ask first on proxy 1, then the top-level responseFormat AUDIO_WAV ask on proxy 2
After (d875f7d), Cache, no ask first on proxy 1, then the top-level responseFormat AUDIO_WAV ask on proxy 2
Before (25b010e), Chat audio naming AUDIO_WAV as the float enum number 4.0, gemini-3.8-flash-tts
After (d875f7d), Chat audio naming AUDIO_WAV as the float enum number 4.0, gemini-3.8-flash-tts
/live-pr-risk (c703997)
No breaking change seen in 92 live cache, float and native cells on base, fd0ef57, c703997 and its merge with main. Its one Medium is the first Low caveat below. Full report
Carried to d875f7d. Changes since are a main merge and a refactor routing chat and pass-through responses through one helper with the same calls. Proof remains valid because the cache, float and native cells passed again live on this tip. Verification is 1730 focused unit tests and an 808-file affected run, 2 flaky reds aside
Type
🐛 Bug Fix
Caveats (if any)
High
responseFormatandextra_bodyLow
ttlnamespaceat upgrade clears itgenerationConfig,generation_configorresponseFormatmiss the cache once after the upgradepcm16generationConfig, as the OpenAI SDK'sextra_bodysends it, now gets PCM where it got WAVFinal Attestation
Bugbot summary