Skip to content

VertexAiSessionService.get_session replaces Event.id with the Vertex resource name, discarding the persisted raw_event.id #6530

Closed

Description

@ashubham

Description

When a session is loaded via VertexAiSessionService.get_session, each returned Event.id is set to the Vertex event resource name (api_event_obj.name.split('/')[-1], a server-assigned numeric id) instead of the original ADK Event.id that was generated during the run and streamed to the caller.

The original id is still persisted — it's inside the raw_event blob — but _from_api_event deep-copies raw_event and then overwrites the id before validating. So the same logical event ends up with two different ids depending on how you observe it:

Observed via Event.id
live run / streaming (run_async, :query/streamQuery) the ADK Event.id (e.g. a UUID like c452feb0-b9ad-48b1-b863-c7dd2f085378)
get_session (reload) the Vertex resource name (e.g. 658731057016733696)

Where

src/google/adk/sessions/vertex_ai_session_service.py, _from_api_event — the raw_event branch (and the same in the fallback branch):

raw_event_dict = _get_raw_event(api_event_obj)   # contains the original ADK id
if raw_event_dict:
    event_dict = copy.deepcopy(raw_event_dict)
    event_dict.update({
        'id': api_event_obj.name.split('/')[-1],  # <-- overwrites the original id
        'invocation_id': getattr(api_event_obj, 'invocation_id', None),
        'author': getattr(api_event_obj, 'author', None),
    })
    return Event.model_validate(event_dict)

raw_event_dict['id'] holds the original id and is discarded. Confirmed on main and on the released 1.31.1.

Impact

Any feature that correlates a streamed event with its persisted form by Event.id breaks, because the two id-spaces don't reconcile.

Our concrete case: a resumable SSE layer keys a per-event resume cursor on Event.id. After a page reload we render history from get_session and reconnect using the last rendered event's id as the resume cursor. Because the reloaded id (numeric) is in a different id-space than the live stream (UUID), the cursor can't be resolved against the in-flight buffer and dedupe-by-id fails — a mid-turn reload re-renders the already-shown prefix of the in-flight turn as duplicates.

More generally this makes Event.id unusable as a stable cross-observation key, which is surprising given invocation_id, author, and the full content are faithfully round-tripped from raw_event.

Minimal repro (no live backend needed)

The overwrite is visible by driving the conversion function directly:

import datetime
from types import SimpleNamespace
from google.adk.events.event import Event
from google.adk.sessions.vertex_ai_session_service import _from_api_event

original = Event(id="c452feb0-b9ad-48b1-b863-c7dd2f085378",
                 invocation_id="e-1", author="model")
raw_event = original.model_dump(exclude_none=True, mode="json", by_alias=True)
# raw_event["id"] == "c452feb0-..."  (the original id is persisted)

api_event = SimpleNamespace(
    raw_event=raw_event,
    name="projects/p/locations/l/reasoningEngines/r/sessions/s/events/658731057016733696",
    invocation_id="e-1", author="model",
    timestamp=datetime.datetime.now(datetime.timezone.utc),
)
loaded = _from_api_event(api_event)
print(loaded.id)  # -> "658731057016733696"  (original UUID discarded, though it's in raw_event)

Expected

get_session should return each event with its original Event.id (the value already present in raw_event), so ids are stable across streaming and reload. The Vertex resource name is still available separately on api_event_obj.name for callers who need it.

Proposed fix

When raw_event is present, preserve its id rather than overwriting it (fall back to the resource name only when raw_event is absent, for older data):

event_dict = copy.deepcopy(raw_event_dict)
event_dict.update({
    'invocation_id': getattr(api_event_obj, 'invocation_id', None),
    'author': getattr(api_event_obj, 'author', None),
})
event_dict.setdefault('id', api_event_obj.name.split('/')[-1])  # only if raw_event lacked one
return Event.model_validate(event_dict)

Workaround

Stash the original id in custom_metadata on append and restore it on read — custom_metadata is a declared field that round-trips through raw_event and (unlike the top-level id) is not overwritten by _from_api_event. (Related: #6055, where an undeclared custom_metadata attribute was dropped on persistence — a declared field avoids that.)

Environment

  • google-adk 1.31.1 (behavior identical on main)
  • VertexAiSessionService (Vertex AI Agent Engine sessions)

Activity

  1. added
    services
    on Jul 31, 2026
  2. surajksharma07 commented on Jul 31, 2026

    @surajksharma07
    Collaborator

    @ashubham The raw_event branch in _from_api_event is clobbering your persisted UUID with the resource-name suffix, exactly as you described.

    Looks like #6531 already has a patch for this (falls back to the resource name only when raw_event has no id) — could you pull that branch and check it resolves your resume-cursor case?
    If it holds up after some testing on your end, that PR looks ready to move forward!

  3. added
    request clarification
    on Jul 31, 2026
  4. added a commit that references this issue on Jul 31, 2026
    222fbe5
  5. ashubham commented on Jul 31, 2026

    @ashubham
    Author

    Yes, the fix works for us. Also, it will be great to port this to 1.x branch.

  6. ashubham commented on Aug 3, 2026

    @ashubham
    Author

    @surajksharma07 Would be great to close on this.

  7. surajksharma07 commented on Aug 4, 2026

    @surajksharma07
    Collaborator

    Fair nudge — everything's checked out on our side too: #6531 correctly stops raw_event's id from being clobbered by the resource-name suffix, and #6545 mirrors that cleanly for v1.x, both green on tests.

    Can't close until one of them actually merges though, and neither has a review from DeanChensj yet — I'll chase that on both now that you've confirmed the fix holds on your end!

  8. self-assigned this
    on Aug 7, 2026
  9. surajksharma07 commented on Aug 28, 2026

    @surajksharma07
    Collaborator

    @wuliang229 @DeanChensj following up again. #6531 has been sitting on ready to pull since Aug 7 (21+ days) with no import yet and the v1 port #6545 is in the same spot. Both are approved, green and confirmed working by the reporter. Is something blocking the Copybara import or does this just need a nudge?

  10. added a commit that references this issue on Sep 4, 2026
    864914b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

request clarificationservices

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions