Skip to content

Commit bb817aa

Browse files
felmononseratch
andauthored
docs: clarify cancel-after-turn streaming guidance (#2710)
Co-authored-by: Kazuhiro Sera <seratch@openai.com>
1 parent c964926 commit bb817aa

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

docs/streaming.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ if result.interruptions:
5555

5656
For a full pause/resume walkthrough, see the [human-in-the-loop guide](human_in_the_loop.md).
5757

58+
## Cancel streaming after the current turn
59+
60+
If you need to stop a streaming run in the middle, call [`result.cancel()`][agents.result.RunResultStreaming.cancel]. By default this stops the run immediately. To let the current turn finish cleanly before stopping, call `result.cancel(mode="after_turn")` instead.
61+
62+
A streamed run is not complete until `result.stream_events()` finishes. The SDK may still be persisting session items, finalizing approval state, or compacting history after the last visible token.
63+
64+
If you are manually continuing from [`result.to_input_list(mode="normalized")`][agents.result.RunResultBase.to_input_list], and `cancel(mode="after_turn")` stops after a tool turn, continue that unfinished turn by rerunning `result.last_agent` with that normalized input instead of appending a fresh user turn right away.
65+
- If a streamed run stopped for tool approval, do not treat that as a new turn. Finish draining the stream, inspect `result.interruptions`, and resume from `result.to_state()` instead.
66+
- Use [`RunConfig.session_input_callback`][agents.run.RunConfig.session_input_callback] to customize how retrieved session history and the new user input are merged before the next model call. If you rewrite new-turn items there, the rewritten version is what gets persisted for that turn.
67+
5868
## Run item events and agent events
5969

6070
[`RunItemStreamEvent`][agents.stream_events.RunItemStreamEvent]s are higher level events. They inform you when an item has been fully generated. This allows you to push progress updates at the level of "message generated", "tool ran", etc, instead of each token. Similarly, [`AgentUpdatedStreamEvent`][agents.stream_events.AgentUpdatedStreamEvent] gives you updates when the current agent changes (e.g. as the result of a handoff).

0 commit comments

Comments
 (0)