🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completions
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
調査の方向性
shared/chat_wrappers.py、特に _handle_response と _set_streaming_token_metrics を読み、その後 tests/traces/test_chat.py と test_chat_reasoning を確認してください。completion_tokens_details.reasoning_tokens を含む usage chunk のストリーミング時のテストを追加してください。ストリーミングされた span に、トークンメトリクスとともに gen_ai.usage.reasoning_tokens が記録されれば完了です。issue の作成者は PR の提出を申し出ていますが、担当者もリンクされた pull request も報告されていません。
索引モデルが issue の本文から書いたものです。
説明
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
- At
8d9c93f:pip install -e packages/opentelemetry-semantic-conventions-ai -e packages/opentelemetry-instrumentation-openai openai==3.35.1 opentelemetry-sdk. - 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_tokensandreasoning streaming. #4458, #4461 and #4462 cover Anthropic, #4488 covers Groq. No open pull request addsreasoningtochat_wrappers.py.
- Searched open and closed issues and pull requests for
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?
- 主要言語
- Python
- スター
- 7.5k
- フォーク
- 1.2k
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
traceloop/openllmetry のほかの issue
-
bug python
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
traceloop/openllmetry#4717 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentation対応中かも @Quantum-Enigma-dot が 21 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
traceloop/openllmetry#4665 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Report: Failed dataset deletes still remove local rows and columns対応中かも @inchang-ing が 32 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
traceloop/openllmetry#4630 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penalty対応中かも @shantam77 が 38 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
traceloop/openllmetry#4598 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: LiteLLM instrumentation never ends the span when a call is interrupted (KeyboardInterrupt, asyncio cancellation)対応中かも @rupak-eng が 39 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
traceloop/openllmetry#4597 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
traceloop/openllmetry の issue をすべて見る
似ている issue
-
Internal Errorオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
frappe/frappe_docker#2071 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
The-Pocket/PocketFlow#161 ·
-
comp/desktop P2 type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NousResearch/hermes-agent#157702 ·
メンテナーはふだん 1 日以内に返信
-
scibench_physics / scibench_chemistry: a correct answer scores 0 when the unit has a power of ten対応中かも @BahaaMattar が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
EvolvingLMMs-Lab/lmms-eval#1699 ·
メンテナーはふだん 1 日以内に返信
-
project_inputs.html uses undefined file variable for dropdown IDs, lacks visual tag badges, and has empty delete labels対応中かも @Rucha0901 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
aboutcode-org/scancode.io#2354 ·
メンテナーはふだん 1 日以内に返信