feat: add turn_id to Event for grouping streaming chunks by LLM call#4817
feat: add turn_id to Event for grouping streaming chunks by LLM call#4817ferponse wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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 introduces a significant enhancement to how streaming events from LLM calls are managed and consumed. By adding a 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
|
|
Response from ADK Triaging Agent Hello @ferponse, thank you for your contribution! It looks like the Contributor License Agreement (CLA) check has failed. Before we can merge this pull request, you'll need to sign the CLA. You can find more information and sign the agreement here: https://cla.developers.google.com/ Once you've signed, the check should automatically update. Thanks! |
There was a problem hiding this comment.
Code Review
This pull request introduces a turn_id to the Event class, which is a great addition for consumers of streaming events to group chunks belonging to the same LLM call. The implementation is clean, and the new logic is well-contained within the SSE streaming flow (run_async). The inclusion of comprehensive unit tests is also appreciated. I have one minor suggestion to improve the type hint for turn_id for better consistency and robustness.
| self, | ||
| invocation_context: InvocationContext, | ||
| *, | ||
| turn_id: int = 0, |
There was a problem hiding this comment.
When using StreamingMode.SSE, all partial chunks from the same LLM call now share a stable turn_id (1-based integer counter). This allows consumers to trivially group streaming chunks by turn without fragile heuristics based on event type transitions. The invocation_id groups all events in a single agent invocation, while id changes on every yield. The new turn_id sits in between: it stays constant across all events produced by one LLM call and increments when a new call starts (e.g. after tool execution).
4444916 to
efa7a2b
Compare
|
Closing in favour of a new PR with review feedback applied. |
Summary
turn_id: intfield toEventthat groups all streaming chunks belonging to the same LLM callturn_idis a 1-based counter that increments with each LLM call insiderun_async, making it easy to identify turn boundaries (turn 1, turn 2, …)Problem
When using
runner.run_async()withStreamingMode.SSE, there is no way to distinguish which partial streaming chunks belong to which LLM response turn. Theinvocation_idis shared across all events in the invocation, andidchanges on every yield. The only workaround is observing transition patterns (partial → function_call → function_response), which is fragile.Solution
A new
turn_id: Optional[int]field onEvent:run_asyncwhile Trueloop)Noneby default, so existing code is unaffectedChanges
src/google/adk/events/event.pyturn_id: Optional[int]field with docstringsrc/google/adk/flows/llm_flows/base_llm_flow.pyrun_asyncloop, passed to_run_one_step_asynctests/unittests/flows/llm_flows/test_turn_id.pyTest plan
test_partial_chunks_share_same_turn_id— partial chunks from one LLM call shareturn_id=1test_turn_id_present_on_final_response— a single final response carriesturn_id=1test_different_llm_calls_get_different_turn_ids— events from separate LLM calls (text → tool → text) getturn_id1 and 2