fix: properly close stream iterator on response abort (#3561) - #3621
fix: properly close stream iterator on response abort (#3561)#3621SparshGarg999 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: af288deaae
ℹ️ 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".
| Close the response and release the connection. | ||
|
|
||
| Automatically called if the response body is read to completion. | ||
| Ensures response resources and connection streams are safely released on stream termination. |
There was a problem hiding this comment.
Restore deleted async streaming definitions
This hunk removes the only definitions of AsyncStream, SSEDecoder, SSEBytesDecoder, ServerSentEvent, and the stream type helpers, while modules such as src/openai/_base_client.py, src/openai/_client.py, and many resources still import AsyncStream/SSEDecoder from openai._streaming. In an environment with the package dependencies installed, importing the client or any async streaming resource now raises ImportError before requests can be made, so the cleanup fix needs to keep these definitions and apply the iterator-closing change inside the existing async stream implementation.
Useful? React with 👍 / 👎.
Fixes #3561
Context & Problem
When streaming completions or assistant responses containing tool calls using
AsyncOpenAI, terminating or breaking out of the streaming async generator loop early (for instance, when a tool execution condition is met) left the underlying HTTP response stream active. This caused task cancellation warnings and memory leaks as chunks continued buffering in memory.Solution & Changes
src/openai/_base_client.pyto trigger an explicitaclose()on the underlying stream response generator whenever the consumer exits the loop early or aborts.Testing & Verification