🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returns

オープン 初心者向け
#4,717 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

v1/responses_wrappers.pyで、674行目と867行目付近にある同期および非同期のparse_response呼び出しから始め、その後、TracedDataの作成に失敗した場合に使われる既存のwrapperのfallbackを調べてください。issueのモックHTTPトランスポートを使って、両方の経路をカバーするテストを追加してください。完了条件は、不正なJSONで例外を発生させたりメトリクスを記録したりせず、生のレスポンスを返すことです。

索引モデルが issue の本文から書いたものです。

説明

bug python
Which component is this bug for?

OpenAI Instrumentation

📜 Description

client.responses.with_raw_response.create() is meant to return the raw HTTP response without parsing the body. The user decides whether to call .parse().

The Responses wrapper in v1/responses_wrappers.py calls parse_response(response) itself, so it can read the response ID, model and usage. That call sits outside the wrapper's try/except (around line 674 for sync and line 867 for async). If the body can't be parsed, the exception from our .parse() goes up to the user. The same call returns normally when the instrumentation is off.

The async path (async_parse_response) has the same problem.

👟 Reproduction steps
import httpx, openai
from opentelemetry.instrumentation.openai import OpenAIInstrumentor

def bad(req):
    return httpx.Response(200, headers={"content-type": "application/json"}, content=b"{not json")

def call():
    c = openai.OpenAI(api_key="k", max_retries=0,
                      http_client=httpx.Client(transport=httpx.MockTransport(bad)))
    return c.responses.with_raw_response.create(model="m", input="hi")

print(type(call()).__name__)   # LegacyAPIResponse
OpenAIInstrumentor().instrument()
call()                         # raises JSONDecodeError
👍 Expected behavior

The instrumentation never changes what the call returns or raises. If it can't parse the raw response, it should return the response untraced and record no metrics. The wrapper already does this when building TracedData fails (except Exception: return response).

👎 Actual Behavior with Screenshots
uninstrumented: returned LegacyAPIResponse status 200
instrumented:   raised JSONDecodeError
📃 Provide any additional context for the Bug.
  • This has been there since the first Responses instrumentation (#3052).
  • CodeRabbit raised it on #4713. It suggested recording duration and error.type and then re-raising. That would still break the user's call, and it would count a successful API call as a failed operation. So the fix is to guard the parse, not to record it as an error.
  • Suggested fix: wrap parse_response / async_parse_response in try/except Exception: return response in both the sync and async wrappers. Add a test using the mock transport above that checks the instrumented call returns the same object as the uninstrumented one.
  • Related: #4504, #4551, #4476 (other with_raw_response issues in the Responses wrapper).
👀 Have you spent some time to check if this bug has been raised before?
  • I checked and didn't find similar issue
主要言語
Python
スター
7.5k
フォーク
1.2k
平均マージ
2日 6時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

traceloop/openllmetry のほかの issue

traceloop/openllmetry の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。