Skip to content

Allow cancel out of the streaming result #579

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

Merged
merged 5 commits into from
Apr 23, 2025

Conversation

handrew
Copy link
Contributor

@handrew handrew commented Apr 23, 2025

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

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())

Copy link
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@handrew looks good! You can add unit tests to the tests/ directory. If you use codex/cursor/windsurf/chatgpt, they can suggest tests and probably even help you write them. You can run the tests locally via make tests

@@ -174,6 +185,7 @@ async def stream_events(self) -> AsyncIterator[StreamEvent]:
try:
item = await self._event_queue.get()
except asyncio.CancelledError:
self.cancel() # Ensure tasks are cleaned up if the coroutine is cancelled
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary? cleanup still occurs outside of the loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose not, though the AI thought it was 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this

@handrew
Copy link
Contributor Author

handrew commented Apr 23, 2025

Thanks for the tip @rm-openai , I'll be sure to do that in the future, pretty neat. Anyway, added the test!

@rm-openai
Copy link
Collaborator

@handrew Test looks good, but it doesn't look like the test is passing - seems like its an infinite loop. You may need to use FakeModel, since there's no network access in unit tests.

Also lint is failing - could you fix plz?

@handrew
Copy link
Contributor Author

handrew commented Apr 23, 2025

@handrew Test looks good, but it doesn't look like the test is passing - seems like its an infinite loop. You may need to use FakeModel, since there's no network access in unit tests.

Also lint is failing - could you fix plz?

Used FakeModel, good call. It only yields two events so I test cancelling after one. Also linted.

Thanks @rm-openai , this was fun. Any idea when I might be able to --upgrade and get this fix :)?

@rm-openai
Copy link
Collaborator

@handrew I'm merging the PR now. Will probably cut a new release tomorrow morning, so should be able to upgrade then.

You can also temporarily directly install from the latest code in this repo if you want, which will include your change

pip uninstall openai-agents
pip install git+https://github.com/openai/openai-agents-python.git

@rm-openai rm-openai merged commit a113fea into openai:main Apr 23, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants