Skip to content

fix(gemini): tolerate missing usage metadata on final response - #4724

Merged
liramon2 merged 1 commit into
strands-agents:mainfrom
charan-rathore:fix/gemini-missing-usage-metadata
Sep 30, 2026
Merged

liramon2 merged 1 commit into
strands-agents:mainfrom
charan-rathore:fix/gemini-missing-usage-metadata

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

Fixes #4721.

Problem

Gemini sometimes sends a complete final response without usage metadata. Strands finishes the answer, then tries to read token counts from an object that is not there and crashes.

Fix

One condition: if event becomes if event and event.usage_metadata is not None. If the counts object is absent, the stream finishes normally without inventing a zero count. If the object exists with empty counts, existing behavior is unchanged. No change to token calculations, finish reasons, formatting, requests, or provider calls.

Tests

Three cases use real google.genai GenerateContentResponse objects parsed from JSON: two omitted-usage cases (STOP and SAFETY finish reasons) checking preserved answer text, expected stop reason, normal completed iteration, and no invented metadata; one empty-but-present usage object case protecting existing zero-count behavior. The client's response source is mocked only through the existing test fixture; the actual editable-checkout GeminiModel.stream and formatter run normally.

Before: 2 failed, 1 passed (the focused 3 cases). After: the full existing + new test_gemini.py passes, 78 tests in 1.22s. Python 3.10.12, pytest 9.1.1. Ruff check/format and git diff --check clean. No API requests, credentials, models, GPU, or broader integration-suite runs are claimed.

Credit: @abdes reported the issue.

Prepared with AI assistance (Instinct assisted).

@charan-rathore
charan-rathore requested a review from a team as a code owner September 30, 2026 10:44
@github-actions github-actions Bot added area-model Related to models or model providers python Pull requests that update python code bug Something isn't working labels Sep 30, 2026
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot added complexity/high A touched function exceeds cognitive complexity 25; may be worth splitting size/xs labels Sep 30, 2026
@charan-rathore

Copy link
Copy Markdown
Contributor Author

Thanks for the review.

@liramon2
liramon2 merged commit 4cbc6a7 into strands-agents:main Sep 30, 2026
40 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-model Related to models or model providers bug Something isn't working complexity/high A touched function exceeds cognitive complexity 25; may be worth splitting python Pull requests that update python code size/xs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] GeminiModel raises AttributeError when the usageMetadata object is absent

2 participants