🐛 Bug Report: Gemini streaming raises TypeError after the last chunk when usage metadata has no candidates_token_count
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- observability, testing
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 7.5k
- Fork
- 1.2k
- Merge medio
- 1g 23h
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di traceloop/openllmetry
-
🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returnsForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertabug python
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
traceloop/openllmetry#4717 ·
I maintainer di solito rispondono entro 1 giorno
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentationForse già presa @Quantum-Enigma-dot l’ha presa 21 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
traceloop/openllmetry#4665 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completionsForse già presa @jayfeng7 l’ha presa 23 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
traceloop/openllmetry#4660 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Report: Failed dataset deletes still remove local rows and columnsForse già presa @inchang-ing l’ha presa 32 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
traceloop/openllmetry#4630 ·
I maintainer di solito rispondono entro 1 giorno
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penaltyForse già presa @shantam77 l’ha presa 38 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
traceloop/openllmetry#4598 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di traceloop/openllmetry
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
mozilla-ai/cq#601 ·
I maintainer di solito rispondono entro 1 giorno
-
Bump Python to 3.14 or 3.15Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
intcomma returns exponential notation for large floatsForse già presa @Omarinho12345 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-humanize/humanize#521 ·
I maintainer di solito rispondono entro 1 giorno
-
LUCJ pass manager raises NoEdgeBetweenNodes when an edge is bad for two two-qubit gatesForse già presa @hat-im l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
opensubtitlescom: provider rejects pt-PT even though its own converter maps itForse già presa @paulo-0308 l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
Diaoul/subliminal#1405 ·