fix(google): move generateContentStream inside try/catch in Gemini TTS - #1096
Merged
toubatbrian merged 2 commits intoMar 4, 2026
Merged
Conversation
When the Gemini API rejects a request before streaming begins (e.g. 429 rate limit), the error escapes ChunkedStream.run() without being caught or converted to an APIStatusError. This bypasses the base class retry logic and emits non-recoverable errors that can kill the AgentSession. Moving generateContentStream inside the existing try block ensures all API errors go through the error handling code path (lines 226-272), are converted to proper APIStatusError instances with the correct retryable flag, and are eligible for retry by _mainTaskImpl. Fixes livekit#1095
🦋 Changeset detectedLatest commit: 93e3ecd The changes in this PR will be included in the next version bump. This PR includes changesets to release 21 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
toubatbrian
approved these changes
Mar 4, 2026
This was referenced Mar 4, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the
generateContentStreamcall inside the existing try/catch block inChunkedStream.run()(plugins/google/src/beta/gemini_tts.ts).Before: When the Gemini API rejects a request before streaming (e.g. 429 rate limit), the
@google/genaiApiErrorescapesrun()without being converted to a LiveKitAPIStatusError. The base class_mainTaskImpldoesn't recognize it asinstanceof APIError, so retries are skipped and the error is emitted as non-recoverable.After: All API errors — whether thrown during request initiation or stream iteration — go through the existing error handling code, get converted to
APIStatusErrorwith the correctretryableflag, and are eligible for retry.This is a one-line structural change (moving the
const responseStream = await ...from beforetry {to inside it). No logic changes.Problem
When using Gemini TTS (e.g.
gemini-2.5-flash-preview-tts) and the API returns a 429 rate limit error, the error is thrown bygenerateContentStreamat line 216 — which sits outside the try/catch block (lines 222-275). The@google/genaiApiErroris a different class from LiveKit'sAPIError, so the base class retry logic in_mainTaskImpldoesn't recognize it:Combined with
StreamAdaptersplitting text into N sentences (each a parallel TTS call), a single quota exhaustion produces N non-recoverable error events — exceedingAgentSession.maxUnrecoverableErrors(default: 3) and killing the session, even though STT, LLM, and text transcription still work.Relevant log output
Test plan
APIStatusError({ retryable: true }), and retried by the base class🤖 Generated with Claude Code