Skip to content

fix(openai): finalize synchronous chat streams on iteration errors - #1476

Open
sorenjing wants to merge 1 commit into
AgentOps-AI:mainfrom
sorenjing:fix/openai-sync-stream-error-cleanup
Open

sorenjing wants to merge 1 commit into
AgentOps-AI:mainfrom
sorenjing:fix/openai-sync-stream-error-cleanup

Conversation

@sorenjing

Copy link
Copy Markdown

📥 Pull Request

📘 Description

When an OpenAI synchronous chat stream raises during iteration, OpenaiStreamWrapper.__next__ propagates the exception but leaves its span recording and attached. The failed call is not exported, and subsequent operations can inherit it as their parent.

Record the exception and ERROR status before re-raising, and share idempotent span/context cleanup across iteration failure, normal exhaustion and context-manager exit. This also prevents repeated end() and context detachment when an exhausted stream exits a with block. An ended wrapper consistently raises StopIteration on subsequent reads.

The change is limited to the synchronous chat wrapper and its tests, based directly on main. It preserves the original exception and yielded chunk objects. Async streams, Responses API wrappers and underlying HTTP stream closure are outside this change.

🧪 Testing

  • Seven new lifecycle cases use an isolated real OpenTelemetry exporter, real OpenAI SDK and synthetic httpx.MockTransport responses. They cover successful/error iteration with and without a manager, catching an error inside a manager, exception/chunk identity, caller exceptions, and repeated reads after termination. On unchanged main: 6 failures, 1 pass; with this fix: 7 pass.
  • Full tests/unit with coverage, Python 3.12.13 / Windows and complete test/dev groups from uv.lock: 495 passed, 1 existing skip, with no test exclusions. OpenAI 1.71.0, OpenTelemetry SDK 1.31.1 and pytest 8.3.5.
  • On Windows the local test launcher uses stdlib pdb and disables optional readline, bypassing a legacy pdbpp/pyreadline startup incompatibility. No production code or installed dependency files are changed by the launcher. The suite still emits closed-log-stream messages during process shutdown.
  • Ruff 0.12.9 lint and formatting checks and git diff --check pass.

Real provider requests, Linux CI and the remaining Python-version matrix have not been run locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-30T15:42:05.608589Z
We couldn’t complete this review. Comment @codex review to try again.
2d0c274 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant