-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add builtin_tools
to Agent
#2102
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
Merged
+5,964
−253
Merged
Changes from all commits
Commits
Show all changes
92 commits
Select commit
Hold shift + click to select a range
57e568b
Add `builtin_tools` to `Agent`
Kludex 97ab44b
make AbstractBuiltinTool serializable
Kludex e3dda9d
Add more work on it
Kludex 3ad6d38
Merge remote-tracking branch 'origin/main' into add-builtin-tools
Kludex 0b43f65
Add builtin tools
Kludex fa7fd11
merge
Kludex 32324fa
add more built-in-tools
Kludex f33e568
Fix test
Kludex 13d7433
Add support on Groq
Kludex ac85205
Add support for Google
Kludex c93633f
Add support for MCP's Streamable HTTP transport (#1716)
BrandonShar 3a8b640
Timeout for initializing MCP client (#1833)
alexmojaki 360de87
Require mcp 1.9.0+ (#1840)
DouweM cb4e539
Don't send empty messages to Anthropic (#1027)
oscar-broman 4e3769a
Add `vendor_id` and `finish_reason` to Gemini/Google model responses …
davide-andreoli ebb536f
Fix units of `sse_read_timeout` `timedelta` (#1843)
alexmojaki c8bb611
Support functions as output_type, as well as lists of functions and o…
DouweM 6bcc1a8
Enhance Gemini usage tracking to collect comprehensive token data (#1…
kiqaps 97ff651
more
Kludex 1d47e1e
merge
Kludex 5f89444
merge
Kludex 9512987
merge
Kludex 800a71a
Pass tests
Kludex d0f4643
Merge main into builtin-tool branch and resolve conflicts
mattbrandman bc298d6
Fix remaining merge conflict markers in openai.py, anthropic.py, and …
mattbrandman 46c06c2
add extra google
mattbrandman 3496567
fix formatting
mattbrandman c193059
fix codespell
mattbrandman 427dec2
fixing types
mattbrandman 866ad21
fixing types in gemini
mattbrandman 4c2622d
misspells are on purpose oops
mattbrandman c13736e
ignore misspellings
mattbrandman a42a75d
ignore misspellings
mattbrandman e2f1daa
fixing comment
mattbrandman 3094a9a
fixing tests and coverage
mattbrandman 6a3c987
fixing types
mattbrandman ac0edb6
Revert "ignore misspellings"
mattbrandman 374e034
revert to known good
mattbrandman 7cccdd2
merging main
mattbrandman 2393c87
adding anthropic test coverage
mattbrandman 21094a7
adding pragma no cover to has_content
mattbrandman 8ac5294
Merge branch 'main' into builtin-tool
mattbrandman de8f32d
Merge branch 'main' into builtin-tool
mattbrandman 13bc865
Merge branch 'main' into builtin-tool
Kludex c0fc35c
Support WebSearch streaming for OpenAI
Kludex b4b3752
Handle Anthropic streaming
Kludex 3a2481a
Drop extra from dependencies on test
Kludex 306514e
move import up
Kludex c84be82
add coverage-n
Kludex a77030b
add coverage-n
Kludex 93e26d6
drop pragma
Kludex bfb4eef
Update pydantic_ai_slim/pydantic_ai/builtin_tools.py
Kludex 4376fc2
Apply suggestions from code review
mattbrandman c029d4b
Merge branch 'main' into builtin-tool
mattbrandman 87f59f1
updates types
mattbrandman 3fca6d4
updating types
mattbrandman 37a1845
fixing event
mattbrandman 5b031d6
Merge branch 'main' into builtin-tool
mattbrandman e1085fa
updates
mattbrandman ae1d4d9
Merge branch 'main' into builtin-tool
mattbrandman 3ee4aa9
Merge branch 'main' into builtin-tool
mattbrandman 9d24f5b
Merge branch 'main' into builtin-tool
mattbrandman 07f7892
Merge branch 'main' into builtin-tool
mattbrandman 7d867ee
Merge branch 'main' into builtin-tool
mattbrandman ba3f6b2
Update pydantic_ai_slim/pydantic_ai/agent.py
Kludex 1ae6155
addressing comments
mattbrandman 3f8cc2d
Merge branch 'main' into builtin-tool
mattbrandman ba962a7
updates
mattbrandman bfd9b21
fixing lint
mattbrandman e2fef42
fixing lint
mattbrandman b1ef2f8
fix misspellings
mattbrandman e9cacc3
rename servertool to builtin tool everywhere
mattbrandman aa91640
fixing ordering
mattbrandman 2d451a7
Merge branch 'main' into builtin-tool
mattbrandman 38b5b7d
addressing comments
mattbrandman 359b9e1
Merge upstream/main into builtin-tool branch - resolved conflicts
mattbrandman bd6e5f7
Fix failing tests after merge - update snapshots for builtin tools
mattbrandman 69267ca
fixing types
mattbrandman 1a4a0f3
Merge remote-tracking branch 'origin/main' into builtin-tool
Kludex c9d0d18
Remove deprecated stuff from yesterday
Kludex a631229
Merge branch 'main' into builtin-tool
Kludex 78fe612
Simplify cohere
Kludex a673c90
refactor a bit
Kludex df2ad47
Handle Google builtin tools
Kludex b4add90
Add test for unsupported builtin tools
Kludex dec1ba3
Add 100% coverage
Kludex e134d43
fix linting issue
Kludex ef4ab6a
Add API documentation for builtin tools
Kludex bb98ccf
Add documentation
Kludex 739f142
pass docs tests
Kludex cc7ab39
Add Groq model profile for compound models that always have web searc…
DouweM 5534bf4
add pragma
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ node_modules/ | |
**.idea/ | ||
.coverage* | ||
/test_tmp/ | ||
.mcp.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# `pydantic_ai.builtin_tools` | ||
|
||
::: pydantic_ai.builtin_tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# Builtin Tools | ||
|
||
Builtin tools are native tools provided by LLM providers that can be used to enhance your agent's capabilities. Unlike [common tools](common-tools.md), which are custom implementations that PydanticAI executes, builtin tools are executed directly by the model provider. | ||
|
||
## Overview | ||
|
||
PydanticAI supports two types of builtin tools: | ||
|
||
- **[`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool]**: Allows agents to search the web | ||
- **[`CodeExecutionTool`][pydantic_ai.builtin_tools.CodeExecutionTool]**: Enables agents to execute code in a secure environment | ||
|
||
These tools are passed to the agent via the `builtin_tools` parameter and are executed by the model provider's infrastructure. | ||
|
||
!!! warning "Provider Support" | ||
Not all model providers support builtin tools. If you use a builtin tool with an unsupported provider, PydanticAI will raise a [`UserError`][pydantic_ai.exceptions.UserError] when you try to run the agent. | ||
|
||
## Web Search Tool | ||
|
||
The [`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool] allows your agent to search the web, | ||
making it ideal for queries that require up-to-date data. | ||
|
||
### Provider Support | ||
|
||
| Provider | Supported | Notes | | ||
|----------|-----------|-------| | ||
| OpenAI | ✅ | Full feature support | | ||
| Anthropic | ✅ | Full feature support | | ||
| Groq | ✅ | Limited parameter support | | ||
| Google | ❌ | Not supported | | ||
| Bedrock | ❌ | Not supported | | ||
| Mistral | ❌ | Not supported | | ||
| Cohere | ❌ | Not supported | | ||
| HuggingFace | ❌ | Not supported | | ||
|
||
!!! note "Groq Support" | ||
To use web search capabilities with Groq, you need to use the [compound models](https://console.groq.com/docs/compound). | ||
|
||
### Usage | ||
|
||
```py title="web_search_basic.py" | ||
from pydantic_ai import Agent, WebSearchTool | ||
|
||
agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[WebSearchTool()]) | ||
|
||
result = agent.run_sync('Give me a sentence with the biggest news in AI this week.') | ||
# > Scientists have developed a universal AI detector that can identify deepfake videos. | ||
|
||
``` | ||
|
||
### Configuration Options | ||
|
||
The `WebSearchTool` supports several configuration parameters: | ||
|
||
```py title="web_search_configured.py" | ||
from pydantic_ai import Agent, WebSearchTool, WebSearchUserLocation | ||
|
||
agent = Agent( | ||
'anthropic:claude-sonnet-4-0', | ||
builtin_tools=[ | ||
WebSearchTool( | ||
search_context_size='high', | ||
user_location=WebSearchUserLocation( | ||
city='San Francisco', | ||
country='US', | ||
region='CA', | ||
timezone='America/Los_Angeles', | ||
), | ||
blocked_domains=['example.com', 'spam-site.net'], | ||
allowed_domains=None, # Cannot use both blocked_domains and allowed_domains with Anthropic | ||
max_uses=5, # Anthropic only: limit tool usage | ||
) | ||
], | ||
) | ||
|
||
result = agent.run_sync('Use the web to get the current time.') | ||
# > In San Francisco, it's 8:21:41 pm PDT on Wednesday, August 6, 2025. | ||
``` | ||
|
||
### Parameter Support by Provider | ||
|
||
| Parameter | OpenAI | Anthropic | Groq | | ||
|-----------|--------|-----------|------| | ||
| `search_context_size` | ✅ | ❌ | ❌ | | ||
| `user_location` | ✅ | ✅ | ❌ | | ||
| `blocked_domains` | ❌ | ✅ | ✅ | | ||
| `allowed_domains` | ❌ | ✅ | ✅ | | ||
| `max_uses` | ❌ | ✅ | ❌ | | ||
|
||
!!! note "Anthropic Domain Filtering" | ||
With Anthropic, you can only use either `blocked_domains` or `allowed_domains`, not both. | ||
|
||
## Code Execution Tool | ||
|
||
The [`CodeExecutionTool`][pydantic_ai.builtin_tools.CodeExecutionTool] enables your agent to execute code | ||
in a secure environment, making it perfect for computational tasks, data analysis, and mathematical operations. | ||
|
||
### Provider Support | ||
|
||
| Provider | Supported | | ||
|----------|-----------| | ||
| OpenAI | ✅ | | ||
| Anthropic | ✅ | | ||
| Google | ✅ | | ||
| Groq | ❌ | | ||
| Bedrock | ❌ | | ||
| Mistral | ❌ | | ||
| Cohere | ❌ | | ||
| HuggingFace | ❌ | | ||
|
||
### Usage | ||
|
||
```py title="code_execution_basic.py" | ||
from pydantic_ai import Agent, CodeExecutionTool | ||
|
||
agent = Agent('anthropic:claude-sonnet-4-0', builtin_tools=[CodeExecutionTool()]) | ||
|
||
result = agent.run_sync('Calculate the factorial of 15 and show your work') | ||
# > The factorial of 15 is **1,307,674,368,000**. | ||
``` | ||
|
||
## API Reference | ||
|
||
For complete API documentation, see the [API Reference](api/builtin_tools.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ nav: | |
- input.md | ||
- thinking.md | ||
- direct.md | ||
- builtin-tools.md | ||
- common-tools.md | ||
- retries.md | ||
- MCP: | ||
|
@@ -72,6 +73,7 @@ nav: | |
- api/agent.md | ||
- api/tools.md | ||
- api/toolsets.md | ||
- api/builtin_tools.md | ||
- api/common_tools.md | ||
- api/output.md | ||
- api/result.md | ||
|
@@ -118,29 +120,29 @@ extra: | |
generator: false | ||
|
||
theme: | ||
name: 'material' | ||
name: "material" | ||
custom_dir: docs/.overrides | ||
palette: | ||
- media: '(prefers-color-scheme)' | ||
- media: "(prefers-color-scheme)" | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/brightness-auto | ||
name: 'Switch to light mode' | ||
- media: '(prefers-color-scheme: light)' | ||
name: "Switch to light mode" | ||
- media: "(prefers-color-scheme: light)" | ||
scheme: default | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/brightness-7 | ||
name: 'Switch to dark mode' | ||
- media: '(prefers-color-scheme: dark)' | ||
name: "Switch to dark mode" | ||
- media: "(prefers-color-scheme: dark)" | ||
scheme: slate | ||
primary: pink | ||
accent: pink | ||
toggle: | ||
icon: material/brightness-4 | ||
name: 'Switch to system preference' | ||
name: "Switch to system preference" | ||
features: | ||
- search.suggest | ||
- search.highlight | ||
|
@@ -153,8 +155,8 @@ theme: | |
- navigation.sections | ||
- navigation.tracking | ||
- toc.follow | ||
logo: 'img/logo-white.svg' | ||
favicon: 'favicon.ico' | ||
logo: "img/logo-white.svg" | ||
favicon: "favicon.ico" | ||
|
||
# https://www.mkdocs.org/user-guide/configuration/#validation | ||
validation: | ||
|
@@ -164,13 +166,13 @@ validation: | |
anchors: warn | ||
|
||
extra_css: | ||
- 'extra/tweaks.css' | ||
- "extra/tweaks.css" | ||
# used for analytics | ||
extra_javascript: | ||
- '/flarelytics/client.js' | ||
- 'https://cdn.jsdelivr.net/npm/[email protected]/dist/lite/builds/browser.umd.js' | ||
- 'https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.production.min.js' | ||
- '/javascripts/algolia-search.js' | ||
- "/flarelytics/client.js" | ||
- "https://cdn.jsdelivr.net/npm/[email protected]/dist/lite/builds/browser.umd.js" | ||
- "https://cdn.jsdelivr.net/npm/[email protected]/dist/instantsearch.production.min.js" | ||
- "/javascripts/algolia-search.js" | ||
|
||
markdown_extensions: | ||
- tables | ||
|
@@ -272,5 +274,5 @@ plugins: | |
- examples/*.md | ||
|
||
hooks: | ||
- 'docs/.hooks/main.py' | ||
- 'docs/.hooks/algolia.py' | ||
- "docs/.hooks/main.py" | ||
- "docs/.hooks/algolia.py" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For curiosity, why MCP servers did you use here?