test: validate mypy-diff CI for serialization fix#2
Conversation
… non-serializable objects When Python callables or other non-JSON-serializable values are stored in session state (e.g. via MCP tool callbacks), EventActions.state_delta and agent_state would cause DatabaseSessionService.append_event to crash with PydanticSerializationError during event.model_dump(mode="json"). Add field serializers for state_delta and agent_state that recursively convert non-serializable leaf values to a descriptive string, allowing the event to be persisted without data loss for serializable fields. Fixes google#4724
- Add cast(dict[str, Any], ...) to _serialize_state_delta and _serialize_agent_state return values to resolve no-any-return errors - Add # type: ignore[misc] to @field_serializer decorators to suppress untyped-decorator errors (known pydantic/mypy compatibility issue) - Add # type: ignore[misc] to EventCompaction and EventActions class definitions to suppress pre-existing BaseModel subclass typing errors
Summary of ChangesHello, 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 primarily addresses potential serialization issues within Pydantic models by introducing a robust JSON serialization utility. It ensures that complex dictionary fields within event actions can be reliably converted to JSON, preventing crashes when encountering non-serializable objects. The changes are also being used to validate the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a fix for a serialization issue by adding a helper function _make_json_serializable and using it within Pydantic field serializers for state_delta and agent_state. This approach effectively prevents crashes when these fields contain non-JSON-serializable data, such as callables. The changes are logical and directly address the problem. I have one suggestion to simplify one of the new serializer methods for better conciseness.
| if value is None: | ||
| return None | ||
| return cast(dict[str, Any], _make_json_serializable(value)) |
There was a problem hiding this comment.
The _make_json_serializable function correctly handles None values by returning None, so the explicit check for value is None is redundant. You can simplify this method by removing the conditional and calling _make_json_serializable directly on the value, which makes the code more concise.
return cast(Optional[dict[str, Any]], _make_json_serializable(value))
Draft PR to validate CI before pushing to upstream google#4748