Skip to content

fix(openai): finish streamed spans on iteration errors - #7966

Closed
intertermux-code wants to merge 1 commit into
getsentry:masterfrom
intertermux-code:fix/7847-openai-stream-spans
Closed

intertermux-code wants to merge 1 commit into
getsentry:masterfrom
intertermux-code:fix/7847-openai-stream-spans

Conversation

@intertermux-code

Copy link
Copy Markdown
Contributor

OpenAI streamed spans were lost when an error was raised mid-iteration: the span was never finished, so the trace showed nothing.

Fix: try/finally in all 4 streaming iterators (span finished with sys.exc_info()) + patched Stream.close/AsyncStream.close to close the wrapped generator first. 16 new regression tests (4 tests x 2 span modes).

Repro: all 5 loss modes LOST before, ALL OK after. pytest tests/integrations/openai/: 713 passed. Ruff clean, mypy shows only pre-existing errors.

Fixes #7847.

@intertermux-code
intertermux-code requested a review from a team as a code owner October 12, 2026 14:29
@alexander-alderman-webb

Copy link
Copy Markdown
Collaborator

Closing this because we want to mirror the approach in the anthropic integration, which this doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI integration: streamed gen_ai.chat/gen_ai.responses span is lost when the stream is closed early or errors

2 participants