feat: add turn_id to Event for grouping streaming chunks by LLM call#4822
feat: add turn_id to Event for grouping streaming chunks by LLM call#4822ferponse wants to merge 3 commits intogoogle:mainfrom
Conversation
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).
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 are handled 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a turn_id to the Event class to group streaming chunks from the same LLM call. The implementation correctly adds the field and passes it through the flow. The new tests cover the main scenarios for this feature.
My main feedback is regarding the turn_id increment logic in run_async. It currently increments for every step, including tool execution steps, which will lead to non-consecutive turn_ids for LLM calls. I've left a specific comment with a suggestion on how to address this to ensure turn_ids are consecutive as intended.
|
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. You can use autoformat.sh to fix the formatting errors. |
Closes #4820
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_asyncasOptional[int] = Nonetests/unittests/flows/llm_flows/test_turn_id.pyField(default_factory=...))Test 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