fix(openai): Attach response model with streamed Completions API #5557
2 issues
Medium
Access to x.model outside capture_internal_exceptions block can cause runtime errors - `sentry_sdk/integrations/openai.py:615`
The new span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model) call is placed outside the capture_internal_exceptions() block, unlike similar operations in this same function (e.g., accessing x.choices). If a streaming chunk lacks the model attribute, an AttributeError will be raised and propagate to the user's code, breaking the stream iteration. The existing pattern at lines 757-758 (in the Responses API streaming function) places this same operation inside the exception handler.
Unprotected attribute access to x.model may raise exception and break iteration - `sentry_sdk/integrations/openai.py:615`
The new code span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model) directly accesses x.model without checking if the attribute exists, and it's placed OUTSIDE the capture_internal_exceptions() block. If x.model is missing or raises an exception (e.g., with API changes or non-standard responses), this will propagate to user code and break their stream iteration. This is inconsistent with the existing pattern at line 472 which uses hasattr(response, "model") before access, and with the responses API handler at line 758 which wraps similar access inside capture_internal_exceptions().
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 1 | 1m 55s | $0.78 |
| find-bugs | 1 | 2m 41s | $1.59 |
| skill-scanner | 0 | 1m 57s | $0.42 |
| security-review | 0 | 2m 47s | $0.34 |
Duration: 9m 20s · Tokens: 1.7M in / 23.9k out · Cost: $3.15 (+dedup: $0.01, +extraction: $0.01)