feat(agents): add tool input coercion and output serialization (GAP-005)#2273
feat(agents): add tool input coercion and output serialization (GAP-005)#2273kevinmessiaen merged 5 commits intomainfrom
Conversation
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
Summary of ChangesHello @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 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. Changelog
Activity
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. 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
|
There was a problem hiding this comment.
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.
| arguments = { | ||
| name: getattr(validated, name) | ||
| for name in self._params_model.model_fields | ||
| if name in arguments | ||
| } |
There was a problem hiding this comment.
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()|
/gemini review |
There was a problem hiding this comment.
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.
| arguments = { | ||
| name: getattr(validated, name) | ||
| for name in self._params_model.model_fields | ||
| if name in arguments | ||
| } |
There was a problem hiding this comment.
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}|
/gemini review |
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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.
| 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
6acb542 to
411dd8a
Compare
|
/gemini review |
There was a problem hiding this comment.
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.
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
Checklist
CODE_OF_CONDUCT.mddocument.CONTRIBUTING.mdguide.pdm.lockrunningpdm update-lock(only applicable whenpyproject.tomlhas beenmodified)