Title: OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errors · Issue #7847 · getsentry/sentry-python · GitHub
Open Graph Title: OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errors · Issue #7847 · getsentry/sentry-python
X Title: OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errors · Issue #7847 · getsentry/sentry-python
Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.71.0 (master 8afefe8), openai 3.22.1 Steps to Reproduce _wrap_synchronous_completions_chunk_iterator (sentry_sdk/integrations/openai.py:1001) finishes the gen_ai.chat span after it...
Open Graph Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.71.0 (master 8afefe8), openai 3.22.1 Steps to Reproduce _wrap_synchronous_completions_chunk_iterator (sentry_sdk/integrations/openai.py:1001...
X Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.71.0 (master 8afefe8), openai 3.22.1 Steps to Reproduce _wrap_synchronous_completions_chunk_iterator (sentry_sdk/integrations/openai.py:1001...
Opengraph URL: https://github.com/getsentry/sentry-python/issues/7847
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errors","articleBody":"### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Version\n\n2.71.0 (master `8afefe8`), openai 3.22.1\n\n### Steps to Reproduce\n\n`_wrap_synchronous_completions_chunk_iterator` (`sentry_sdk/integrations/openai.py:1001`) finishes the `gen_ai.chat` span after its `for` loop, with no `finally` and no patched `Stream.close()`. The caller never reaches that code if it closes the stream early, leaves a `with` block after one chunk, or loses the connection. The span then has no timestamp and the transaction drops it. The Responses API iterator at `:1143` and both async iterators have the same gap.\n\nAnthropic finishes the span in a `finally` (#5643) and in a patched `Stream.close()` (#5674, #5675). #5645 closed on 2026-09-30, but the openai iterators on master still finish the span only after the loop.\n\nProbe: a `httpx.MockTransport` serves five SSE chunks, and `before_send_transaction` records the span ops before the gen_ai split.\n\n```\n$ python l17_sentry_openai_stream_span_lost_on_early_exit.py\nsentry_sdk 2.71.0 /var/tmp/p9bL17/sentry-python/sentry_sdk/__init__.py\nopenai 3.22.1\nread to the end spans=['gen_ai.chat', 'subprocess', 'subprocess.communicate', 'subprocess.wait', 'subprocess.wait', 'http.client']\nbreak, then stream.close() spans=['http.client']\nwith stream: first chunk only spans=['http.client']\nconnection drops mid-stream spans=['http.client']\n```\n\nThe same probe against `client.responses.create(stream=True)`:\n\n```\n$ python l17_sentry_openai_responses_stream_early_exit.py\nopenai 3.22.1\nread to the end spans=['gen_ai.responses', 'subprocess', 'subprocess.communicate', 'subprocess.wait', 'subprocess.wait', 'http.client']\nbreak, then stream.close() spans=['http.client']\n```\n\n\u003cdetails>\u003csummary>Probe source\u003c/summary>\n\n```python\n\"\"\"An OpenAI chat stream that is not read to the end never finishes its gen_ai.chat span.\"\"\"\nimport json\n\nimport httpx\nimport openai\nimport sentry_sdk\nfrom sentry_sdk.integrations.openai import OpenAIIntegration\n\nprint(\"sentry_sdk\", sentry_sdk.VERSION, sentry_sdk.__file__)\nprint(\"openai\", openai.__version__)\n\n\ndef sse(n_chunks):\n body = \"\"\n for i in range(n_chunks):\n chunk = {\n \"id\": \"c1\", \"object\": \"chat.completion.chunk\", \"created\": 0, \"model\": \"gpt-4o\",\n \"choices\": [{\"index\": 0, \"delta\": {\"content\": f\"tok{i} \"}, \"finish_reason\": None}],\n }\n body += f\"data: {json.dumps(chunk)}\\n\\n\"\n return body + \"data: [DONE]\\n\\n\"\n\n\ntransport = httpx.MockTransport(\n lambda request: httpx.Response(200, headers={\"content-type\": \"text/event-stream\"}, text=sse(5))\n)\nseen = []\n\n\ndef before_send_transaction(event, hint):\n seen.append(event)\n return None\n\n\nsentry_sdk.init(\n dsn=\"https://public@example.com/1\",\n traces_sample_rate=1.0,\n integrations=[OpenAIIntegration()],\n before_send_transaction=before_send_transaction,\n)\nclient = openai.OpenAI(api_key=\"x\", http_client=httpx.Client(transport=transport))\n\n\ndef run(label, consume):\n seen.clear()\n with sentry_sdk.start_transaction(name=label):\n stream = client.chat.completions.create(\n model=\"gpt-4o\", messages=[{\"role\": \"user\", \"content\": \"hi\"}], stream=True\n )\n consume(stream)\n ops = [s[\"op\"] for s in seen[0][\"spans\"]]\n print(f\"{label:30} spans={ops}\")\n\n\ndef read_all(s):\n for _ in s:\n pass\n\n\ndef break_then_close(s):\n for _ in s:\n break\n s.close()\n\n\ndef with_block_first_chunk(s):\n with s:\n next(iter(s))\n\n\nrun(\"read to the end\", read_all)\nrun(\"break, then stream.close()\", break_then_close)\nrun(\"with stream: first chunk only\", with_block_first_chunk)\n\n\nclass BrokenBody(httpx.SyncByteStream):\n def __iter__(self):\n yield sse(2).replace(\"data: [DONE]\\n\\n\", \"\").encode()\n raise httpx.ReadError(\"connection reset\")\n\n\nbroken = openai.OpenAI(\n api_key=\"x\",\n max_retries=0,\n http_client=httpx.Client(\n transport=httpx.MockTransport(\n lambda request: httpx.Response(\n 200, headers={\"content-type\": \"text/event-stream\"}, stream=BrokenBody()\n )\n )\n ),\n)\nseen.clear()\ntry:\n with sentry_sdk.start_transaction(name=\"connection drops mid-stream\"):\n for _ in broken.chat.completions.create(\n model=\"gpt-4o\", messages=[{\"role\": \"user\", \"content\": \"hi\"}], stream=True\n ):\n pass\nexcept openai.APIConnectionError:\n pass\nprint(f\"{'connection drops mid-stream':30} spans={[s['op'] for s in seen[0]['spans']]}\")\n```\n\n\u003c/details>\n\n### Expected Result\n\nA finished `gen_ai.chat` span per call, errored on a connection error.\n\n### Actual Result\n\nNo `gen_ai.chat` span on any of those three paths, and no `gen_ai.responses` span after an early close. The `http.client` span is the only trace of the call.\n\n@alexander-alderman-webb was openai meant to be covered by #5645? If not, would you take a PR that ports both parts to `openai.Stream`, `AsyncStream` and the four iterators?\n","author":{"url":"https://github.com/chrikrah","@type":"Person","name":"chrikrah"},"datePublished":"2026-10-04T05:28:04Z","url":"https://github.com/getsentry/sentry-python/issues/7847"}
| 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:c11f0efe-5fb1-2316-8acf-f3844c2959f2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB6A:A99C2:4405EA:59488F:6B00C7A6 |
| html-safe-nonce | 08569b6067b8092c39e5291399af3107a743352705990c2a8eb49afe25551ce7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjZBOkE5OUMyOjQ0MDVFQTo1OTQ4OEY6NkIwMEM3QTYiLCJ2aXNpdG9yX2lkIjoiNzkxNDEyMTY4MzA0MzE0MTU0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 05480911f7b943b38e6557b80f15b20bccd21c6fab578801c5ade5228ee4e1ba |
| hovercard-subject-tag | issue:5696532984 |
| 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/7847/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cbfaf73263228ad980762a586b843bc34ba033cded4bba92fac17c71e8d00ddf/getsentry/sentry-python/issues/7847 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cbfaf73263228ad980762a586b843bc34ba033cded4bba92fac17c71e8d00ddf/getsentry/sentry-python/issues/7847 |
| og:image:alt | How do you use Sentry? Sentry Saas (sentry.io) Version 2.71.0 (master 8afefe8), openai 3.22.1 Steps to Reproduce _wrap_synchronous_completions_chunk_iterator (sentry_sdk/integrations/openai.py:1001... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrikrah |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width