You've already forked adk-python
mirror of
https://github.com/encounter/adk-python.git
synced 2026-03-30 10:57:20 -07:00
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
This commit is contained in:
committed by
Copybara-Service
parent
8bed01cbdc
commit
838530ebe0
@@ -310,7 +310,11 @@ class StorageEvent(Base):
|
||||
branch=self.branch,
|
||||
# This is needed as previous ADK version pickled actions might not have
|
||||
# value defined in the current version of the EventActions model.
|
||||
actions=EventActions().model_copy(update=self.actions.model_dump()),
|
||||
actions=(
|
||||
EventActions.model_validate(self.actions.model_dump())
|
||||
if self.actions
|
||||
else EventActions()
|
||||
),
|
||||
timestamp=self.timestamp.timestamp(),
|
||||
long_running_tool_ids=self.long_running_tool_ids,
|
||||
partial=self.partial,
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from datetime import datetime
|
||||
from datetime import timezone
|
||||
|
||||
from google.adk.events.event_actions import EventActions
|
||||
from google.adk.events.event_actions import EventCompaction
|
||||
from google.adk.sessions.schemas.v0 import StorageEvent
|
||||
from google.genai import types
|
||||
|
||||
|
||||
def test_storage_event_v0_to_event_rehydrates_compaction_model():
|
||||
compaction = EventCompaction(
|
||||
start_timestamp=1.0,
|
||||
end_timestamp=2.0,
|
||||
compacted_content=types.Content(
|
||||
role="user",
|
||||
parts=[types.Part(text="compacted")],
|
||||
),
|
||||
)
|
||||
actions = EventActions(compaction=compaction)
|
||||
storage_event = StorageEvent(
|
||||
id="event_id",
|
||||
invocation_id="invocation_id",
|
||||
author="author",
|
||||
actions=actions,
|
||||
session_id="session_id",
|
||||
app_name="app_name",
|
||||
user_id="user_id",
|
||||
timestamp=datetime.fromtimestamp(3.0, tz=timezone.utc),
|
||||
)
|
||||
|
||||
event = storage_event.to_event()
|
||||
|
||||
assert event.actions is not None
|
||||
assert isinstance(event.actions.compaction, EventCompaction)
|
||||
assert event.actions.compaction.start_timestamp == 1.0
|
||||
assert event.actions.compaction.end_timestamp == 2.0
|
||||
Reference in New Issue
Block a user