fix(google): capture usage_metadata before early continues in streaming#5404
Open
Panmax wants to merge 2 commits intolivekit:mainfrom
Open
fix(google): capture usage_metadata before early continues in streaming#5404Panmax wants to merge 2 commits intolivekit:mainfrom
Panmax wants to merge 2 commits intolivekit:mainfrom
Conversation
…streaming In Gemini's streaming API, the final chunk often has usage_metadata with token counts but empty candidates or content.parts. Two early `continue` statements caused the usage_metadata check to be skipped, resulting in prompt_tokens=0, completion_tokens=0 in LLM metrics. Move the usage_metadata check to before any continue statements so it is always evaluated regardless of whether the chunk has content. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
prompt_tokens: 0, completion_tokens: 0in LLM metrics for Gemini streaming requestsusage_metadatacheck before the twocontinuestatements inLLMStream._runso token counts are always capturedusage_metadatawith complete token counts but has emptycandidatesorcontent.parts, causing the existing code to skip the usage check entirelyDetails
In
livekit-plugins-google/livekit/plugins/google/llm.py, the streaming loop has two earlycontinuestatements:if not response.candidates: continueif not candidate.content or not candidate.content.parts: continueBoth skip past the
usage_metadatacheck at the end of the loop body. Since Gemini's final streaming chunk often has afinish_reasonbut empty content, the usage data (which is only fully populated on this last chunk) is never captured.The fix moves the
usage_metadatacheck to immediately after theprompt_feedbackcheck, before anycontinuestatements.Test plan
prompt_tokens,completion_tokens, andtokens_per_second🤖 Generated with Claude Code