🐛 Bug Report: Traces go missing when a model reports 0 tokens

Aperta
#4,588 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@manojmulammagari ci sta già lavorando.

Dal 13/10/2026.

  • #4618 di @manojmulammagari — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
observability

Direzione di ricerca

Start by running the reproduction script in the issue, which uses a mocked OpenAI transport returning zero token usage, and confirm both span lists are empty. The two instrumentors involved are LangchainInstrumentor and OpenAIInstrumentor, so compare how each handles a zero token count in the usage data. Done when both spans appear with 0 tokens and later calls keep being traced.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Which component is this bug for?

Langchain Instrumentation

📜 Description

When a model returns 0 in its token usage, the Langchain span is never sent, and later LLM calls stop being traced too.
Local models and proxies often report 0 tokens, so this is easy to hit.
This is what i assume and reviewed, correct me if i am wrong

👟 Reproduction steps

No API key needed, it uses a fake OpenAI server that returns 0 tokens:

import httpx
import openai
from langchain_openai import ChatOpenAI
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter

exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
LangchainInstrumentor().instrument(tracer_provider=provider)
OpenAIInstrumentor().instrument(tracer_provider=provider)

# fake OpenAI server that reports 0 tokens
http = httpx.Client(transport=httpx.MockTransport(lambda request: httpx.Response(200, json={
    "id": "1", "object": "chat.completion", "created": 0, "model": "m",
    "choices": [{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}],
    "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
})))

ChatOpenAI(model="m", api_key="x", base_url="http://mock/v1", http_client=http).invoke("hi")
print("langchain spans:", [s.name for s in exporter.get_finished_spans()])

exporter.clear()
openai.OpenAI(api_key="x", base_url="http://mock/v1", http_client=http).chat.completions.create(
    model="m", messages=[{"role": "user", "content": "hi"}]
)
print("openai spans:", [s.name for s in exporter.get_finished_spans()])
👍 Expected behavior
langchain spans: ['ChatOpenAI.chat']
openai spans: ['openai.chat']
👎 Actual Behavior with Screenshots
langchain spans: []
openai spans: []

If you change the usage to non-zero numbers, both spans show up as expected.

Image
🤖 Python Version

3.11

📃 Provide any additional context for the Bug.

No response

👀 Have you spent some time to check if this bug has been raised before?
  • I checked and didn't find similar issue
Are you willing to submit PR?

Yes I am willing to submit a PR!

Lingua principale
Python
Stelle
7.5k
Fork
1.2k
Merge medio
1g 23h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di traceloop/openllmetry

Tutte le issue di traceloop/openllmetry

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.