Skip to content

fix(speech): do not wrap Gemini TTS audio that is already a WAV - #44548

Closed
canyang25 wants to merge 2 commits into
BerriAI:mainfrom
canyang25:fix/44541-gemini-tts-wav-header
Closed

canyang25 wants to merge 2 commits into
BerriAI:mainfrom
canyang25:fix/44541-gemini-tts-wav-header

Conversation

@canyang25

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Gemini TTS speech returns a WAV with a nested RIFF header
  • Players treat the inner header bytes as audio samples

How it solves it:

  • Leave audio alone when it is already a valid WAV
  • For response_format=pcm, strip to the data chunk only
  • Older models that return raw PCM still get a WAV wrapper

User Flow

Before: a developer calling Gemini TTS gets a double-header WAV that sounds like noise

  1. They POST https://litellm-domain/v1/audio/speech with a gemini TTS model and no special format
  2. The response is HTTP 200 with audio/wav bytes that start with RIFF
  3. Opening that file in a player shows garbage / crackle because a second RIFF sits inside the data

After: the same call returns a single clean WAV (or raw PCM when requested)

  1. They POST the same https://litellm-domain/v1/audio/speech request for the gemini TTS model
  2. The response is HTTP 200 with a normal RIFF/WAVE file (one header)
  3. With response_format=pcm, the body is raw PCM samples with no RIFF wrapper
  4. The file plays cleanly in a normal audio player

Relevant issues

Closes #44541

Screenshots / Proof of Fix

Unit coverage is on the public litellm.speech path (14 passed; new tests fail when the fix is reverted). Live Gemini TTS curl before/after was not re-run in this session — replace this section with a tip SHA before/after once you have a live recording if maintainers want e2e proof.

Before (be4481779e / main without fix)

  1. Call /v1/audio/speech for a gemini TTS model that returns WAV
  2. Response body starts with RIFF but contains a second RIFF inside the data chunk

After (736abf07474d31a3b9ceef58bfa1298e2f91156a)

  1. Same /v1/audio/speech call
  2. Response is a single RIFF/WAVE; response_format=pcm returns raw PCM only

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Data-chunk search ignores declared chunk sizes (non-standard WAVs only)

Developed with assistance from Cursor (Grok).

gemini-3.8 TTS models return a complete RIFF file. The speech bridge always added another WAV header, so players treated the inner header as samples

Developed with assistance from Cursor (Grok).
@canyang25
canyang25 requested a review from a team October 5, 2026 05:49
@CLAassistant

CLAassistant commented Oct 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Fixes audio format handling in the speech-to-text bridge.

The chunk-handling concern is non-blocking, but the repository’s provider-specific code placement requirement should be satisfied before merging

Findings

  1. P2 PCM includes WAV metadata ▶
  2. P2 Provider handling outside llms ▶

Summary

The PR preserves WAV audio already returned by Gemini TTS and strips its header for PCM responses while continuing to wrap raw PCM. The PCM extraction needs chunk-aware parsing, and the new provider-specific handling conflicts with the repository’s placement rule

Reviews (1) · Last reviewed commit: "fix(speech): do not wrap Gemini TTS audi..."

Comment on lines +31 to +34
data_index: Final = audio.find(b"data", 12)
if data_index < 12:
return audio
return audio[data_index + 8 :]

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.

P2 PCM includes WAV metadata If a WAV has chunks after its audio data, this slice returns them as PCM samples, causing noise. A metadata value containing data before the audio can also make extraction start at the wrong byte. Parse chunk boundaries and return only the declared audio data

Comment on lines +162 to +164
return _pcm16_payload(decoded_audio), "audio/pcm"
if decoded_audio.startswith(b"RIFF") and decoded_audio[8:12] == b"WAVE":
return decoded_audio, "audio/wav"

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.

P2 Provider handling outside llms The new Gemini WAV stripping and passthrough logic lives in the speech bridge. Repository instructions require provider-specific handling under llms/; this requirement must be satisfied before merging

Rule Used: What: Avoid writing provider-specific code outside of the llms/ directory. Why: This practice ensures better maintainability and reduces complexity over time. Good: ```python # Handle provider-specific logic within llms/vertex_ai/transformation.py ... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@codspeed

codspeed Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing canyang25:fix/44541-gemini-tts-wav-header (3c36c6b) with main (77fc331)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (3538e87) during the generation of this report, so 77fc331 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Gemini-specific PCM/WAV wrapping lived in the shared speech bridge. It now lives under litellm/llms/gemini, and PCM extraction walks RIFF chunks so a LIST chunk before data is not treated as samples

Developed with assistance from Cursor (Grok).
@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

#44541 was fixed by #51145 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Nov 24, 2026
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.

[Bug]: /v1/audio/speech returns a double WAV header for gemini-3.8 TTS models

2 participants