-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Allow dynamic configuration of built-in tools via RunContext #3600
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?
feat: Allow dynamic configuration of built-in tools via RunContext #3600
Conversation
|
@AlanPonnachan Nice work! Can you please mention this in the Builtin tools docs as well? |
8caac69 to
289f8d0
Compare
|
@DouweM I added docs as well. Can you please review now |
|
|
||
| ## Dynamic Configuration | ||
|
|
||
| Sometimes you need to configure a built-in tool dynamically based on the [run context](api/tools.md#pydantic_ai.tools.RunContext) (e.g., user dependencies). You can achieve this by passing a function to `builtin_tools` that takes [`RunContext`][pydantic_ai.tools.RunContext] as an argument and returns an [`AbstractBuiltinTool`][pydantic_ai.builtin_tools.AbstractBuiltinTool] or `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.
| Sometimes you need to configure a built-in tool dynamically based on the [run context](api/tools.md#pydantic_ai.tools.RunContext) (e.g., user dependencies). You can achieve this by passing a function to `builtin_tools` that takes [`RunContext`][pydantic_ai.tools.RunContext] as an argument and returns an [`AbstractBuiltinTool`][pydantic_ai.builtin_tools.AbstractBuiltinTool] or `None`. | |
| Sometimes you need to configure a built-in tool dynamically based on the [run context][pydantic_ai.tools.RunContext] (e.g., user dependencies). You can achieve this by passing a function to `builtin_tools` that takes [`RunContext`][pydantic_ai.tools.RunContext] as an argument and returns an [`AbstractBuiltinTool`][pydantic_ai.builtin_tools.AbstractBuiltinTool] or `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.
Let's also explicitly mention this can be used for omitting, as is mentioned in the docstring
| model_settings: ModelSettings | None, | ||
| model_request_parameters: ModelRequestParameters, | ||
| ) -> ModelResponse: | ||
| self.captured_tools = model_request_parameters.builtin_tools |
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.
Let's use TestModel instead, which already has last_model_request_parameters that we can read this info off of
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.
Please move this to test_agent.py.
| This is useful if you want to customize the builtin tool based on the run context (e.g. user dependencies), | ||
| or omit it completely from a step. | ||
| Usage `BuiltinToolFunc[AgentDepsT]`. |
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.
This doesn't add much :)
Allow dynamic configuration of built-in tools via RunContext
This PR closes #3555
Adding support for dynamic configuration of built-in tools (e.g.,
WebSearchTool) based on the agent'sRunContext.Previously,
builtin_toolsonly accepted static instances. Now, it acceptsAbstractBuiltinToolinstances or aBuiltinToolFunc— a callable that takesRunContextand returns anAbstractBuiltinToolorNone. This allows users to inject context-aware parameters (like a user's location) into tools at runtime.Key Changes:
BuiltinToolFuncalias forCallable[[RunContext], Awaitable[AbstractBuiltinTool | None] | AbstractBuiltinTool | None]._agent_graph.pyto resolve these dynamic functions before generating model parameters.Agent,AbstractAgent,WrapperAgent, and durable execution integrations (DBOS, Prefect, Temporal).tests/test_dynamic_builtin_tools.pycovering sync/async definitions, conditional omission (returningNone), and runtime injection.Example Usage: