Skip to content

Conversation

@Ayush10
Copy link

@Ayush10 Ayush10 commented Jan 31, 2026

Summary

  • Calls .model_dump() on all Pydantic BaseModel instances before storing them in tool_context.state, ensuring values are plain dicts that json.dumps() can serialize.
  • Calls .model_validate() to reconstruct typed models when reading back from state where attribute access is needed.
  • Adds missing CartMandate import to payment_method_collector/tools.py.

Closes #129

Affected files

  • samples/python/src/roles/shopping_agent/tools.py — 5 writes serialized, 3 reads reconstructed
  • samples/python/src/roles/shopping_agent/subagents/shopper/tools.py — 2 writes serialized, 2 reads reconstructed
  • samples/python/src/roles/shopping_agent/subagents/payment_method_collector/tools.py — 1 read reconstructed, 1 import added

Test plan

  • Run samples/python/scenarios/a2a/human-present/cards/run.sh and complete a full purchase flow
  • Confirm no TypeError on state serialization with sqlite_session_service
  • Confirm end-to-end purchase completes (cart selection, payment, receipt)

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
@Ayush10 Ayush10 requested a review from a team as a code owner January 31, 2026 09:29
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 TypeError during JSON serialization. By explicitly converting Pydantic models to plain dictionaries before storage and reconstructing them upon retrieval, this change ensures robust data persistence and prevents runtime errors, thereby stabilizing the end-to-end purchase flow within the shopping agent.

Highlights

  • Pydantic Model Serialization: Ensured Pydantic BaseModel instances are explicitly serialized using .model_dump() before being stored in tool_context.state to prevent serialization errors.
  • Pydantic Model Deserialization: Implemented explicit deserialization using .model_validate() when retrieving Pydantic models from tool_context.state to reconstruct typed objects.
  • Missing Import Added: Added a necessary CartMandate import to samples/python/src/roles/shopping_agent/subagents/payment_method_collector/tools.py.

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

Choose a reason for hiding this comment

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

low

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