-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add prepare_tools param to Agent class #1474
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
Conversation
@rmaceissoft Thank you! Having an agent-wide That'd be less consistent with the @dmontagu What do you think? |
@DouweM thanks for the suggestion, I really like the idea! Defining I'm also on board with supporting multiple Happy to explore and implement that change if we align on the direction! We could also consider supporting both styles — passing it as an argument and using the decorator — similar to how tools work. That would add some nice flexibility without too much overhead. |
@rmaceissoft Before giving you the go-ahead: @Kludex Do you think this is reasonable? This mechanism could also help us clean up some of the tool prep we're already doing, like in |
Something similar will likely be implemented for MCP tools, on a per-server level: #1220 (comment). The most consistent route between these two paths may be a |
Hi team! Just leaving an update here: I've merged the latest changes from Regarding the idea of passing a As for the comment about transforming request parameters per model, I understand the motivation to streamline that logic, but I believe it may be better handled in a separate PR, as it addresses a different concern. Let me know how you'd like to proceed. Happy to revise the PR as needed, just waiting for feedback from the team before making further changes. |
Co-authored-by: Douwe Maan <[email protected]>
PR Change SummaryAdded a new parameter
Modified Files
How can I customize these reviews?Check out the Hyperlint AI Reviewer docs for more information on how to customize the review. If you just want to ignore it on this PR, you can add the Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add |
- Make `ToolsPrepareFunc` available directly from the `pydantic_ai.tools` module. - Enhance documentation for the agent-wide `prepare_tools` feature. - Fix return type hints in usage examples for clarity and correctness.
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.
The API looks good @DouweM 👍
All my comments are minor, but I'd like them to be addressed. 👀
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Reiner Marquez <[email protected]>
Purpose
Allow tools to be customized and switched off on each step from one place
Motivated by this comment
Technical Details:
The
prepare_tools
param is an optional callable with the following signature:This param provides a hook for customizing or filtering available tools just before they are sent to the model and it:
Tool.prepare_tool_def
).MCPServer.list_tools
).Tool.prepare_tool_def
returned None are excluded.Use Cases
Dynamic Tool Registration (filtering out tools when certain contextual conditions are met)
Custom Tool Definitions (force strict mode when using openai models)
TODO