feat(responses): add cancel() support for streaming responses#2916
feat(responses): add cancel() support for streaming responses#2916giulio-leone wants to merge 6 commits intoopenai:mainfrom
Conversation
Adds response_id property and cancel() method to ResponseStream and AsyncResponseStream, allowing users to cancel a response mid-stream. Uses a callback pattern to avoid coupling stream classes to the Responses resource. Refs: openai#2643
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b77dd8cfd6
ℹ️ 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".
There was a problem hiding this comment.
Pull request overview
Adds first-class cancellation support for streaming Responses by exposing response_id and a new cancel() API on both sync and async streaming helpers, wired through the Responses.stream() / AsyncResponses.stream() managers.
Changes:
- Add
response_idproperty onResponseStream/AsyncResponseStream(backed by a newResponseStreamState.current_snapshotaccessor). - Add
cancel()/async cancel()on stream objects that invokes the Responses cancel endpoint via a passed callback and closes the stream. - Add unit tests covering
response_id, cancel preconditions, callback invocation, and snapshot exposure.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/openai/lib/streaming/responses/_responses.py |
Adds cancel callback plumbing, response_id, cancel() methods, and exposes current_snapshot. |
src/openai/resources/responses/responses.py |
Wires self.cancel into stream managers so streaming objects can call the cancel endpoint. |
tests/lib/responses/test_response_stream_cancel.py |
Adds unit tests for cancellation behavior and snapshot/ID availability. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Break long ResponseStreamManager return into multi-line (ruff line-length) - Close stream before issuing cancel API call to release the HTTP connection - Prevents connection pool deadlock with constrained clients (e.g. max_connections=1) - Applied to both sync ResponseStream.cancel() and async AsyncResponseStream.cancel() Refs: openai#2916
Use try/finally so that stream.close() always runs even if the cancel API call raises an exception. Applies to both sync and async paths. Refs: openai#2916
111faaa to
aad7295
Compare
… stream before cancel
Remove the pre-try await self.close() that would close the stream before the cancel API call, and keep only the finally block to ensure close() is always called regardless of cancel API success/failure.
Summary
Adds the ability to cancel streaming responses mid-stream via a new
cancel()method onResponseStreamandAsyncResponseStream.Fixes #2643
Changes
response_idproperty toResponseStreamandAsyncResponseStream— returns the response ID from the accumulated snapshot (available after the firstresponse.createdevent)cancel()method toResponseStreamandasync cancel()toAsyncResponseStream— calls the cancel API endpoint and closes the streamcurrent_snapshotproperty toResponseStreamStateto cleanly expose the internal snapshotcancel_responsecallback fromResponses.stream()andAsyncResponses.stream()to the stream managers and stream classes, using a callback pattern to avoid coupling stream classes to the resource layerresponse_idavailability, error cases, callback invocation, and stream closureUsage
Backwards Compatibility
Nonedefaults — fully backwards compatible