-
Couldn't load subscription status.
- Fork 2.1k
Add Pydantic schema support for smolagents tools #1643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Pydantic schema support for smolagents tools #1643
Conversation
| "object", | ||
| "any", | ||
| "null", | ||
| # Additional types that can appear in Pydantic schemas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # Additional types that can appear in Pydantic schemas |
| "any", | ||
| "null", | ||
| # Additional types that can appear in Pydantic schemas | ||
| "enum", # For enum constraints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the comment (remnants of vibe coding?)
| potential_kwargs = args[0] | ||
|
|
||
| # If the dictionary keys match our input parameters, convert it to kwargs | ||
| # This handles the case where the dict keys are the parameter names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear comment IMO
| # This handles the case where the dict keys are the parameter names |
| if all(key in self.inputs for key in potential_kwargs): | ||
| args = () | ||
| kwargs = potential_kwargs | ||
| # If we have exactly one input parameter and the dict doesn't match parameter names, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the dict does not match the single parameter name, maybe it should be considered an error, i.e. the agent is trying to call a tool with wrong parameters?
| Checks that all provided arguments match the tool's expected input types and that | ||
| all required arguments are present. Supports both dictionary arguments and single | ||
| value arguments for tools with one input parameter. | ||
| value arguments for tools with one input parameter. Now includes updated support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remnants of vibe coding?
| # Use updated validation for complex schemas | ||
| _validate_value_against_schema(value, input_schema, key) | ||
| else: | ||
| # Fall back to original simple validation for backward compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of having a split validation: just make a full new validation and test it to make sure it does not break anything
|
Thank you @chitralputhran, I think supporting pydantic BaseModel would be great! Noting that this would require adding |
|
@chitralputhran FYI I've opened #1660 to finish this great work! |
Summary
PR implements full Pydantic BaseModel support for smolagents tools, allowing developers to use Pydantic models as tool parameters with automatic validation, schema generation, and dict-to-model conversion.
Fixes #699
Features
Changes Made
Implementation
src/smolagents/_function_type_hints_utils.py
_is_pydantic_model()to detect Pydantic BaseModel classes_get_pydantic_json_schema()to extract JSON schemas from Pydantic models_process_pydantic_schema()to handle schema processing including $refs resolutionsrc/smolagents/tools.py
Tool.__call__()with argument parsing for both single-parameter and multi-parameter scenarios_convert_dict_args_to_pydantic_models()method for automatic dict-to-Pydantic conversion_validate_value_against_schema()with additional validation for complex schemasTesting
tests/test_tools.py
TestPydanticToolIntegrationclass with additional test casestests/test_function_type_hints_utils.py
TestPydanticIntegrationclass with additional test casesUsage Examples
Before (Limited Support)
After (Full Pydantic Support)
Backward Compatibility
This implementation maintains full backward compatibility:
This implementation resolves the core issues outlined in #699 by providing Pydantic integration.