[Platform][VertexAi] Yield TokenUsage from Gemini stream chunks#2008
Open
Amoifr wants to merge 1 commit intosymfony:mainfrom
Open
[Platform][VertexAi] Yield TokenUsage from Gemini stream chunks#2008Amoifr wants to merge 1 commit intosymfony:mainfrom
Amoifr wants to merge 1 commit intosymfony:mainfrom
Conversation
The streaming token extraction was disabled in symfony#1267 because the previous implementation re-iterated the response generator, which conflicted with the agent already consuming it (see symfony#972). As a result, token usage was silently lost on streaming Gemini calls. Align Gemini with the OpenAi/Ollama pattern: yield TokenUsage directly from `ResultConverter::convertStream()` whenever a chunk carries `usageMetadata`, so the value is delivered through the same generator that produces the deltas — no second iteration needed. `TokenUsageExtractor::extractUsageMetadata()` is renamed to `fromUsageMetadata()` and made public so the converter can reuse it, mirroring `OpenAi\\Gpt\\TokenUsageExtractor::fromDataArray()`. Closes symfony#972
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.
The streaming token extraction was disabled in #1267 because the previous implementation re-iterated the response generator, which conflicted with the agent already consuming it (the
Generator passed to yield from was abortederror reported by @franzwilding in #972). The workaround stopped the crash but silently dropped token usage on every streaming Gemini call.This PR aligns Gemini with the OpenAi (
Gpt\\ResultConverter::convertStream) and Ollama (OllamaResultConverter::convertStream) pattern: yieldTokenUsagedirectly from the converter's stream whenever a chunk carriesusageMetadata, so the value is delivered through the same generator that produces the deltas — no second iteration needed.TokenUsageExtractor::extractUsageMetadata()is renamed tofromUsageMetadata()and made public so the converter can reuse it, mirroringOpenAi\\Gpt\\TokenUsageExtractor::fromDataArray(). The non-streamextract()path is unchanged.Test coverage:
testStreamingYieldsTokenUsageWhenUsageMetadataIsPresentasserts that a stream emittingusageMetadatamid-flight produces aTokenUsageInterfacedelta interleaved with the regularTextDeltas, with the rightprompt/completion/thinking/totaltoken countsTokenUsageExtractor::extract()continues to returnnullfor streams, since the new path now delivers the usage through the converter — the extractor is only invoked on non-stream calls or by external code that may still want to attempt stream extraction without going through the converter.