Skip to content

Conversation

@diptendulkar
Copy link

Description

Summary

Fixes session state serialization error when using ADK's SqliteSessionService. Pydantic models stored in tool_context.state were causing TypeError: Object of type IntentMandate is not JSON serializable because json.dumps() cannot serialize Pydantic BaseModel instances directly.

Problem

When the ADK persists session state to SQLite, it calls json.dumps(delta) on the state delta. If the state contains Pydantic model instances (like IntentMandate, CartMandate, PaymentMandate), serialization fails with:

This fixes the 'Object of type IntentMandate is not JSON serializable' error when the ADK's SqliteSessionService tries to serialize session state.

Solution

Store Pydantic models as dictionaries using .model_dump() and reconstruct them on retrieval using .model_validate() where property access is needed.

Changes

samples/python/src/roles/shopping_agent/subagents/shopper/tools.py:

  • intent_mandate → stored as .model_dump()
  • cart_mandates → stored as list of dicts
  • update_chosen_cart_mandate() → reconstructs CartMandate objects on retrieval

samples/python/src/roles/shopping_agent/tools.py:

  • cart_mandate, payment_receipt, payment_mandate, signed_payment_mandate → stored as dicts
  • create_payment_mandate() → reconstructs CartMandate on retrieval
  • sign_mandates_on_user_device() → reconstructs PaymentMandate and CartMandate on retrieval

Testing

Run the sample scenario:
bash samples/python/scenarios/a2a/human-present/cards/run.sh## Related Issues

  • Reported upstream to Google ADK: [link to your GitHub issue if applicable]

Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

Fixes #129 🦕

@diptendulkar diptendulkar requested a review from a team as a code owner December 29, 2025 15:41
@google-cla
Copy link

google-cla bot commented Dec 29, 2025

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 @diptendulkar, 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 error encountered when Pydantic models are stored in the ADK's SqliteSessionService. Previously, direct storage of Pydantic BaseModel instances led to TypeError during JSON serialization. The solution involves a systematic approach of converting Pydantic models to dictionaries using .model_dump() before storing them in the session state and then reconstructing them back into Pydantic objects using .model_validate() upon retrieval, ensuring seamless compatibility with JSON serialization mechanisms.

Highlights

  • Intent Mandate Storage: The intent_mandate is now stored as a dictionary using .model_dump() to ensure JSON serialization compatibility within the session state.
  • Cart Mandates Storage: cart_mandates are now stored as a list of dictionaries, converting each CartMandate object with .model_dump().
  • Cart Mandate Retrieval: The update_chosen_cart_mandate function now reconstructs CartMandate objects from their stored dictionary representations using .model_validate().
  • Shopping Agent Mandate Storage: In the main shopping agent tools, cart_mandate, payment_receipt, payment_mandate, and signed_payment_mandate are all stored as dictionaries using .model_dump().
  • Shopping Agent Mandate Retrieval: Functions like create_payment_mandate and sign_mandates_on_user_device now reconstruct CartMandate and PaymentMandate objects from their dictionary forms using .model_validate() when needed for property access.

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

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

  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 correctly addresses a JSON serialization error that occurred when storing Pydantic models in the session state. The solution of converting models to dictionaries using .model_dump() before storing and reconstructing them with .model_validate() upon retrieval is sound and has been applied consistently. The changes are well-implemented and include defensive checks to handle both dictionaries and model instances, ensuring robustness. I have one suggestion to improve maintainability by refactoring the repeated model reconstruction logic.

Comment on lines +249 to +250
payment_mandate: PaymentMandate = PaymentMandate.model_validate(payment_mandate_data) if isinstance(payment_mandate_data, dict) else payment_mandate_data
cart_mandate: CartMandate = CartMandate.model_validate(cart_mandate_data) if isinstance(cart_mandate_data, dict) else cart_mandate_data
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This logic for reconstructing Pydantic models from dictionaries is repeated in a few places (e.g., here, line 188 in this file, and in samples/python/src/roles/shopping_agent/subagents/shopper/tools.py on line 127).

To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, consider extracting this into a utility function. For example:

from pydantic import BaseModel
from typing import Type, TypeVar

M = TypeVar("M", bound=BaseModel)

def reconstruct_model(data: dict | M, model_class: Type[M]) -> M:
    """Reconstructs a Pydantic model from a dict if necessary."""
    if isinstance(data, dict):
        return model_class.model_validate(data)
    return data

This helper could be placed in a shared utility module. Using it would simplify the code to:

payment_mandate: PaymentMandate = reconstruct_model(payment_mandate_data, PaymentMandate)
cart_mandate: CartMandate = reconstruct_model(cart_mandate_data, CartMandate)

Since modifying shared files might be outside the scope of this PR, this could be addressed in a follow-up.

…zation

This fixes the 'Object of type IntentMandate is not JSON serializable' error
when the ADK's SqliteSessionService tries to serialize session state.

Changes:
- Store IntentMandate, CartMandate, PaymentMandate, and PaymentReceipt as
  dicts using .model_dump() instead of raw Pydantic objects
- Reconstruct Pydantic objects on retrieval where needed for property access
@diptendulkar diptendulkar force-pushed the fix-pydantic-session-serialization branch from 34e268e to 24f4e3b Compare December 30, 2025 04:39
@kharerajat2014
Copy link

I have signed the SLA

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

2 participants