---
type: email
from: Hassan Abu Alhaj <notifications@github.com>
subject: [openai/openai-agents-python] Add graceful cancel mode for streaming

 runs (PR #1896)
received: 2026-03-06 08:03:53
date_header: Tue, 14 Oct 2025 08:32:55 -0700
priority: normal
status: awaiting_approval
uid: 32555
---

## Email Content

**From:** Hassan Abu Alhaj <notifications@github.com>
**Subject:** [openai/openai-agents-python] Add graceful cancel mode for streaming

 runs (PR #1896)
**Date:** Tue, 14 Oct 2025 08:32:55 -0700

### Body (preview)

Resolves #1895



Solves the problem where canceling mid-turn discards partial work by adding an optional mode that waits for turn completion.



**Implementation**



API change (backward compatible):

```python

def cancel(self, mode: Literal[&quot;immediate&quot;, &quot;after_turn&quot;] = &quot;immediate&quot;)

```

Default is &quot;immediate&quot; (existing behavior). New &quot;after_turn&quot; mode sets a flag and lets the background task finish the current turn.



**Core changes:**

- result.py: Add _cancel_mode field and mode parameter to cancel()

- run.py: Add 3 checkpoint checks in streaming loop at top of turn loop, after handoff, and after tool execution

  

Checkpoints are placed after session saves, so when they trigger, the turn is already persisted.



**How it works:**

1. User calls cancel(mode=&quot;after_turn&quot;) - sets flag, doesn&#39;t clean up

2. Background task checks flag at next checkpoint

3. Marks complete and exits loop (cleanup happens in finally block as usual)

You can view, comment on, or merge this pull request online at:



  https://github.com/openai/openai-agents-python/pull/1896



-- Commit Summary --



  * feat: implement soft cancel mode for streaming runs



-- File Changes --



    M src/agents/result.py (59)

    M src/agents/run.py (18)

    M tests/test_cancel_streaming.py (17)

    A tests/test_soft_cancel.py (419)



-- Patch Links --



https://github.com/openai/openai-agents-python/pull/1896.patch

https://github.com/openai/openai-agents-python/pull/1896.diff



-- 

Reply to this email directly or view it on GitHub:

https://github.com/openai/openai-agents-python/pull/1896

You are receiving this because you are subscribed to this thread.



Message ID: &lt;openai/openai-agents-python/pull/1896@github.com&gt;

---

## Suggested Actions
- [ ] Reply
- [ ] Archive
- [ ] Escalate
