Skip to content

fix(ai-gemini): parse usageMetadata in image adapter response#331

Open
tecoad wants to merge 1 commit intoTanStack:mainfrom
tecoad:fix/gemini-image-usage-metadata
Open

fix(ai-gemini): parse usageMetadata in image adapter response#331
tecoad wants to merge 1 commit intoTanStack:mainfrom
tecoad:fix/gemini-image-usage-metadata

Conversation

@tecoad
Copy link

@tecoad tecoad commented Mar 4, 2026

Summary

  • Fix Gemini image adapter always returning usage: undefined by mapping usageMetadata from the API response to the ImageGenerationResult.usage field
  • Maps promptTokenCountinputTokens, candidatesTokenCountoutputTokens, totalTokenCounttotalTokens
  • Consistent with how the OpenAI image adapter already handles usage data

Closes #330

Test plan

  • Generate an image using Gemini adapter and verify usage is populated with token counts
  • Verify that when usageMetadata is absent, usage gracefully falls back to undefined
  • Run pnpm test:lib and pnpm test:types on the ai-gemini package

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Gemini image generation requests now properly include token usage information in responses, enabling better tracking and monitoring of API consumption metrics.

The Gemini image adapter was always returning `usage: undefined` even
though the API response includes `usageMetadata` with token counts.
Map `usageMetadata` fields to the `ImageGenerationResult.usage` object,
consistent with how the OpenAI image adapter already handles usage.

Closes TanStack#330

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 233cad24-153b-4d33-b4d5-f214d566b805

📥 Commits

Reviewing files that changed from the base of the PR and between 228e0e3 and dcc7b7c.

📒 Files selected for processing (1)
  • packages/typescript/ai-gemini/src/adapters/image.ts

📝 Walkthrough

Walkthrough

The Gemini image adapter now parses usageMetadata from API responses and populates the usage field in ImageGenerationResult, mapping prompt, candidates, and total token counts accordingly.

Changes

Cohort / File(s) Summary
Gemini Image Adapter
packages/typescript/ai-gemini/src/adapters/image.ts
Added conditional parsing of usageMetadata from Gemini response to populate usage object with mapped token counts; returns undefined when metadata is absent.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A rabbit hopped through Gemini's nest,
Found tokens hiding in the test,
With prompt and output counts aligned,
The usage field is now defined! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing the Gemini image adapter to parse usageMetadata in responses.
Description check ✅ Passed The description covers the changes made and references the test plan, but doesn't explicitly mark the template checklist items as complete.
Linked Issues check ✅ Passed The code changes directly address issue #330 by mapping Gemini's usageMetadata fields to ImageGenerationResult.usage with correct field transformations.
Out of Scope Changes check ✅ Passed The changes are limited to the Gemini image adapter and directly address the linked issue without introducing unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Gemini image adapter doesn't parse usageMetadata from response

1 participant