Skip to content

Canceling the stream from result.stream_events() #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
handrew opened this issue Apr 23, 2025 · 5 comments
Closed

Canceling the stream from result.stream_events() #574

handrew opened this issue Apr 23, 2025 · 5 comments
Labels
enhancement New feature or request

Comments

@handrew
Copy link
Contributor

handrew commented Apr 23, 2025

Below is the example from the docs. I'm trying to break out of the result.stream_events() async for. I can break out, but the agent will continue to take actions. It would be great if there was some way for us to stop the events entirely, because the agent will continue wreaking havoc on my things.

import asyncio
from openai.types.responses import ResponseTextDeltaEvent
from agents import Agent, Runner

async def main():
    agent = Agent(
        name="Joker",
        instructions="You are a helpful assistant.",
    )

    result = Runner.run_streamed(agent, input="Please tell me 5 jokes.")
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            print(event.data.delta, end="", flush=True)


if __name__ == "__main__":
    asyncio.run(main())
@handrew handrew added the enhancement New feature or request label Apr 23, 2025
@rm-openai
Copy link
Collaborator

Yeah makes sense, we should add this

@handrew
Copy link
Contributor Author

handrew commented Apr 23, 2025

@rm-openai Any way I can help?

@rm-openai
Copy link
Collaborator

@handrew PR would be great! Should be able to add cancel() to RunResultStreaming and just cancel all the asyncio.tasks. Some tests would be great too

@handrew
Copy link
Contributor Author

handrew commented Apr 23, 2025

@rm-openai Alrighty I gave it a shot. I'm a bit of a noob to OSS contribution so please bear with me. #579

rm-openai pushed a commit that referenced this issue Apr 23, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Fix for #574 

@rm-openai I'm not sure how to add a test within the repo but I have
pasted a test script below that seems to work

```python
import asyncio
from openai.types.responses import ResponseTextDeltaEvent
from agents import Agent, Runner

async def main():
    agent = Agent(
        name="Joker",
        instructions="You are a helpful assistant.",
    )

    result = Runner.run_streamed(agent, input="Please tell me 5 jokes.")
    num_visible_event = 0
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            print(event.data.delta, end="", flush=True)
            num_visible_event += 1
            print(num_visible_event)
            if num_visible_event == 3:
                result.cancel()


if __name__ == "__main__":
    asyncio.run(main())
````
@handrew
Copy link
Contributor Author

handrew commented Apr 23, 2025

Thanks @rm-openai !

@handrew handrew closed this as completed Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants