Skip to content

fix(streaming): ensure remaining body is consumed after [DONE] in Str… - #3581

Open
vrs-darkness wants to merge 2 commits into
openai:mainfrom
vrs-darkness:fix/drain-stream-after-done-cleanup
Open

fix(streaming): ensure remaining body is consumed after [DONE] in Str…#3581
vrs-darkness wants to merge 2 commits into
openai:mainfrom
vrs-darkness:fix/drain-stream-after-done-cleanup

Conversation

@vrs-darkness

Copy link
Copy Markdown

fix(streaming): best-effort drain after [DONE] for connection reuse

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

When using a connection-pooled OpenAI client, each streaming request was adding ~300ms of overhead. SSE streams break on data: [DONE] without fully reading the remaining response body; closing that partially-read response drops the connection from the pool, so the next request pays for a new TCP/TLS handshake.

After [DONE], drain the remaining iterator with consume_sync_iterator / consume_async_iterator before close so the body can be fully consumed and the pooled connection reused.

Drain is best-effort: httpx.HTTPError during cleanup is swallowed so [DONE] stays terminal for callers even if a proxy leaves trailing heartbeats, the socket errors, or trailing bytes are truncated/malformed.

…eam and AsyncStream

- Added best-effort draining of remaining body bytes after receiving [DONE] to allow connection reuse.
- Implemented error handling to prevent stream failures due to transport errors during draining.
- Introduced tests to validate the behavior for both synchronous and asynchronous streams.
@vrs-darkness
vrs-darkness requested a review from a team as a code owner August 5, 2026 22:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4b47e7bb6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/_streaming.py
Comment thread src/openai/_streaming.py Outdated
…and AsyncStream

- Updated error handling in both Stream and AsyncStream classes to catch UnicodeError in addition to HTTPError during the draining of the stream after receiving [DONE].
- Added a new test to ensure that malformed trailing bytes after [DONE] do not cause failures in already-complete streams for both synchronous and asynchronous scenarios.
@vrs-darkness

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: d59c409388

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Security review completed. No security issues were found in this pull request.

Reviewed commit: d59c409388

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@vrs-darkness

vrs-darkness commented Aug 5, 2026

Copy link
Copy Markdown
Author

@jbeckwith-oai please review PR whenever possible. It is currently a patch in my service would be great if you could check the PR and take it for a release which i could use directly.
Thanks!!

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