Title: OpenAI integration raises `TypeError: 'NoneType' object is not iterable` into user code when `response.output` is `None` · Issue #7222 · getsentry/sentry-python · GitHub
Open Graph Title: OpenAI integration raises `TypeError: 'NoneType' object is not iterable` into user code when `response.output` is `None` · Issue #7222 · getsentry/sentry-python
X Title: OpenAI integration raises `TypeError: 'NoneType' object is not iterable` into user code when `response.output` is `None` · Issue #7222 · getsentry/sentry-python
Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.68.0 (also reproduces on 2.35.1 and 2.40.0) Steps to Reproduce The OpenAI integration raises out of the instrumented create() call when a response object has a None collection wher...
Open Graph Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.68.0 (also reproduces on 2.35.1 and 2.40.0) Steps to Reproduce The OpenAI integration raises out of the instrumented create() call when a re...
X Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.68.0 (also reproduces on 2.35.1 and 2.40.0) Steps to Reproduce The OpenAI integration raises out of the instrumented create() call when a re...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/7222
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"OpenAI integration raises `TypeError: 'NoneType' object is not iterable` into user code when `response.output` is `None`","articleBody":"### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Version\n\n2.68.0 (also reproduces on 2.35.1 and 2.40.0)\n\n### Steps to Reproduce\n\nThe OpenAI integration raises out of the instrumented `create()` call when a response object has a `None` collection where the integration expects an iterable. The exception is not contained by the SDK, so it surfaces as an application error and replaces whatever the API actually returned.\n\nThis is reachable in practice through OpenAI-compatible gateways. When a gateway has already flushed `200 OK` (for example after sending keep-alive padding while it buffers a large request) and only then learns the upstream call failed, it can no longer change the status code, so it reports the failure in the body:\n\n```json\n{\"error\": {\"message\": \"... At least one of the image dimensions exceed max allowed size: 8000 pixels\", \"code\": 400}}\n```\n\nThe `openai` client parses that into a model object with no `output` / `choices` populated, and instrumenting it raises.\n\nMinimal reproduction, no network needed:\n\n```python\nfrom unittest import mock\n\nimport sentry_sdk\nfrom openai import OpenAI\nfrom openai.types.responses import Response\nfrom sentry_sdk.integrations.openai import OpenAIIntegration\n\nsentry_sdk.init(\n dsn=\"https://public@o1.ingest.sentry.io/1\",\n traces_sample_rate=1.0,\n integrations=[OpenAIIntegration()],\n transport=lambda event: None,\n)\n\n# What the client parses out of a 200 response that only carries an error\ngateway_error = Response.construct(\n error={\"message\": \"upstream failed\", \"code\": 400},\n output=None,\n)\n\nclient = OpenAI(api_key=\"z\")\nclient.responses._post = mock.Mock(return_value=gateway_error)\n\nwith sentry_sdk.start_transaction(name=\"tx\"):\n response = client.responses.create(model=\"gpt-4o\", input=\"hello\")\n print(response.error)\n```\n\n### Expected Result\n\n`create()` returns the response object, and the application can read `response.error` to see what actually went wrong. Instrumentation problems stay inside the SDK.\n\n### Actual Result\n\n```\n File \"sentry_sdk/integrations/openai.py\", line 1532, in _new_sync_responses_create\n _set_responses_api_output_data(\n File \"sentry_sdk/integrations/openai.py\", line 1275, in _set_responses_api_output_data\n _set_common_output_data(\n File \"sentry_sdk/integrations/openai.py\", line 746, in _set_common_output_data\n for output in response.output:\n ^^^^^^^^^^^^^^^\nTypeError: 'NoneType' object is not iterable\n```\n\nThe application never sees the real error message, so any retry/fallback logic keyed on it stops working.\n\nThere appear to be two separate causes:\n\n1. **`response.output` is not checked for `None`.** `response.choices` was given an `is not None` check after #5071, but the Responses API branch still does a bare `hasattr(response, \"output\")` in `_set_common_output_data` and `_calculate_responses_token_usage`.\n\n2. **The non-streaming paths lost their `capture_internal_exceptions()` wrapper.** Before #4612 the whole response-handling block ran inside `with capture_internal_exceptions():`. The restructuring into `_set_*_input_data` / `_set_*_output_data` moved that code out from under the wrapper, so any error there now propagates into user code. The streaming iterators kept their protection; the non-streaming paths did not. This is why `\u003c2.34` is unaffected.\n\nThe second point is the more general one — with it fixed, this class of bug degrades to a missing span attribute rather than an application crash, which matches the SDK contract in CONTRIBUTING.md (\"Users do not expect their application to crash\").\n\nI have a patch with tests for both points and am happy to open a PR if this looks like the right direction.\n","author":{"url":"https://github.com/FromCSUZhou","@type":"Person","name":"FromCSUZhou"},"datePublished":"2026-08-18T18:37:09Z","url":"https://github.com/getsentry/sentry-python/issues/7222"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4356a063-28b2-dec2-4868-12cabbf748e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9C2:1BD25B:33ADE6:477AAD:6B008AD8 |
| html-safe-nonce | 11fde597c08005a42de9f946651c232fb6f187cf035b72a9b3fce214ff61e331 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUMyOjFCRDI1QjozM0FERTY6NDc3QUFEOjZCMDA4QUQ4IiwidmlzaXRvcl9pZCI6IjQ4MzY0MzgzNTE2OTc0NDc2NDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c676028d73bc2d55eccee2925d576c9c172ad925b6f77e2e6aaec157b599ba70 |
| hovercard-subject-tag | issue:5185065600 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-python/7222/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c4482aa46122cf88ffc43ca3520ca19033a5310a8e13fd1b1c05427942987834/getsentry/sentry-python/issues/7222 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c4482aa46122cf88ffc43ca3520ca19033a5310a8e13fd1b1c05427942987834/getsentry/sentry-python/issues/7222 |
| og:image:alt | How do you use Sentry? Sentry Saas (sentry.io) Version 2.68.0 (also reproduces on 2.35.1 and 2.40.0) Steps to Reproduce The OpenAI integration raises out of the instrumented create() call when a re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FromCSUZhou |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4d3d8e50f02c1d7bf86d5acfa1d3bfe9366abd6528a6b429eb07a168429d2b61 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 137814779 |
| octolytics-dimension-repository_nwo | getsentry/sentry-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 137814779 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | db758f8e02b8257935fb03edf81a3e76eb3abd11 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width