🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completions

Đang mở Phù hợp với người mới
#4,660 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@jayfeng7 đang làm issue này rồi.

Từ ngày 27/10/2026.

  • #4662 của @jayfeng7 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
observability

Hướng nghiên cứu

Đọc shared/chat_wrappers.py, đặc biệt là _handle_response và _set_streaming_token_metrics, sau đó kiểm tra tests/traces/test_chat.py và test_chat_reasoning. Bổ sung kiểm thử cho luồng streaming có chunk usage chứa completion_tokens_details.reasoning_tokens; hoàn tất khi span của luồng streaming ghi nhận gen_ai.usage.reasoning_tokens cùng với các chỉ số token. Tác giả issue đề nghị gửi PR, nhưng không có thông tin về người được giao hoặc pull request được liên kết.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Which component is this bug for?

OpenAI Instrumentation

📜 Description

A streamed chat completion never gets gen_ai.usage.reasoning_tokens, even when its usage chunk carries completion_tokens_details.reasoning_tokens. Without stream=True the span records it.

Only _handle_response sets the attribute (shared/chat_wrappers.py:334-354 at 8d9c93f), since #3336. Every stream path, async included, goes through _set_streaming_token_metrics (chat_wrappers.py:615), which sets input and output tokens and nothing else. The semantic-conventions page defines it as the reasoning part of completion_tokens, with no exception for streams. The only test, test_chat_reasoning (tests/traces/test_chat.py:1449), makes a non-stream call.

👟 Reproduction steps
  1. At 8d9c93f: pip install -e packages/opentelemetry-semantic-conventions-ai -e packages/opentelemetry-instrumentation-openai openai==3.35.1 opentelemetry-sdk.
  2. Run this (a mock transport, no network):
import json

import httpx
import openai
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))
OpenAIInstrumentor().instrument(tracer_provider=provider)

USAGE = {"prompt_tokens": 12, "completion_tokens": 300, "total_tokens": 312,
         "completion_tokens_details": {"reasoning_tokens": 256}}
BASE = {"id": "c1", "created": 0, "model": "o4-mini"}


def handler(request):
    if json.loads(request.content).get("stream"):
        chunks = [
            BASE | {"object": "chat.completion.chunk", "choices": [{"index": 0, "delta": {
                "role": "assistant", "content": "Paris."}, "finish_reason": "stop"}]},
            BASE | {"object": "chat.completion.chunk", "choices": [], "usage": USAGE},
        ]
        body = "".join(f"data: {json.dumps(c)}\n\n" for c in chunks) + "data: [DONE]\n\n"
        return httpx.Response(200, content=body.encode(),
                              headers={"content-type": "text/event-stream"})
    return httpx.Response(200, json=BASE | {"object": "chat.completion", "usage": USAGE,
        "choices": [{"index": 0, "message": {"role": "assistant", "content": "Paris."},
                     "finish_reason": "stop"}]})


client = openai.OpenAI(api_key="x", http_client=httpx.Client(transport=httpx.MockTransport(handler)))
for stream in (False, True):
    kw = {"stream_options": {"include_usage": True}} if stream else {}
    resp = client.chat.completions.create(model="o4-mini", stream=stream,
                                          messages=[{"role": "user", "content": "Capital of France?"}], **kw)
    if stream:
        for _ in resp:
            pass
    a = exporter.get_finished_spans()[-1].attributes
    print(f"stream={stream!s:<5} output_tokens={a.get('gen_ai.usage.output_tokens')} "
          f"reasoning_tokens={a.get('gen_ai.usage.reasoning_tokens')}")
    exporter.clear()
👍 Expected behavior

Both spans carry gen_ai.usage.reasoning_tokens=256.

👎 Actual Behavior with Screenshots
$ python repro_openai.py
stream=False output_tokens=300 reasoning_tokens=256
stream=True  output_tokens=300 reasoning_tokens=None
🤖 Python Version

3.12

📃 Provide any additional context for the Bug.

I ran the sync stream only, and read the async path from the code.

👀 Have you spent some time to check if this bug has been raised before?
  • I checked and didn't find similar issue
    • Searched open and closed issues and pull requests for reasoning_tokens and reasoning streaming. #4458, #4461 and #4462 cover Anthropic, #4488 covers Groq. No open pull request adds reasoning to chat_wrappers.py.
Are you willing to submit PR?

Yes I am willing to submit a PR!

@doronkopit5, I'd move the reasoning block into a helper that _handle_response and _set_streaming_token_metrics both call, so every stream path gets it, plus a streamed twin of test_chat_reasoning. Shall I open it?

Ngôn ngữ chính
Python
Star
7.5k
Fork
1.2k
Merge trung bình
2 ngày 6 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của traceloop/openllmetry

Tất cả issue của traceloop/openllmetry

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.