Skip to content

fix: serialize Pydantic models before storing in session state#188

Open
ayushozha wants to merge 1 commit intogoogle-agentic-commerce:mainfrom
ayushozha:fix/serialize-pydantic-models-in-session-state
Open

fix: serialize Pydantic models before storing in session state#188
ayushozha wants to merge 1 commit intogoogle-agentic-commerce:mainfrom
ayushozha:fix/serialize-pydantic-models-in-session-state

Conversation

@ayushozha
Copy link

Summary

  • Call .model_dump() on Pydantic models (IntentMandate, CartMandate, PaymentMandate, ContactAddress, PaymentReceipt) before storing them in ADK session state, ensuring they are JSON-serializable dicts
  • Reconstruct Pydantic models from dicts when reading them back from state where attribute access is needed (e.g., cart_mandate.contents.payment_request)
  • This fixes the TypeError: Object of type IntentMandate is not JSON serializable error when using sqlite_session_service

Test plan

  • Run the shopping agent end-to-end flow with sqlite_session_service and verify no JSON serialization errors
  • Verify intent mandate creation and product search still works
  • Verify cart selection, update, and payment mandate creation flow works
  • Verify payment initiation (with and without OTP) works
  • Verify the credentials provider interactions still work

Fixes #129

🤖 Generated with Claude Code

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>
@ayushozha ayushozha requested a review from a team as a code owner March 18, 2026 08:28
@google-cla
Copy link

google-cla bot commented Mar 18, 2026

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 sqlite_session_service, preventing runtime errors and ensuring the agent's end-to-end flows function correctly.

Highlights

  • Pydantic Model Serialization: Implemented serialization of Pydantic models (IntentMandate, CartMandate, PaymentMandate, ContactAddress, PaymentReceipt) using .model_dump() before storing them in ADK session state. This ensures they are JSON-serializable dictionaries.
  • Pydantic Model Deserialization: Added logic to reconstruct Pydantic models from their dictionary representations when retrieving them from session state, allowing for proper attribute access.
  • Bug Fix: Resolved the TypeError: Object of type IntentMandate is not JSON serializable error that occurred when using sqlite_session_service.

🧠 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 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.

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.

Copy link
Contributor

@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 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TypeError: Object of type IntentMandate is not JSON serializable - sqlite_session_service fails to serialize Pydantic models in session state

1 participant