-
Notifications
You must be signed in to change notification settings - Fork 380
fix: Serialize Pydantic models before storing in session state #159
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
base: main
Are you sure you want to change the base?
fix: Serialize Pydantic models before storing in session state #159
Conversation
Call .model_dump() on Pydantic BaseModel instances before writing them to tool_context.state, and reconstruct with .model_validate() when reading them back. This prevents TypeError when ADK's sqlite_session_service calls json.dumps() on session state values. Closes google-agentic-commerce#129
Summary of ChangesHello @Ayush10, 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 addresses a critical issue where Pydantic models were not correctly serialized when stored in the application's session state, leading to 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. 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
|
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.
Code Review
This pull request addresses a serialization issue with Pydantic models in the session state by consistently serializing them to dictionaries before storage and reconstructing them upon retrieval. The changes are well-implemented across the affected files. I have one minor suggestion in samples/python/src/roles/shopping_agent/subagents/shopper/tools.py to improve consistency in handling model reconstruction from the state. Otherwise, the changes look good and effectively solve the described problem.
| A2aMessageBuilder() | ||
| .add_text("Find products that match the user's IntentMandate.") | ||
| .add_data(INTENT_MANDATE_DATA_KEY, intent_mandate.model_dump()) | ||
| .add_data(INTENT_MANDATE_DATA_KEY, intent_mandate) |
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.
While this change is correct given that intent_mandate is now a dictionary, it breaks the pattern of validating models immediately after reading them from state, which is followed elsewhere in this PR. For consistency and to ensure the data from the state is valid, it would be better to reconstruct the IntentMandate object on line 87. Consequently, this line should be reverted to call .model_dump() on the IntentMandate object.
Reconstruct IntentMandate via model_validate() when reading from session state, consistent with how CartMandate is validated elsewhere. Call .model_dump() when passing to the message builder.
Summary
.model_dump()on all PydanticBaseModelinstances before storing them intool_context.state, ensuring values are plain dicts thatjson.dumps()can serialize..model_validate()to reconstruct typed models when reading back from state where attribute access is needed.CartMandateimport topayment_method_collector/tools.py.Closes #129
Affected files
samples/python/src/roles/shopping_agent/tools.py— 5 writes serialized, 3 reads reconstructedsamples/python/src/roles/shopping_agent/subagents/shopper/tools.py— 2 writes serialized, 2 reads reconstructedsamples/python/src/roles/shopping_agent/subagents/payment_method_collector/tools.py— 1 read reconstructed, 1 import addedTest plan
samples/python/scenarios/a2a/human-present/cards/run.shand complete a full purchase flowTypeErroron state serialization withsqlite_session_service