Skip to content

Commit 838530e

Browse files
GWealecopybara-github
authored andcommitted
fix: rehydration of EventActions in StorageEvent.to_event
The change updates the `StorageEvent.to_event` method to use `EventActions.model_validate` when rehydrating the `actions` field. This ensures that nested models within `EventActions`, such as `EventCompaction`, are correctly reconstructed from the stored data Close #4047 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 852408683
1 parent 8bed01c commit 838530e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/google/adk/sessions/schemas/v0.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,11 @@ def to_event(self) -> Event:
310310
branch=self.branch,
311311
# This is needed as previous ADK version pickled actions might not have
312312
# value defined in the current version of the EventActions model.
313-
actions=EventActions().model_copy(update=self.actions.model_dump()),
313+
actions=(
314+
EventActions.model_validate(self.actions.model_dump())
315+
if self.actions
316+
else EventActions()
317+
),
314318
timestamp=self.timestamp.timestamp(),
315319
long_running_tool_ids=self.long_running_tool_ids,
316320
partial=self.partial,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from datetime import datetime
16+
from datetime import timezone
17+
18+
from google.adk.events.event_actions import EventActions
19+
from google.adk.events.event_actions import EventCompaction
20+
from google.adk.sessions.schemas.v0 import StorageEvent
21+
from google.genai import types
22+
23+
24+
def test_storage_event_v0_to_event_rehydrates_compaction_model():
25+
compaction = EventCompaction(
26+
start_timestamp=1.0,
27+
end_timestamp=2.0,
28+
compacted_content=types.Content(
29+
role="user",
30+
parts=[types.Part(text="compacted")],
31+
),
32+
)
33+
actions = EventActions(compaction=compaction)
34+
storage_event = StorageEvent(
35+
id="event_id",
36+
invocation_id="invocation_id",
37+
author="author",
38+
actions=actions,
39+
session_id="session_id",
40+
app_name="app_name",
41+
user_id="user_id",
42+
timestamp=datetime.fromtimestamp(3.0, tz=timezone.utc),
43+
)
44+
45+
event = storage_event.to_event()
46+
47+
assert event.actions is not None
48+
assert isinstance(event.actions.compaction, EventCompaction)
49+
assert event.actions.compaction.start_timestamp == 1.0
50+
assert event.actions.compaction.end_timestamp == 2.0

0 commit comments

Comments
 (0)