René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:c11f0efe-5fb1-2316-8acf-f3844c2959f2
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAB6A:A99C2:4405EA:59488F:6B00C7A6
html-safe-nonce08569b6067b8092c39e5291399af3107a743352705990c2a8eb49afe25551ce7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjZBOkE5OUMyOjQ0MDVFQTo1OTQ4OEY6NkIwMEM3QTYiLCJ2aXNpdG9yX2lkIjoiNzkxNDEyMTY4MzA0MzE0MTU0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac05480911f7b943b38e6557b80f15b20bccd21c6fab578801c5ade5228ee4e1ba
hovercard-subject-tagissue:5696532984
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-python/7847/issue_layout
twitter:imagehttps://opengraph.githubassets.com/cbfaf73263228ad980762a586b843bc34ba033cded4bba92fac17c71e8d00ddf/getsentry/sentry-python/issues/7847
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/cbfaf73263228ad980762a586b843bc34ba033cded4bba92fac17c71e8d00ddf/getsentry/sentry-python/issues/7847
og:image:altHow 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamechrikrah
hostnamegithub.com
expected-hostnamegithub.com
None98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4
turbo-cache-controlno-preview
go-importgithub.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git
octolytics-dimension-user_id1396951
octolytics-dimension-user_logingetsentry
octolytics-dimension-repository_id137814779
octolytics-dimension-repository_nwogetsentry/sentry-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id137814779
octolytics-dimension-repository_network_root_nwogetsentry/sentry-python
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseef013399cf0a3c324bea644a685d7c1e0f06b5b7
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/getsentry/sentry-python/issues/7847#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fissues%2F7847
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fissues%2F7847
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=getsentry%2Fsentry-python
Reloadhttps://github.com/getsentry/sentry-python/issues/7847
Reloadhttps://github.com/getsentry/sentry-python/issues/7847
Reloadhttps://github.com/getsentry/sentry-python/issues/7847
getsentryhttps://github.com/getsentry
sentry-pythonhttps://github.com/getsentry/sentry-python
Codehttps://github.com/getsentry/sentry-python
Issues345 (345)https://github.com/getsentry/sentry-python/issues
Pull requests42 (42)https://github.com/getsentry/sentry-python/pulls
Discussionshttps://github.com/getsentry/sentry-python/discussions
Actionshttps://github.com/getsentry/sentry-python/actions
Security and quality2 (2)https://github.com/getsentry/sentry-python/security
Insightshttps://github.com/getsentry/sentry-python/insights
https://github.com/getsentry/sentry-python
New issuehttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/7847
New issuehttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/7847
OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errorshttps://github.com/getsentry/sentry-python/issues/7847#top
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
Spanshttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Spans%22
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
https://github.com/chrikrah
chrikrahhttps://github.com/chrikrah
on Oct 4, 2026https://github.com/getsentry/sentry-python/issues/7847#issue-5696532984
#5643https://github.com/getsentry/sentry-python/pull/5643
#5674https://github.com/getsentry/sentry-python/pull/5674
#5675https://github.com/getsentry/sentry-python/pull/5675
#5645https://github.com/getsentry/sentry-python/issues/5645
@alexander-alderman-webbhttps://github.com/alexander-alderman-webb
#5645https://github.com/getsentry/sentry-python/issues/5645
getsantryhttps://github.com/apps/getsantry
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
on Oct 4, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32429625727
https://github.com/apps/linear-code
linear-codehttps://github.com/apps/linear-code
on Oct 4, 2026https://github.com/getsentry/sentry-python/issues/7847#issuecomment-5976931256
Linear Codehttps://linear.app
PY-2925https://linear.app/getsentry/issue/PY-2925
getsantryhttps://github.com/apps/getsantry
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
on Oct 4, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32447185532
linear-codehttps://github.com/apps/linear-code
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
Spanshttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Spans%22
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
on Oct 4, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32429635925
intertermux-codehttps://github.com/intertermux-code
on Oct 5, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32480302954
fix(openai): finish streaming gen_ai span on early stream closehttps://github.com/intertermux-code/sentry-python/commit/d0064c61e7362eedb4671499dcec9663bd353efe
https://docs.github.com/articles/closing-issues-via-commit-messages
d0064c6https://github.com/intertermux-code/sentry-python/commit/d0064c61e7362eedb4671499dcec9663bd353efe
fix(openai): finish streaming gen_ai span on early stream closehttps://github.com/intertermux-code/sentry-python/commit/66051721cc911df61daeb5c1342c2332fb9cf9cb
https://docs.github.com/articles/closing-issues-via-commit-messages
6605172https://github.com/intertermux-code/sentry-python/commit/66051721cc911df61daeb5c1342c2332fb9cf9cb
https://github.com/ericapisani
ericapisanihttps://github.com/ericapisani
on Oct 5, 2026https://github.com/getsentry/sentry-python/issues/7847#issuecomment-5996469987
@chrikrahhttps://github.com/chrikrah
@alexander-alderman-webbhttps://github.com/alexander-alderman-webb
getsantryhttps://github.com/apps/getsantry
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
on Oct 5, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32516928895
https://github.com/chrikrah
chrikrahhttps://github.com/chrikrah
on Oct 11, 2026https://github.com/getsentry/sentry-python/issues/7847#issuecomment-6107478321
getsantryhttps://github.com/apps/getsantry
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
on Oct 11, 2026https://github.com/getsentry/sentry-python/issues/7847#event-32979015556
Sign up for freehttps://github.com/signup?return_to=https://github.com/getsentry/sentry-python/issues/7847
Sign in to commenthttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/7847
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
Spanshttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Spans%22
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.