Skip to content

fix: Accept usage metadata without a total token count - #191

Merged
aydinfatih merged 1 commit into
mainfrom
fix/usage-metadata-missing-total
Oct 12, 2026
Merged

aydinfatih merged 1 commit into
mainfrom
fix/usage-metadata-missing-total

Conversation

@aydinfatih

Copy link
Copy Markdown
Member

The API leaves promptTokenCount and totalTokenCount out of usageMetadata in some cases. For example, gemini-2.5-flash with a per-part media resolution returns:

"usageMetadata": {
  "candidatesTokenCount": 10,
  "thoughtsTokenCount": 667,
  "serviceTier": "standard"
}

totalTokenCount was required, so the whole response failed to parse:

TypeError: Gemini\Data\UsageMetadata::__construct(): Argument #2 ($totalTokenCount) must be of type int, null given

Fix

Default a missing totalTokenCount to 0, like promptTokenCount already is. The property type stays int.

Tests

  • A response with usage metadata that has neither count parses, and text() works. The test fails without the fix.

Verified against the API: the request above failed on main and works with this change.

The API leaves promptTokenCount and totalTokenCount out of the usage
metadata in some cases, e.g. for requests with a per-part media
resolution. A missing totalTokenCount failed the whole response with a
TypeError; it now defaults to 0, like promptTokenCount already did.
@aydinfatih
aydinfatih merged commit a74fc26 into main Oct 12, 2026
20 checks passed
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.

1 participant