Skip to content

feat(agents): add tool input coercion and output serialization (GAP-005)#2273

Merged
kevinmessiaen merged 5 commits intomainfrom
feat/agents-gap005-tool-serialization
Mar 10, 2026
Merged

feat(agents): add tool input coercion and output serialization (GAP-005)#2273
kevinmessiaen merged 5 commits intomainfrom
feat/agents-gap005-tool-serialization

Conversation

@Hartorn
Copy link
Copy Markdown
Member

@Hartorn Hartorn commented Feb 26, 2026

Tool.run() now coerces dict arguments into validated Pydantic instances via _params_model and serializes return values to JSON-safe primitives via TypeAdapter.dump_python(mode="json").

Made-with: Cursor

Description

Related Issue

Type of Change

  • 📚 Examples / docs / tutorials / dependencies update
  • 🔧 Bug fix (non-breaking change which fixes an issue)
  • 🥂 Improvement (non-breaking change which improves an existing feature)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🔐 Security fix

Checklist

  • I've read the CODE_OF_CONDUCT.md document.
  • I've read the CONTRIBUTING.md guide.
  • I've written tests for all new methods and classes that I created.
  • I've written the docstring in Google format for all the methods and classes that I used.
  • I've updated the pdm.lock running pdm update-lock (only applicable when pyproject.toml has been
    modified)

Tool.run() now coerces dict arguments into validated Pydantic instances
via _params_model and serializes return values to JSON-safe primitives
via TypeAdapter.dump_python(mode="json").

Made-with: Cursor
@Hartorn Hartorn self-assigned this Feb 26, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Hartorn, 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 significantly enhances the Tool.run() method by integrating robust input coercion and output serialization. It leverages Pydantic to automatically validate and transform dictionary arguments into typed BaseModel instances and ensures that all return values are serialized into JSON-safe primitive types. This makes tool interactions more reliable, predictable, and easier to integrate with systems expecting standardized data formats.

Highlights

  • Input Coercion: Implemented Pydantic-based input coercion for tool arguments, ensuring validated BaseModel instances are passed to the tool's function.
  • Output Serialization: Introduced JSON-safe output serialization for tool return values using TypeAdapter.dump_python, converting complex types to primitives suitable for JSON.
  • Automated Configuration: Updated Tool.from_callable to automatically configure input validation models (_params_model) and output serialization adapters (_return_adapter) based on the decorated function's signature.

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

Changelog
  • libs/giskard-agents/src/giskard/agents/tools/tool.py
    • Imported PrivateAttr and TypeAdapter from Pydantic.
    • Added _params_model and _return_adapter as private attributes to the Tool class.
    • Modified from_callable to create and assign a Pydantic model for input parameters (_params_model) and a TypeAdapter for return values (_return_adapter).
    • Updated the run method to perform input coercion using _params_model.model_validate(arguments) before executing the tool's function.
    • Modified the run method to serialize the tool's return value using _return_adapter.dump_python(res, mode="json") if an adapter is present.
  • libs/giskard-agents/tests/test_tools.py
    • Added necessary imports for datetime, timezone, UUID, and BaseModel to support new test cases.
    • Introduced new test functions to verify input coercion for single BaseModel instances, optional BaseModel instances, and lists of BaseModel instances.
    • Added new test functions to confirm JSON-safe serialization of BaseModel outputs, including complex types like UUID and datetime, and lists of BaseModel outputs.
    • Included a parameterized test to ensure primitive types remain unchanged during the new coercion and serialization process.
Activity
  • The pull request was created by Hartorn.
  • The description indicates it was "Made-with: Cursor".
  • A standard checklist for changes, related issues, and contribution guidelines is included, with checkboxes currently unmarked.
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
Copy Markdown
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 introduces input coercion and output serialization for tools, which is a great enhancement for robustness. The use of Pydantic's TypeAdapter for serialization and model_validate for coercion is well-implemented. The accompanying tests are thorough and cover various scenarios, including primitive types, BaseModels, optional types, and lists. I have one suggestion to simplify and improve the efficiency of argument handling after validation.

Comment on lines +164 to +168
arguments = {
name: getattr(validated, name)
for name in self._params_model.model_fields
if name in arguments
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The way arguments is reconstructed after validation can be simplified and made more efficient. The current dictionary comprehension iterates over all model fields, which is not optimal. Using Pydantic's model_dump() method is more idiomatic, readable, and performant. It will correctly create a dictionary of all arguments, including those with default values, ready to be passed to the tool's function.

            arguments = validated.model_dump()

@Hartorn
Copy link
Copy Markdown
Member Author

Hartorn commented Feb 26, 2026

/gemini review

Copy link
Copy Markdown
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 introduces input coercion and output serialization for tools. Input arguments are now validated and coerced into Pydantic models, and return values are serialized into JSON-safe primitives using TypeAdapter. This is a great enhancement that makes tools more robust and predictable. The changes are well-implemented and accompanied by a comprehensive set of tests covering various scenarios like nested models, optional models, and lists of models. I have one suggestion to simplify the argument reconstruction logic after validation.

Comment on lines +167 to +171
arguments = {
name: getattr(validated, name)
for name in self._params_model.model_fields
if name in arguments
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The logic for rebuilding the arguments dictionary after validation can be simplified. Since model_validate will raise an error if arguments contains keys that are not part of the model (as extra='forbid' is the default for create_model), we can be sure that all keys in arguments are valid field names.

Therefore, you can iterate directly over the keys of the arguments dictionary to get the coerced values from the validated model. This is more concise and slightly more efficient if the number of provided arguments is less than the total number of fields in the model.

            arguments = {name: getattr(validated, name) for name in arguments}

@Hartorn
Copy link
Copy Markdown
Member Author

Hartorn commented Feb 26, 2026

/gemini review

Copy link
Copy Markdown
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 introduces input coercion and output serialization for tools, which is a great enhancement for robustness, especially when dealing with complex data types. The use of Pydantic's TypeAdapter for this is a solid approach. The implementation is mostly correct, but I've found one potential issue where extra arguments in a tool call could lead to a crash. I've provided a suggestion to fix this. The accompanying tests are well-written and cover the new functionality.

# caller explicitly provided, letting Python defaults handle the rest.
if self._params_model is not None:
validated = self._params_model.model_validate(arguments)
arguments = {name: getattr(validated, name) for name in arguments}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

There's a potential AttributeError here if the input arguments dictionary contains keys that are not defined in the tool's parameter model (_params_model). With Pydantic's default behavior (extra='ignore'), model_validate will not include these extra fields in the validated object. The dictionary comprehension then fails when getattr is called for an extra key, which can crash the tool execution.

To make this more robust, you can filter the arguments to only include keys that are defined in the model's fields.

Suggested change
arguments = {name: getattr(validated, name) for name in arguments}
arguments = {name: getattr(validated, name) for name in arguments if name in self._params_model.model_fields}

…P-005)

Iterate over caller-provided keys instead of model_fields, filtering
out any extra keys unknown to the params model (Pydantic defaults to
extra='ignore', so model_validate silently drops them). Update the
inline comment to document the getattr vs model_dump trade-off and
the extra-key handling.

Made-with: Cursor
@Hartorn Hartorn force-pushed the feat/agents-gap005-tool-serialization branch from 6acb542 to 411dd8a Compare February 26, 2026 15:11
@Hartorn
Copy link
Copy Markdown
Member Author

Hartorn commented Feb 26, 2026

/gemini review

Copy link
Copy Markdown
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 introduces input coercion and output serialization for tools, which is a great enhancement. The implementation correctly uses Pydantic's TypeAdapter for robust serialization and model_validate for input coercion. The accompanying tests are thorough and cover various scenarios, including nested models, optional types, and lists.

I have one suggestion in tool.py to simplify the argument reconstruction logic, making it more explicit and easier to maintain by using the Pydantic model as the single source of truth for all parameters.

@kevinmessiaen kevinmessiaen merged commit 54c39ea into main Mar 10, 2026
23 checks passed
@kevinmessiaen kevinmessiaen deleted the feat/agents-gap005-tool-serialization branch March 10, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants