Skip to content

fix(gemini): return raw pcm16 when Gemini TTS answers WAV audio on lts/1.108.x - #54193

Merged
mateo-berri merged 6 commits into
BerriAI:lts/1.108.xfrom
quietjules:litellm_gemini_tts_wav_to_pcm16_lts
Nov 24, 2026
Merged

mateo-berri merged 6 commits into
BerriAI:lts/1.108.xfrom
quietjules:litellm_gemini_tts_wav_to_pcm16_lts

Conversation

@quietjules

@quietjules quietjules commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #51145 to lts/1.108.x. Gemini 3.8 Flash TTS sends back a WAV file where older voices sent raw audio, so chat pcm16 audio came back as WAV and /v1/audio/speech played a click before the voice. This strips that WAV wrapper on 1.108 too, while WAV a caller asked for by name comes back as before

Details

TLDR

Problem this solves:

  • Gemini 3.8 TTS models answer audio/wav, older ones raw PCM
  • Chat audio with format: pcm16 comes back as WAV bytes on 1.108
  • /v1/audio/speech puts a second WAV header around that WAV

How it solves it:

  • Unwraps 24 kHz mono 16-bit WAV to raw PCM, as main does
  • A request naming an audio mime type, and native or pass-through routes, keep Google's audio

User Flow

Before: a 1.108 user asking Gemini 3.8 TTS for pcm16 chat audio gets a WAV file instead

  1. Send POST https://litellm-domain/v1/chat/completions with "model": "gemini-3.8-flash-tts", "modalities": ["audio"], and "audio": {"voice": "Kore", "format": "pcm16"}
  2. Get HTTP 200, but the audio starts with a RIFF WAV header
  3. POST https://litellm-domain/v1/audio/speech with "response_format": "wav" returns a WAV with a second RIFF header inside

After: the same calls return the format they name

  1. The same chat call returns raw 16-bit PCM with no RIFF header
  2. The same speech call returns a WAV with one header

Relevant issues

Backport of #51145, which fixes #44541 on main

Picks

main commit lts commit
2490a55 6f0fb1d
9339b4b 5689abf
ca840c6 1b90c88
1ba64fb 25591f1
6d57373 25591f1
efcc5d4 25591f1
1f5446e 25591f1
d34fc9c 25591f1
bd5452a 25591f1
fd0ef57 25591f1
c703997 25591f1
d875f7d 25591f1

The first three picks applied clean apart from imports. The other nine went in as one commit, since they touch code only main has

Differences on this line
  • 1.108 has no tool-call-id signature flag or speech-config normalization, so only the audio flag goes through candidate parsing
  • The 1.108 speech bridge takes no model argument, so its tests call it without one
  • The 1.108 cache key leaves out extra_body for every param, so the two extra_body cache cases are dropped. Main keys it since fix(caching): key the response cache on extra_body by default #53898
  • 1.108 does not stream chat audio, so the streamed container and pass-through logging tests are dropped
  • The native logging test goes through the 1.108 success-logging entry point, since main's endpoint normalizer does not exist here

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays 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

Last updated: 6c2a6f2. QA ran on 6c2a6f2. Verification: 2639 tests across the alias check and every test file this PR changes, and make check pass

Live smoke on lts/1.108.x with real Gemini calls, one proxy booted from each tree with no database. The streamed cases from #51145 do not apply, since 1.108 does not stream chat audio

Case Before (d08d9d0) After (6c2a6f2)
Chat audio, pcm16 WAV, starts with RIFF Raw PCM, no RIFF
Speech, wav Two RIFF headers One RIFF header
Chat, AUDIO_WAV asked WAV WAV, kept
Proxy config

Both proxies ran this config with 2 workers and no database, each booted from its own tree on a random port, with real Gemini API calls

model_list:
  - model_name: gemini-3.8-flash-tts
    litellm_params:
      model: gemini/gemini-3.8-flash-tts
      api_key: os.environ/GEMINI_API_KEY

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

Launch script, run as launch.sh <tree> <port> proxy_config.yaml master_key (the base proxy on port 21463, the head proxy on port 25028)

#!/bin/zsh
set -eu
tree=$1 port=$2 cfg=$3 mkfile=$4
export PYTHON_DOTENV_DISABLED=1
export LITELLM_LOCAL_MODEL_COST_MAP=True
export GEMINI_API_KEY="$(~/.claude/bin/dotenv-get GEMINI_API_KEY --file $tree/.env)"
export LITELLM_MASTER_KEY="$(cat $mkfile)"
unset DATABASE_URL
cd $tree
exec .venv/bin/python litellm/proxy/proxy_cli.py --config $cfg --port $port --num_workers 2

decode_chat.py

import base64, json, sys
body = json.load(open(sys.argv[1]))
message = body["choices"][0]["message"]
audio = base64.b64decode(message["audio"]["data"])
print("model:", body.get("model"))
print("decoded audio bytes:", len(audio))
print("first 16 bytes:", audio[:16].hex(" "))
print("first 4 bytes as text:", audio[:4])
print("starts with RIFF:", audio.startswith(b"RIFF"))
print("RIFF headers in audio:", audio.count(b"RIFF"))

count_riff.py

import sys
audio = open(sys.argv[1], "rb").read()
print("file bytes:", len(audio))
print("first 16 bytes:", audio[:16].hex(" "))
print("RIFF headers in file:", audio.count(b"RIFF"))
print("RIFF header offsets:", [i for i in range(len(audio) - 3) if audio[i:i + 4] == b"RIFF"])

Before (d08d9d0)

(a) Chat audio with format pcm16

  1. Send the chat request that asks for pcm16 audio

    $ curl -sS http://127.0.0.1:21463/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "modalities": ["audio"], "audio": {"voice": "Kore", "format": "pcm16"}, "messages": [{"role": "user", "content": "Say hello from LiteLLM"}]}' -o chat_pcm16.json -w 'HTTP %{http_code}\n'
    HTTP 200
    
  2. Decode the returned audio

    $ python3 decode_chat.py chat_pcm16.json
    model: gemini-3.8-flash-tts
    decoded audio bytes: 130588
    first 16 bytes: 52 49 46 46 14 fe 01 00 57 41 56 45 66 6d 74 20
    first 4 bytes as text: b'RIFF'
    starts with RIFF: True
    RIFF headers in audio: 1
    

The audio starts with RIFF, so the caller asked for raw pcm16 and got a WAV file

(b) Speech with response_format wav
  1. Request the speech file as WAV

    $ curl -sS http://127.0.0.1:21463/v1/audio/speech -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "input": "Say hello from LiteLLM", "voice": "Kore", "response_format": "wav"}' -o speech.wav -w 'HTTP %{http_code} content-type %{content_type}\n'
    HTTP 200 content-type audio/wav
    
  2. Count the RIFF headers in the file

    $ python3 count_riff.py speech.wav
    file bytes: 132552
    first 16 bytes: 52 49 46 46 c0 05 02 00 57 41 56 45 66 6d 74 20
    RIFF headers in file: 2
    RIFF header offsets: [0, 44]
    

The file has two RIFF headers, the second one at byte 44 inside the audio data

(c) Chat audio with responseFormat AUDIO_WAV
  1. Send the chat request that asks Google for WAV

    $ curl -sS http://127.0.0.1:21463/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "modalities": ["audio"], "audio": {"voice": "Kore"}, "responseFormat": {"audio": {"mimeType": "AUDIO_WAV"}}, "messages": [{"role": "user", "content": "Say hello from LiteLLM"}]}' -o chat_wav.json -w 'HTTP %{http_code}\n'
    HTTP 200
    
  2. Decode the returned audio

    $ python3 decode_chat.py chat_wav.json
    model: gemini-3.8-flash-tts
    decoded audio bytes: 134428
    first 16 bytes: 52 49 46 46 14 0d 02 00 57 41 56 45 66 6d 74 20
    first 4 bytes as text: b'RIFF'
    starts with RIFF: True
    RIFF headers in audio: 1
    

The audio starts with RIFF, which is the WAV the caller asked Google for

After (6c2a6f2)

(a) Chat audio with format pcm16

  1. Send the chat request that asks for pcm16 audio

    $ curl -sS http://127.0.0.1:25028/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "modalities": ["audio"], "audio": {"voice": "Kore", "format": "pcm16"}, "messages": [{"role": "user", "content": "Say hello from LiteLLM"}]}' -o chat_pcm16.json -w 'HTTP %{http_code}\n'
    HTTP 200
    
  2. Decode the returned audio

    $ python3 decode_chat.py chat_pcm16.json
    model: gemini-3.8-flash-tts
    decoded audio bytes: 130560
    first 16 bytes: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    first 4 bytes as text: b'\x00\x00\x00\x00'
    starts with RIFF: False
    RIFF headers in audio: 0
    

The audio is raw 16-bit PCM with no RIFF header anywhere

(b) Speech with response_format wav
  1. Request the speech file as WAV

    $ curl -sS http://127.0.0.1:25028/v1/audio/speech -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "input": "Say hello from LiteLLM", "voice": "Kore", "response_format": "wav"}' -o speech.wav -w 'HTTP %{http_code} content-type %{content_type}\n'
    HTTP 200 content-type audio/wav
    
  2. Count the RIFF headers in the file

    $ python3 count_riff.py speech.wav
    file bytes: 122924
    first 16 bytes: 52 49 46 46 24 e0 01 00 57 41 56 45 66 6d 74 20
    RIFF headers in file: 1
    RIFF header offsets: [0]
    

The file has exactly one RIFF header, at byte 0

(c) Chat audio with responseFormat AUDIO_WAV
  1. Send the chat request that asks Google for WAV

    $ curl -sS http://127.0.0.1:25028/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "gemini-3.8-flash-tts", "modalities": ["audio"], "audio": {"voice": "Kore"}, "responseFormat": {"audio": {"mimeType": "AUDIO_WAV"}}, "messages": [{"role": "user", "content": "Say hello from LiteLLM"}]}' -o chat_wav.json -w 'HTTP %{http_code}\n'
    HTTP 200
    
  2. Decode the returned audio

    $ python3 decode_chat.py chat_wav.json
    model: gemini-3.8-flash-tts
    decoded audio bytes: 132508
    first 16 bytes: 52 49 46 46 94 05 02 00 57 41 56 45 66 6d 74 20
    first 4 bytes as text: b'RIFF'
    starts with RIFF: True
    RIFF headers in audio: 1
    

The audio still starts with RIFF, so the WAV the caller asked for passes through as is

Type

🐛 Bug Fix

Caveats (if any)

Copied from #51145, plus one row for this line

Low

  • WAV cached before the upgrade still reaches no-ask callers, and never expires without a ttl
    • Not worth a fix, since it repeats the old output. A new cache namespace at upgrade clears it
  • Requests carrying generationConfig, generation_config or responseFormat miss the cache once after the upgrade
    • Not worth a fix, since keeping the old key keeps serving the wrong container
  • Google's C2PA provenance chunk is dropped
    • Not worth a fix, since keeping it means returning WAV for pcm16
  • A top-level generationConfig, as the OpenAI SDK's extra_body sends it, now gets PCM where it got WAV
    • Not worth a fix, since honoring it means forwarding a key the proxy always dropped
  • On 1.108 an audio ask sent only inside a literal extra_body field shares a cache entry with a no-ask call
  • The router_code_coverage code-quality step fails on four untested router helpers
    • Not worth a fix here, since lts/1.108.x at 3404754 fails it the same way

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
Bugbot summary

[!NOTE]
Medium Risk
Touches response transformation and caching for Gemini/Vertex audio across chat, speech, batch, and passthrough paths; behavior changes for default pcm16 callers but preserves explicitly requested formats.

Overview
Backports Gemini 3.8 TTS audio handling so pcm16 chat and /v1/audio/speech no longer inherit nested WAV when Google returns audio/wav.

New as_raw_pcm16_base64 strips 24 kHz mono 16-bit WAV wrappers when the caller did not name an audio mime type; requests_audio_mime_type plus a keeps_requested_audio_format flag thread through Gemini/Vertex transforms, batch output, logging, streaming, and pass-through routes so explicitly requested WAV (and native passthrough) stay unchanged. The speech-to-completion bridge skips re-wrapping bytes that already sniff as WAV, and response cache keys now include responseFormat / generationConfig so audio-format asks do not collide with plain requests.

Reviewed by Cursor Bugbot for commit 6c2a6f2. Bugbot is set up for automated code reviews on this repo. Configure here.

…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.

(cherry picked from commit 9339b4b)
…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

(cherry picked from commit ca840c6)
…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
@quietjules
quietjules requested a review from a team November 24, 2026 16:42
@veria-ai

veria-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no outstanding findings

Summary

Backports the Gemini TTS fix so compatible WAV audio becomes raw PCM16 for chat callers, while explicitly requested formats and native responses stay unchanged

  • Gemini and Vertex audio follows the format the caller names.
  • Gemini speech responses keep a WAV file to one header.
  • Audio format requests get separate response-cache keys.

Reviews (3) · Last reviewed commit: "fix(gemini): forward the audio flag posi..." · Reviewed by Greptile

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

}:
return bool(mime_type)
case _:
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snake-case audio ask ignored beside camelCase

Medium Severity

requests_audio_mime_type picks generationConfig or generation_config with or, so a truthy camelCase config hides a snake-case mime ask. A chat call that sets modalities/audio (which fills generationConfig) and names AUDIO_WAV only under extra_body.generation_config is treated as a no-ask and unwrapped to PCM. That extra_body shape is an explicitly supported path, so the caller gets raw pcm16 instead of the WAV they named.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d6b7d35. Configure here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google answers HTTP 400 ("oneof field '_generation_config' is already set") when a body has both keys, so no response reaches the unwrap

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

1 issue from previous review remains unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 6c2a6f2. Configure here.

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mateo-berri
mateo-berri merged commit 2dd02ef into BerriAI:lts/1.108.x Nov 24, 2026
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants