š Bug Report: Gemini streaming raises TypeError after the last chunk when usage metadata has no candidates_token_count
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- python
- Domain
- observability, testing
Research direction
Start with the usage calculation and histogram recording in packages/opentelemetry-instrumentation-google-generativeai/opentelemetry/instrumentation/google_generativeai/span_utils.py and the sync and async stream finalization in __init__.py. Reproduce the issue with the synthetic response described here, then check the Google Generative AI instrumentation tests for coverage of both wrappers. Done means streams finish normally and spans end when candidate token counts are absent; linked PR #4712 is already open.
Written by the indexing model from the issue text.
Description
Hullo folks,
Iām doing some research for Tessl Code Review and we ran it against your repo. It flagged a potential issue, and I figured it made sense to share it with you:
Which component is this bug for?
Google Generative AI Instrumentation
š Description
With metrics enabled, a Gemini stream whose last chunk has usage metadata but no candidates_token_count raises TypeError when the caller tries to finish iterating.
The last chunk has already been yielded. The failure occurs while recording the output-token histogram, and span.end() is not reached. Both sync and async stream wrappers reproduce this.
š Reproduction steps
This uses real SDK response objects and a real OpenTelemetry histogram, with a synthetic stream and no Gemini API call:
from google.genai.types import (
GenerateContentResponse, GenerateContentResponseUsageMetadata,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.instrumentation.google_generativeai import (
_build_from_streaming_response,
)
tracer = TracerProvider().get_tracer("repro")
meter = MeterProvider().get_meter("repro")
histogram = meter.create_histogram("gen_ai.client.token.usage")
chunk = GenerateContentResponse(
usage_metadata=GenerateContentResponseUsageMetadata(
prompt_token_count=2, total_token_count=2,
)
)
span = tracer.start_span("repro")
stream = _build_from_streaming_response(
span, iter([chunk]), "gemini-test", None, histogram,
)
assert next(stream) is chunk
try:
next(stream) # Expected: StopIteration
finally:
print("span still recording:", span.is_recording())
span.end()
š Expected behavior
The stream ends normally, the span ends, and missing token counts do not become non-numeric histogram measurements.
š Actual Behavior
span still recording: True
TypeError: must be real number, not NoneType
Controls tested:
| Final usage / metrics | Result |
|---|---|
| Missing candidate count, no thoughts, metrics on | TypeError |
| Candidate count 0, metrics on | Clean end |
| Missing candidate count, thoughts count 3, metrics on | Clean end |
| Missing candidate count, metrics off | Clean end |
The async wrapper produced the same TypeError after yielding its final chunk.
š¤ Python Version
3.13.11 on macOS arm64.
š Additional context
Tested installed versions:
opentelemetry-instrumentation-google-generativeai==0.63.1google-genai==3.4.0opentelemetry-sdk==1.45.2
The current source at 864f8591081251a40a40e0bb06a4d8084c3664d2 retains the same path:
This is a local wrapper reproduction with synthetic response data, not a live provider reproduction. #4603 handles thinking tokens, but when both candidate and thinking counts are absent, output_tokens still stays None. Skipping absent metrics or normalizing them according to the intended usage semantics would avoid the exception.
š Duplicate check
- Checked existing issues and related token-usage PRs; did not find this exact report.
Iām with Tessl. Tessl reviews code using repository-level context and specific review lenses that improve over time through feedback. Thatās how this finding surfaced. Weāre just getting started and want to offer the tool free to open-source projects. If this is useful, happy to give you access.
Baptiste
- Dominant language
- Python
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 2
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up ā it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from traceloop/openllmetry
-
š Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returnsPossibly taken A pull request linked to this issue is open or already merged. Openbug python
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
traceloop/openllmetry#4717 Ā·
Maintainers usually reply within 1 day
-
š Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentationPossibly taken @Quantum-Enigma-dot claimed this 21 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
traceloop/openllmetry#4665 Ā· 1 comment Ā·
Maintainers usually reply within 1 day
-
š Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completionsPossibly taken @jayfeng7 claimed this 23 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
traceloop/openllmetry#4660 Ā· 1 comment Ā·
Maintainers usually reply within 1 day
-
Bug Report: Failed dataset deletes still remove local rows and columnsPossibly taken @inchang-ing claimed this 32 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
traceloop/openllmetry#4630 Ā·
Maintainers usually reply within 1 day
-
š Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penaltyPossibly taken @shantam77 claimed this 38 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
traceloop/openllmetry#4598 Ā· 2 comments Ā·
Maintainers usually reply within 1 day
All issues in traceloop/openllmetry
Similar issues
-
opensubtitlescom: provider rejects pt-PT even though its own converter maps itPossibly taken @paulo-0308 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Diaoul/subliminal#1405 Ā·
-
Internal ErrorOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
frappe/frappe_docker#2071 Ā·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
The-Pocket/PocketFlow#161 Ā·
-
[Bug]: Webapp workspace fails to render in Safari ā "Can't find variable: requestIdleCallback"Opencomp/desktop P2 type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NousResearch/hermes-agent#157702 Ā·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
zai-org/zcode-plugins#202 Ā·
Maintainers usually reply within 1 day