Skip to content

Fix bug in how tokens are counted when streaming generateContent method is called.#4152

Merged
aabmass merged 3 commits intoopen-telemetry:mainfrom
DylanRussell:fix_a_bug
Feb 2, 2026
Merged

Fix bug in how tokens are counted when streaming generateContent method is called.#4152
aabmass merged 3 commits intoopen-telemetry:mainfrom
DylanRussell:fix_a_bug

Conversation

@DylanRussell
Copy link
Contributor

@DylanRussell DylanRussell commented Jan 30, 2026

Description

Don't accumulate token counts across streaming responses from generateContent, the cumulative count is returned each time, so just use what's returned in the response instead. See #4120

Fixes #4120

Type of change

  • [x ] Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Unit tests

Does This PR Require a Core Repo Change?

  • [ x] No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • [x ] Followed the style guidelines of this project
  • [ x] Changelogs have been updated
  • [ x] Unit tests have been added
  • [ x] Documentation has been updated

Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a cassette test for this since it's a slightly confusing implementation detail of their RPC responses.

@aabmass aabmass merged commit 1f64f15 into open-telemetry:main Feb 2, 2026
663 checks passed
wikaaaaa pushed a commit to wikaaaaa/opentelemetry-python-contrib that referenced this pull request Feb 5, 2026
…thod is called. (open-telemetry#4152)

* Fix token count bug

* Add changelog

* Fix changelog
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.

[google-genai] Streaming token counts massively overcounted due to incorrect accumulation

7 participants