🐛 Bug Report: OpenAI Responses API records no metrics (gen_ai.client.operation.duration, gen_ai.client.token.usage)

未关闭
#4,585 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@zhaoruizhe81 已经在做这个了。

开始于 2026年10月10日。

  • #4596 来自 @zhaoruizhe81 —— 未关闭
  • #4697 来自 @doronkopit5 —— 已关闭,未合并
  • #4713 来自 @doronkopit5 —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
observability

调研方向

Start with the wrappers in opentelemetry/instrumentation/openai/v1/init.py (responses_get_or_create_wrapper and async_responses_get_or_create_wrapper) and with v1/responses_wrappers.py, then compare them with the Chat Completions path that already records gen_ai.client.operation.duration and gen_ai.client.token.usage. Run the reproduction script in the issue: it should show new gen_ai metric points for responses.create, sync and async, on success and on the InternalServerError path, with error.type set on failure. Streaming should record metrics too.

由索引模型根据 Issue 内容生成。

描述

Which component is this bug for?

OpenAI Instrumentation

📜 Description

For Chat Completions, the OpenAI instrumentation records gen_ai.client.operation.duration and gen_ai.client.token.usage, including a duration point with error.type when the call fails. For the Responses API (client.responses.create), no metric is recorded at all, neither on success nor on failure. Spans are created normally.

In opentelemetry/instrumentation/openai/v1/__init__.py, the Responses wrappers (responses_get_or_create_wrapper, async_responses_get_or_create_wrapper) only receive the tracer, and v1/responses_wrappers.py never records to the duration or token histograms. So an application that moves from Chat Completions to the Responses API loses its latency and token-usage metrics without any warning.

gen_ai.client.operation.duration is a required metric in the OpenTelemetry GenAI semantic conventions, and gen_ai.client.token.usage is recommended whenever token counts are available. The Responses API returns them in usage, and the span already records them.

👟 Reproduction steps

pip install openai httpx opentelemetry-sdk opentelemetry-instrumentation-openai

import httpx, openai
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.trace import TracerProvider

def respond(request):  # local stand-in for the OpenAI API
    if request.url.path.endswith("/responses"):
        return httpx.Response(200, json={
            "id": "resp_1", "object": "response", "created_at": 0, "status": "completed", "model": "gpt-4o-mini",
            "output": [{"type": "message", "id": "msg_1", "status": "completed", "role": "assistant",
                        "content": [{"type": "output_text", "text": "hi", "annotations": []}]}],
            "parallel_tool_calls": True, "tool_choice": "auto", "tools": [],
            "usage": {"input_tokens": 7, "output_tokens": 3, "total_tokens": 10,
                      "input_tokens_details": {"cached_tokens": 0}, "output_tokens_details": {"reasoning_tokens": 0}}})
    return httpx.Response(200, json={
        "id": "chatcmpl-1", "object": "chat.completion", "created": 0, "model": "gpt-4o-mini",
        "choices": [{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}],
        "usage": {"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10}})

reader = InMemoryMetricReader()
OpenAIInstrumentor().instrument(tracer_provider=TracerProvider(),
                                meter_provider=MeterProvider(metric_readers=[reader]))
client = openai.OpenAI(api_key="x", max_retries=0,
                       http_client=httpx.Client(transport=httpx.MockTransport(respond)))

def gen_ai_points():
    data = reader.get_metrics_data()
    return sum(p.count for rm in (data.resource_metrics if data else []) for sm in rm.scope_metrics
               for m in sm.metrics if m.name.startswith("gen_ai.client.") for p in m.data.data_points)

before = gen_ai_points()
client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "hello"}])
print("chat.completions.create: new gen_ai metric points =", gen_ai_points() - before)

before = gen_ai_points()
client.responses.create(model="gpt-4o-mini", input="hello")
print("responses.create:        new gen_ai metric points =", gen_ai_points() - before)
👍 Expected behavior

responses.create (sync and async, streaming and non-streaming) records gen_ai.client.operation.duration for every call, with error.type when the call fails, and gen_ai.client.token.usage (input and output) from response.usage, as Chat Completions does.

👎 Actual Behavior with Screenshots

chat.completions.create ok new metric points: gen_ai.client.token.usage (output) +1, gen_ai.client.token.usage (input) +1, gen_ai.client.operation.duration +1
responses.create ok new metric points: NONE
chat.completions.create InternalServerError new metric points: gen_ai.client.operation.duration (error.type=InternalServerError) +1
responses.create InternalServerError new metric points: NONE

🤖 Python Version

3.12

📃 Provide any additional context for the Bug.

opentelemetry-instrumentation-openai 0.62.4 (also main at df4921a), openai 2.54.0.

👀 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?

None

主要语言
Python
星标
7.5k
派生
1.2k
平均合并
1 天 23 小时
30 天内合并 PR
3

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

traceloop/openllmetry 的其他 Issue

查看 traceloop/openllmetry 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。