Skip to content

Conversation

@hung12ct
Copy link
Contributor

EventCompaction was incorrectly deserialized as dict instead of object when loading from database. Changed model_copy() to model_validate() which properly deserializes nested Pydantic models.

Fixes: AttributeError: 'dict' object has no attribute 'start_timestamp'

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable): #3487

2. Or, if no issue exists, describe the change:

If applicable, please follow the issue templates to provide as much detail as
possible.

Problem:
When using DatabaseSessionService (for example: with MySQL), events with EventCompaction crash after being loaded from the database with the following error:

AttributeError: 'dict' object has no attribute 'start_timestamp' 
image

Root Cause:

The StorageEvent.to_event() method in database_session_service.py (line 349) uses model_copy() to deserialize EventActions:

actions=EventActions().model_copy(update=self.actions.model_dump())

Issue: model_copy() performs a shallow copy and does not properly deserialize nested Pydantic models. When self.actions.model_dump() returns a dict containing compaction as a nested dict (e.g., {'compaction': {'start_timestamp': 1.0, 'end_timestamp': 2.0, ...}}), model_copy() leaves it as a dict instead of converting it to an EventCompaction object.

This causes runtime errors when code attempts to access compaction attributes like compaction.start_timestamp, because Python tries to access an attribute on a dict instead of an EventCompaction instance.

Solution:

Changed model_copy() to model_validate() which properly deserializes nested Pydantic models:

actions=EventActions.model_validate(self.actions.model_dump())

model_validate() performs full validation and deserialization, converting nested dicts into their corresponding Pydantic model instances (EventCompaction).

Testing Plan

Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Please include a summary of passed pytest results.
image

Manual End-to-End (E2E) Tests:

Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Add any other context or screenshots about the feature request here.

EventCompaction was incorrectly deserialized as dict instead of object when loading from database. Changed model_copy() to model_validate() which properly deserializes nested Pydantic models. Fixes: AttributeError: 'dict' object has no attribute 'start_timestamp'
@gemini-code-assist
Copy link

Summary of Changes

Hello @hung12ct, 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 resolves a critical bug in the DatabaseSessionService where nested Pydantic models, specifically EventCompaction within EventActions, were not being properly deserialized from the database. The previous use of model_copy() resulted in these nested models being treated as plain dictionaries, causing runtime AttributeError when attempting to access their attributes. By switching to model_validate(), the system now correctly reconstructs the full Pydantic object hierarchy, ensuring data integrity and preventing application crashes related to session loading.

Highlights

  • Deserialization Fix for Nested Pydantic Models: Corrected an issue where EventCompaction within EventActions was incorrectly deserialized as a dictionary instead of a Pydantic object when loaded from the database, leading to AttributeError.
  • Transition from model_copy() to model_validate(): The StorageEvent.to_event() method in database_session_service.py now uses EventActions.model_validate() instead of EventActions().model_copy() to ensure proper, deep deserialization of nested Pydantic models.
  • New Comprehensive Test Suite: A new test file, test_database_compaction_deserialization.py, has been added to verify that EventCompaction objects retain their type and attributes after being saved to and loaded from various session services (In-Memory, Database, Sqlite).
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 services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Nov 11, 2025
Copy link

@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 addresses a deserialization issue with nested EventCompaction objects within EventActions when using DatabaseSessionService. The fix correctly replaces model_copy() with model_validate(), ensuring that nested Pydantic models are properly instantiated from dictionary representations, which resolves the AttributeError. The change is accompanied by a new, well-structured test file that provides excellent coverage for this fix. The tests are parameterized to run against multiple session service implementations, verifying the fix and checking for regressions. The overall changes are of high quality and improve the robustness of session handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

3 participants