Skip to content

fix: properly close stream iterator on response abort (#3561) - #3621

Open
SparshGarg999 wants to merge 1 commit into
openai:mainfrom
SparshGarg999:fix-response-stream-abort-tool-call-3561
Open

fix: properly close stream iterator on response abort (#3561)#3621
SparshGarg999 wants to merge 1 commit into
openai:mainfrom
SparshGarg999:fix-response-stream-abort-tool-call-3561

Conversation

@SparshGarg999

@SparshGarg999 SparshGarg999 commented Aug 15, 2026

Copy link
Copy Markdown

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

  • Updated the response stream async context manager in src/openai/_base_client.py to trigger an explicit aclose() on the underlying stream response generator whenever the consumer exits the loop early or aborts.

Testing & Verification

  • Tested early stream cancellation during active streaming tool calls, verifying clean resource disposal and prevention of generator leak warnings.

@SparshGarg999
SparshGarg999 requested a review from a team as a code owner August 15, 2026 03:09

@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: 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".

Comment thread src/openai/_streaming.py
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

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.

Aborting a streaming Responses API request drops the function_call item, breaking the next turn with 400 "No tool call found for function call output"

1 participant