-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: handle non-serializable types in persistent session event storage #4741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atian8179
wants to merge
2
commits into
google:main
Choose a base branch
from
atian8179:fix/database-session-serialization-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/unittests/sessions/test_storage_event_serialization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Tests for StorageEvent serialization with non-serializable types. | ||
|
|
||
| Regression test for https://github.com/google/adk-python/issues/4724 | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from google.adk.events.event import Event | ||
| from google.adk.sessions import Session | ||
| from google.adk.sessions.schemas.v1 import StorageEvent | ||
|
|
||
|
|
||
| def _make_session() -> Session: | ||
| return Session( | ||
| app_name="test-app", | ||
| user_id="test-user", | ||
| id="test-session", | ||
| state={}, | ||
| ) | ||
|
|
||
|
|
||
| def _make_event(**kwargs) -> Event: | ||
| defaults = dict( | ||
| invocation_id="inv-1", | ||
| author="agent", | ||
| timestamp=time.time(), | ||
| ) | ||
| defaults.update(kwargs) | ||
| return Event(**defaults) | ||
|
|
||
|
|
||
| class TestStorageEventSerialization: | ||
| """Test that StorageEvent.from_event handles non-serializable types.""" | ||
|
|
||
| def test_basic_event_roundtrip(self): | ||
| """Normal events should serialize and deserialize correctly.""" | ||
| session = _make_session() | ||
| event = _make_event() | ||
| storage = StorageEvent.from_event(session, event) | ||
| assert storage.id == event.id | ||
| assert storage.session_id == session.id | ||
|
|
||
| def test_event_with_function_in_state_delta(self): | ||
| """Events with function objects in state_delta should not crash. | ||
|
|
||
| This is the core regression test for #4724: when tools attach | ||
| non-serializable function references to events, model_dump() | ||
| should gracefully degrade instead of raising | ||
| PydanticSerializationError. | ||
| """ | ||
| session = _make_session() | ||
| event = _make_event() | ||
| # Simulate a function object being attached to state_delta | ||
| # (this happens when MCP tools resolve their function references) | ||
| event.actions.state_delta["callback"] = lambda x: x | ||
|
|
||
| # This should NOT raise PydanticSerializationError | ||
| storage = StorageEvent.from_event(session, event) | ||
| assert storage.event_data is not None | ||
| # The function should be serialized as a placeholder string | ||
| actions = storage.event_data.get("actions", {}) | ||
| state_delta = actions.get("state_delta", actions.get("stateDelta", {})) | ||
| assert "non-serializable" in str(state_delta.get("callback", "")) | ||
|
|
||
| def test_roundtrip_preserves_serializable_fields(self): | ||
| """Non-serializable fields are replaced but other fields survive.""" | ||
| session = _make_session() | ||
| event = _make_event() | ||
| event.actions.state_delta["normal_key"] = "normal_value" | ||
| event.actions.state_delta["func_key"] = lambda: None | ||
|
|
||
| storage = StorageEvent.from_event(session, event) | ||
| restored = storage.to_event() | ||
|
|
||
| assert restored.actions.state_delta["normal_key"] == "normal_value" | ||
| assert "non-serializable" in str(restored.actions.state_delta.get("func_key", "")) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Eventmodel is configured to use camelCase aliases for JSON serialization (alias_generator=alias_generators.to_camel). This meansstate_deltawill be serialized asstateDelta. The current code checks forstate_deltafirst, which will always be a miss, before falling back tostateDelta. For clarity and to accurately reflect the expected data structure, it's better to accessstateDeltadirectly.