feat: add stop_event to cancel SSE streaming mid-generation#4825
feat: add stop_event to cancel SSE streaming mid-generation#4825ferponse wants to merge 2 commits intogoogle:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical need for controlled termination of streaming operations in the system. By integrating an Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a valuable feature for cancelling SSE streaming via an asyncio.Event. The implementation is clean and logically sound, with checks correctly placed to stop generation both between and during LLM calls. The addition of comprehensive unit tests is also a great inclusion. I have a couple of minor suggestions to improve code readability and consistency, and one for improving the robustness of a test case.
Adds an optional asyncio.Event-based cancellation mechanism that allows consumers to stop SSE streaming mid-generation. When stop_event.set() is called, the flow stops yielding new chunks and returns cleanly. This enables "stop generating" buttons in chat UIs: the consumer passes an asyncio.Event to runner.run_async(), and sets it when the user clicks stop.
21382a6 to
1b80c1f
Compare
|
Hi @ferponse , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please fix the failing unit tests and formatting errors before we can proceed with a review. |
Closes #4823
Summary
stop_event: asyncio.Eventparameter torunner.run_async()for cancelling SSE streaming mid-generationstop_event.set()is called, the flow stops yielding new chunks and returns cleanlyProblem
When using
runner.run_async()withStreamingMode.SSE, there is no clean way for consumers to stop generation mid-stream. Task cancellation is a hard stop that may not clean up properly. Breaking out of theasync forloop triggersaclose()but doesn't give the flow a chance to return gracefully.Solution
An
asyncio.Event-based cancellation mechanism:asyncio.Eventand passes it torunner.run_async(stop_event=...)InvocationContext.stop_eventstop_event.is_set()at two points:run_async(prevents starting a new turn)_run_one_step_async(stops mid-stream)Usage
Changes
src/google/adk/agents/invocation_context.pystop_event: Optional[asyncio.Event]fieldsrc/google/adk/runners.pystop_eventparameter torun_async(), propagated toInvocationContextsrc/google/adk/flows/llm_flows/base_llm_flow.pystop_eventbefore each LLM call and before yielding each chunktests/unittests/flows/llm_flows/test_stop_event.pyTest plan
test_stop_event_stops_streaming_mid_chunks— setting stop_event mid-stream prevents further chunkstest_stop_event_not_set_yields_all_chunks— providing stop_event without setting it yields all chunks normallytest_stop_event_prevents_next_llm_call— setting stop_event between LLM calls prevents the next calltest_no_stop_event_works_normally— not providing stop_event works exactly as before