Skip to content

Commit 289f8d0

Browse files
committed
add docs for built-in tools
1 parent 17c8e83 commit 289f8d0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

docs/builtin-tools.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,51 @@ These tools are passed to the agent via the `builtin_tools` parameter and are ex
2020

2121
If a provider supports a built-in tool that is not currently supported by Pydantic AI, please file an issue.
2222

23+
## Dynamic Configuration
24+
25+
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`.
26+
27+
This is particularly useful for tools like [`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool] where you might want to set the user's location based on the current request.
28+
29+
```python {title="dynamic_builtin_tool.py"}
30+
from dataclasses import dataclass
31+
from pydantic_ai import Agent, RunContext, WebSearchTool, WebSearchUserLocation
32+
33+
@dataclass
34+
class UserContext:
35+
location: str | None
36+
37+
async def prepared_web_search(ctx: RunContext[UserContext]) -> WebSearchTool | None:
38+
if not ctx.deps.location:
39+
return None
40+
41+
return WebSearchTool(
42+
user_location=WebSearchUserLocation(city=ctx.deps.location),
43+
)
44+
45+
agent = Agent(
46+
'openai-responses:gpt-5',
47+
builtin_tools=[prepared_web_search],
48+
deps_type=UserContext,
49+
)
50+
51+
# Run with location
52+
result = agent.run_sync(
53+
'What is the weather like?',
54+
deps=UserContext(location='London'),
55+
)
56+
print(result.output)
57+
#> It's currently raining in London.
58+
59+
# Run without location (tool will be omitted)
60+
result = agent.run_sync(
61+
'What is the capital of France?',
62+
deps=UserContext(location=None),
63+
)
64+
print(result.output)
65+
#> The capital of France is Paris.
66+
```
67+
2368
## Web Search Tool
2469

2570
The [`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool] allows your agent to search the web,

0 commit comments

Comments
 (0)