Repository navigation
fix(antigravity): forward the trailing OpenAI usage-only chunk as usageMetadata - #18643
Open
AndresDiagoM wants to merge 3 commits into
Open
AndresDiagoM wants to merge 3 commits into
AndresDiagoM wants to merge 3 commits into
Conversation
This branch has not been deployed
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
Problem. OpenAI-compatible upstreams called with
stream_options.include_usagesend usage in a separate final chunk withchoices: [], after the finish chunk (fix(sse): estimate usage in passthrough stream even with include_usage #12151).open-sse/translator/response/openai-to-antigravity.tshandles a chunk withoutchoices[0]by stashingstate._usageand returningnull(L27-33 on the tip). The stash is only read when a later chunk with a choice builds a response, and after the finish chunk none comes. The stream-end flush does not rescue it either:stream.ts(L3174, L3218) callstranslateResponse(..., null, state), and the translator returnsnullfor anullchunk. So the usage never reaches the client: nousageMetadata, no prompt/candidate/total counts, no thoughts or cached tokens.Scope. The same function is registered for
OPENAI → GEMINItoo (openai-to-gemini.ts:14), so both Antigravity and Gemini client formats lose usage this way. fix(gemini): forward OpenAI usage-only chunks and cached/thought token counts to /v1beta clients #17842 fixed the same gap in the/v1betaSSE converter (openai-to-gemini-sse.ts); this is the sibling path.Fix (same approach as fix(gemini): forward OpenAI usage-only chunks and cached/thought token counts to /v1beta clients #17842). A small helper,
usageOnlyChunkResponse, handles the no-choices case:finishReason,usageMetadata, and the stream'smodelVersion/responseId).The inline usage mapping is replaced by the shared
buildGeminiUsageMetadatafromgeminiUsageMetadata.ts(fix(gemini): forward OpenAI usage-only chunks and cached/thought token counts to /v1beta clients #17842), which produces the same fields.Not changed: tool-call accumulation, how the finish chunk carries usage, the stash-before-finish behavior, the non-streaming path. A trailing chunk with neither choices nor usage is still dropped.
Proof Of Fix
tests/unit/translator-resp-openai-to-antigravity.test.ts, 5 new tests:finishReason, and the stream'smodelVersion/responseId.translateResponse(OPENAI, ANTIGRAVITY): text, finish, usage-only chunk, then the flushnull. Exactly one chunk carriesusageMetadata, and it is the last one.Validation
check:open-sse-typecheck(0 errors),check:complexity-ratchets --base-ref origin/release/v3.8.53(complexity violations in the touched file 2 → 1),check:file-size: OKconfig/quality/eslint-suppressions.json) clean; commit hooks incl.check:ai-attributionpasstranslateResponseregistry, not throughstream.ts's transform stream.