Skip to content

feat: add turn_id to Event for grouping streaming chunks by LLM call#4822

Open
ferponse wants to merge 3 commits intogoogle:mainfrom
ferponse:feat/add-turn-id-to-streaming-events
Open

feat: add turn_id to Event for grouping streaming chunks by LLM call#4822
ferponse wants to merge 3 commits intogoogle:mainfrom
ferponse:feat/add-turn-id-to-streaming-events

Conversation

@ferponse
Copy link

Closes #4820

Summary

  • Adds an optional turn_id: int field to Event that groups all streaming chunks belonging to the same LLM call
  • turn_id is a 1-based counter that increments with each LLM call inside run_async, making it easy to identify turn boundaries (turn 1, turn 2, …)
  • Consumers can use this field to trivially separate streaming chunks into message bubbles without brittle heuristics based on event type transitions

Problem

When using runner.run_async() with StreamingMode.SSE, there is no way to distinguish which partial streaming chunks belong to which LLM response turn. The invocation_id is shared across all events in the invocation, and id changes 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 on Event:

  • 1-based counter incremented with each LLM call in run_async
  • Remains the same across all partial chunks and the final aggregated event of that call
  • Increments when a new LLM call starts (next iteration of the while True loop)
  • Scoped to the SSE streaming flow only; BIDI (live) flow is not affected
  • None by default, so existing code is unaffected

Changes

File Change
src/google/adk/events/event.py Added turn_id: Optional[int] field with docstring
src/google/adk/flows/llm_flows/base_llm_flow.py Counter in run_async loop, passed to _run_one_step_async as Optional[int] = None
tests/unittests/flows/llm_flows/test_turn_id.py 3 new tests with proper test isolation (Field(default_factory=...))

Test plan

  • test_partial_chunks_share_same_turn_id — partial chunks from one LLM call share turn_id=1
  • test_turn_id_present_on_final_response — a single final response carries turn_id=1
  • test_different_llm_calls_get_different_turn_ids — events from separate LLM calls (text → tool → text) get turn_id 1 and 2
  • All 108 existing flow + runner tests pass unchanged

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).
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 turn_id to the Event object. This new identifier provides a robust mechanism for grouping related streaming chunks from a single LLM call, addressing the previous difficulty in distinguishing which partial responses belonged to which turn in a multi-turn conversation. The change improves the clarity and usability of streaming data for downstream consumers, making it simpler to reconstruct coherent responses.

Highlights

  • New turn_id field: An optional turn_id: int field has been added to the Event class to group all streaming chunks that originate from the same LLM call within a single invocation.
  • Turn ID Logic: The turn_id is a 1-based counter that increments with each distinct LLM call made within the run_async method, providing a clear identifier for 'turns' in a multi-step LLM interaction.
  • Improved Streaming Chunk Grouping: This feature allows consumers to easily separate streaming chunks into distinct message bubbles without relying on fragile heuristic-based event type transitions.
  • Backward Compatibility: The turn_id defaults to None, ensuring that existing codebases remain unaffected by this change.

🧠 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
  • src/google/adk/events/event.py
    • Added turn_id: Optional[int] field with comprehensive docstring.
  • src/google/adk/flows/llm_flows/base_llm_flow.py
    • Initialized and incremented turn_id within the run_async loop.
    • Modified _run_one_step_async to accept turn_id as an optional keyword argument.
    • Passed the generated turn_id to the Event constructor during event creation.
  • tests/unittests/flows/llm_flows/test_turn_id.py
    • Added a new test file to validate the turn_id functionality.
    • Included tests for partial chunks sharing the same turn_id.
    • Verified that the final response event carries a turn_id.
    • Confirmed that different LLM calls (e.g., text -> tool -> text) receive distinct turn_id values.
Activity
  • The pull request was created by ferponse.
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Mar 13, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

@rohityan rohityan self-assigned this Mar 13, 2026
@rohityan
Copy link
Collaborator

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.

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a turn/response group identifier to streaming events

3 participants