feat: add turn_id to Event for grouping streaming chunks by LLM call#4815
feat: add turn_id to Event for grouping streaming chunks by LLM call#4815ferponse wants to merge 1 commit into
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 Large Language Models (LLMs) 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 creating this PR! Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). You can visit https://cla.developers.google.com/ to see your current agreements or to sign a new one. This information will help reviewers to review your PR more efficiently. 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 grouping streaming chunks from a single LLM call. The implementation for the non-live run_async flow looks correct. However, the implementation for the live streaming (run_live) flow has a critical issue where a new turn_id is generated for every chunk, which defeats the purpose of this feature. I've left a specific comment with a suggested fix. Additionally, the new tests only cover the run_async flow; adding tests for the run_live flow would be beneficial to prevent such issues.
| id=Event.new_id(), | ||
| invocation_id=invocation_context.invocation_id, | ||
| author=get_author_for_event(llm_response), | ||
| turn_id=Event.new_id(), |
There was a problem hiding this comment.
In the live streaming flow (_receive_from_model), a new turn_id is generated for every event chunk. This prevents grouping chunks that belong to the same LLM turn. The turn_id should remain constant for all events within a single turn.
To fix this, the turn_id should be managed within the scope of _receive_from_model. It should be initialized once before the event-receiving loop and only regenerated after a turn is complete (i.e., when an event with llm_response.turn_complete=True is received).
Example of the logic:
# Inside _receive_from_model
turn_id = Event.new_id()
try:
while True:
async with Aclosing(llm_connection.receive()) as agen:
async for llm_response in agen:
# ...
model_response_event = Event(
# ...,
turn_id=turn_id, # Use the same turn_id for all chunks
)
# ... (process and yield event)
# ...
if llm_response.turn_complete:
turn_id = Event.new_id() # Generate new id for the next turnAdditionally, please consider adding a unit test for the live streaming scenario to verify this behavior.
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).
ed704ea to
dc971b9
Compare
|
Closing in favour of a new PR with improved implementation (turn_id as integer counter instead of UUID). |
Summary
turn_idfield toEventthat groups all streaming chunks belonging to the same LLM callturn_id, whileidchanges on every yieldProblem
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[str]field onEvent:_run_one_step_asyncwhile Trueloop inrun_async)Noneby default, so existing code is unaffectedChanges
src/google/adk/events/event.pyturn_idfield with docstringsrc/google/adk/flows/llm_flows/base_llm_flow.pyturn_idwhen creatingmodel_response_event(SSE + BIDI flows)tests/unittests/flows/llm_flows/test_turn_id.pyTest plan
test_partial_chunks_share_same_turn_id— partial chunks from one LLM call share the sameturn_idtest_turn_id_present_on_final_response— a single final response carries aturn_idtest_different_llm_calls_get_different_turn_ids— events from separate LLM calls (text → tool → text) have differentturn_ids