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

Aperta Adatta ai principianti
#4,717 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Inizia in v1/responses_wrappers.py, nelle chiamate sincrona e asincrona a parse_response intorno alle righe 674 e 867, quindi esamina il fallback esistente del wrapper quando la creazione di TracedData non riesce. Aggiungi test usando il trasporto HTTP simulato dell’issue per entrambi i percorsi; il lavoro è completato quando il JSON malformato restituisce la risposta grezza senza generare eccezioni né registrare metriche.

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

Descrizione

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
Lingua principale
Python
Stelle
7.5k
Fork
1.2k
Merge medio
2g 6h
PR unite (30g)
2

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.