-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add Runtime builtin_tools
Support
#3009
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
base: main
Are you sure you want to change the base?
Add Runtime builtin_tools
Support
#3009
Conversation
…t and implement merge_builtin_tools function
builtin_tools
Support #2821builtin_tools
Support (#2821)
builtin_tools
Support (#2821)builtin_tools
Support
Heyy @DouweM |
builtin_tools=merge_builtin_tools( | ||
list(self._builtin_tools), list(builtin_tools) if builtin_tools else None | ||
), |
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.
We can skip having a helper method using this trick, in a variable we build further up in this method:
builtin_tools = list(
{
**({type(tool): tool for tool in self._builtin_tools or []}),
**({type(tool): tool for tool in builtin_tools or []}),
}.values()
)
from pydantic_ai.models.test import TestModel | ||
|
||
|
||
def test_merge_builtin_tools_basic(): |
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.
We can drop these tests if we drop the merge helper as I suggested above
|
||
# Should raise error when builtin_tools contains actual tools | ||
with pytest.raises(Exception, match='TestModel does not support built-in tools'): | ||
agent.run_sync('Hello', builtin_tools=[WebSearchTool()]) |
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.
We can replace these tests with one that verifies that test_model.last_model_request_parameters.builtin_tools
contains what we expect
Add Runtime Builtin Tools Support with Priority-Based Merging
Overview
This PR Fixes issue #2821 adds support for providing builtin tools at runtime in
Agent.run()
,Agent.run_sync()
,Agent.run_stream()
andAgent.iter()
methods, with runtime tools taking priority over agent initialization tools. The implementation follows the exact same pattern as the existing model settings priority system.Motivation
Previously, builtin tools could only be configured when creating an Agent instance. This limitation made it difficult to:
Implementation Approach
Priority System Design
The implementation follows the established pattern used for model settings
merge_model_settings()
behavior:Core Changes
1. Settings Module Enhancement
merge_builtin_tools()
function that implements type-based deduplicationWebSearchTool
)2. Agent Interface Updates
builtin_tools: Sequence[AbstractBuiltinTool] | None = None
parameter to all agent method signaturesNone
3. Core Agent Implementation
iter
method (the base method all others delegate to)GraphAgentDeps
4. Wrapper Agent Support
WrapperAgent
to pass through thebuiltin_tools
parameter