Skip to content

Streaming is blocking and cannot be cancelled #2222

Closed

Description

@varunursekar

Describe the bug

RunResultStreamed.stream_events() cannot be cancelled using asyncio primitives. In the script below, we'd like to time out if a streaming event takes longer than 5 seconds to arrive. However, using asyncio.wait_for's cancellation event is not propagated to the stream_events iterator.

Debug information

  • Agents SDK version: (e.g. v0.0.3)
  • Python version (e.g. Python 3.10)

Repro steps

from agents import Agent, Runner, function_tool
import asyncio
import datetime


@function_tool
async def get_secret_key():
    await asyncio.sleep(120)
    return "secret_key"


async def main():
    agent = Agent(
        name="secret_key_agent",
        model="gpt-4.1-mini",
        tools=[get_secret_key],
    )

    result = Runner.run_streamed(agent, input="What is the secret key?")
    event_iter = aiter(result.stream_events())

    while True:
        try:
            event = await asyncio.wait_for(anext(event_iter), timeout=5)
            print(datetime.datetime.now(), event)
        except StopAsyncIteration:
            break
        except asyncio.TimeoutError:
            print("Timed out waiting for next event")
            break  # or continue if you want to keep waiting

if __name__ == "__main__":
    asyncio.run(main())

Expected behavior

The iterator should respond as expected to asyncio cancellations. The loop above should time out.

Activity

  1. added
    bug
    on Dec 22, 2025
  2. ihower commented on Dec 23, 2025

    @ihower
    Contributor

    @varunursekar I’m not sure whether this is a bug or expected behavior, but currently if the caller times out, the SDK won’t cancel streaming immediately. It “safely” waits for any background task to finish (see _await_task_safely function).

    If you need streaming timeouts today, the best approach seems to be implementing your own timer and call result.cancel() directly. It supports "immediate" and "after_turn". See: #1896 and #1895

  3. added
    enhancement
    and removed
    bug
    on Dec 23, 2025
  4. added a commit that references this issue on Dec 23, 2025
    b3b5bcb
  5. seratch commented on Dec 23, 2025

    @seratch
    Member

    This use case was not supported by design, but enabling to use the timeout this way may be useful for other developers too. My pull request #2226 may resolve it. Feedback is welcome!

  6. added this to the 0.7.x milestone on Dec 23, 2025
  7. added a commit that references this issue on Jan 6, 2026
    fd65d1a
  8. modified the milestones: 0.7.x, 0.6.x on Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeature:core

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions