Skip to content

Conversation

aymeric-roucher
Copy link
Collaborator

@aymeric-roucher aymeric-roucher commented Aug 7, 2025

Tools can use pydantic models as their input schemas!

Builds on #1643, fixes #699

Features:

  • Automatic Pydantic detection: Tools can now accept Pydantic BaseModel classes as parameter types
  • Dict-to-model conversion: Automatically converts dictionary arguments to Pydantic model instances during tool execution (this is specially needed for ToolCallingAgent, where the model can only output a dictionary)
  • Schema integration: Pydantic JSON schemas are processed and integrated with smolagents validation system
  • Updated validation: Supports complex Pydantic features including nested models, enum constraints, optional fields, and custom validators
  • Backward compatibility: All existing tools continue to work without changes

Discussion points:

  • Requires adding pydantic dependency to smolagents
  • This PR enables forcing the agent to return structured outputs, as requested many times! Indeed, one can now just create a custom FinalAnswer tool, for which the forward method takes any custom pydantic model as input, and returns its input. We could make this method more accessible by passing a structured_output arg to agent initialization, this would be for an upcoming PR

@aymeric-roucher aymeric-roucher marked this pull request as ready for review August 8, 2025 16:30
@aymeric-roucher aymeric-roucher changed the title Feature/pydantic schema support Support pydantic schemas in tools Aug 8, 2025
# Return primitive values as-is
return obj

def convert_anyof_to_nullable(obj):
Copy link
Collaborator Author

@aymeric-roucher aymeric-roucher Aug 17, 2025

Choose a reason for hiding this comment

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

@albertvillanova a big part of the logic in there is to handle our "nullable" parameter with Pydantic models that follow a different json schema spec.

Maybe it would be wise in a follow-up PR to handle json schemas directly rather than converting their logic to our nullable fomat
Handling json shcemas directly could use either of the solutions below:

Solution 1:
Just add a completely new validation logic, with a tool.input_schema parameter that follows the pydantic schema : each tool init could either (exclusive OR) use tool.inputs or tool.input_schema, depending on what was use for initialization.
Then after some time, tool.inputs could be deprecated to only follow the new version.

Solution 2:
Let tool.inputs be either our legacy way of describing inputs, or a json schema. This is detected dynamically, then either logic is used for validation.

@chahn
Copy link
Contributor

chahn commented Aug 25, 2025

@duguyue100 to my understanding, this PR might be a comprehensive solution to your issue described in #1708 and grll/mcpadapt#68

@duguyue100
Copy link
Contributor

@duguyue100 to my understanding, this PR might be a comprehensive solution to your issue described in #1708 and grll/mcpadapt#68

@chahn Does this have anything to do with how MCPAdapt creates smolagents tools? I couldn't see it.
Or you are saying that MCPAdapt will somehow convert the input schema from MCP server to pydantic models and then create smolagents tools?

@aymeric-roucher
Copy link
Collaborator Author

@albertvillanova do you have some time for a review?

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.

[Feature request] Enable Tool arguments to process pydantic schemas properly
4 participants