fix: serialize Pydantic models before storing in session state#188
Conversation
Call .model_dump() on IntentMandate, CartMandate, and PaymentMandate before storing in session state to ensure JSON serialization works with sqlite_session_service. Fixes google-agentic-commerce#129 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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 addresses a critical serialization issue within the shopping agent's session state management. By ensuring that Pydantic models are converted to JSON-serializable dictionaries before storage and reconstructed upon retrieval, the changes enable robust operation with session services like 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 effectively resolves the JSON serialization error by consistently using .model_dump() when storing Pydantic models in the session state and reconstructing them when they are read back and need to be accessed as objects. The changes are logical and well-implemented across the affected files. I have one minor suggestion to improve code clarity and consistency regarding variable naming.
| 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.
This change is correct since intent_mandate is now a dictionary. For better clarity and consistency with other parts of this PR (e.g., update_chosen_cart_mandate where cart_mandate_dicts is used), consider renaming the intent_mandate variable to intent_mandate_dict on line 87. This would make it explicit that it's a dictionary and not a Pydantic model instance, improving code readability.
Summary
.model_dump()on Pydantic models (IntentMandate, CartMandate, PaymentMandate, ContactAddress, PaymentReceipt) before storing them in ADK session state, ensuring they are JSON-serializable dictscart_mandate.contents.payment_request)TypeError: Object of type IntentMandate is not JSON serializableerror when usingsqlite_session_serviceTest plan
sqlite_session_serviceand verify no JSON serialization errorsFixes #129
🤖 Generated with Claude Code