Skip to content

Commit cd91e81

Browse files
committed
move supported_builtin_tools to abstract tool set type
1 parent 85f63bc commit cd91e81

File tree

12 files changed

+64
-54
lines changed

12 files changed

+64
-54
lines changed

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ def get_weather(city: str) -> str:
15381538
# Then run with: uvicorn app:app --reload
15391539
```
15401540
"""
1541-
from ..ui.web import create_web_app
1541+
from ..ui._web import create_web_app
15421542

15431543
return create_web_app(self, models=models, builtin_tools=builtin_tools)
15441544

pydantic_ai_slim/pydantic_ai/builtin_tools.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
'ImageGenerationTool',
1919
'MemoryTool',
2020
'MCPServerTool',
21-
'BUILTIN_TOOL_CLASSES',
21+
'get_builtin_tool_ids',
22+
'get_builtin_tool_types',
23+
'get_builtin_tool_cls',
2224
)
2325

2426
_BUILTIN_TOOL_TYPES: dict[str, type[AbstractBuiltinTool]] = {}
@@ -415,21 +417,20 @@ def _tool_discriminator(tool_data: dict[str, Any] | AbstractBuiltinTool) -> str:
415417
return tool_data.kind
416418

417419

418-
BUILTIN_TOOL_ID = Literal[
419-
'web_search',
420-
'code_execution',
421-
'image_generation',
422-
'web_fetch',
423-
'memory',
424-
'mcp_server',
425-
]
426-
427-
ACTIVE_BUILTIN_TOOL_IDS: frozenset[str] = frozenset(BUILTIN_TOOL_ID.__args__)
428-
429-
BUILTIN_TOOL_CLASSES: dict[BUILTIN_TOOL_ID, type[AbstractBuiltinTool]] = {
430-
'web_search': WebSearchTool,
431-
'code_execution': CodeExecutionTool,
432-
'image_generation': ImageGenerationTool,
433-
'web_fetch': WebFetchTool,
434-
'memory': MemoryTool,
435-
}
420+
def get_builtin_tool_ids() -> frozenset[str]:
421+
"""Get the set of all builtin tool IDs (excluding deprecated tools like url_context)."""
422+
return frozenset(_BUILTIN_TOOL_TYPES.keys() - {'url_context'})
423+
424+
425+
def get_builtin_tool_types() -> frozenset[type[AbstractBuiltinTool]]:
426+
"""Get the set of all builtin tool types (excluding deprecated tools like UrlContextTool)."""
427+
return frozenset(cls for kind, cls in _BUILTIN_TOOL_TYPES.items() if kind != 'url_context')
428+
429+
430+
def get_builtin_tool_cls(tool_id: str) -> type[AbstractBuiltinTool] | None:
431+
"""Get a builtin tool class by its ID.
432+
433+
Args:
434+
tool_id: The tool ID (e.g., 'web_search', 'code_execution')
435+
"""
436+
return _BUILTIN_TOOL_TYPES.get(tool_id)

pydantic_ai_slim/pydantic_ai/models/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ def model_name(self) -> str:
495495
raise NotImplementedError()
496496

497497
@classmethod
498-
def supported_builtin_tools(cls) -> frozenset[str]:
499-
"""Return the set of builtin tool IDs this model class can handle.
498+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
499+
"""Return the set of builtin tool types this model class can handle.
500500
501501
Subclasses should override this to reflect their actual capabilities.
502502
Default is empty set - subclasses must explicitly declare support.

pydantic_ai_slim/pydantic_ai/models/anthropic.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
from .. import ModelHTTPError, UnexpectedModelBehavior, _utils, usage
1414
from .._run_context import RunContext
1515
from .._utils import guard_tool_call_id as _guard_tool_call_id
16-
from ..builtin_tools import CodeExecutionTool, MCPServerTool, MemoryTool, WebFetchTool, WebSearchTool
16+
from ..builtin_tools import (
17+
AbstractBuiltinTool,
18+
CodeExecutionTool,
19+
MCPServerTool,
20+
MemoryTool,
21+
WebFetchTool,
22+
WebSearchTool,
23+
)
1724
from ..exceptions import ModelAPIError, UserError
1825
from ..messages import (
1926
BinaryContent,
@@ -255,9 +262,9 @@ def system(self) -> str:
255262
return self._provider.name
256263

257264
@classmethod
258-
def supported_builtin_tools(cls) -> frozenset[str]:
259-
"""Return the set of builtin tool IDs this model class can handle."""
260-
return frozenset({'web_search', 'code_execution', 'web_fetch', 'memory', 'mcp_server'})
265+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
266+
"""Return the set of builtin tool types this model class can handle."""
267+
return frozenset({WebSearchTool, CodeExecutionTool, WebFetchTool, MemoryTool, MCPServerTool})
261268

262269
async def request(
263270
self,

pydantic_ai_slim/pydantic_ai/models/function.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .. import _utils, usage
1515
from .._run_context import RunContext
1616
from .._utils import PeekableAsyncStream
17+
from ..builtin_tools import AbstractBuiltinTool
1718
from ..messages import (
1819
BinaryContent,
1920
BuiltinToolCallPart,
@@ -201,11 +202,11 @@ def system(self) -> str:
201202
return self._system
202203

203204
@classmethod
204-
def supported_builtin_tools(cls) -> frozenset[str]:
205+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
205206
"""FunctionModel supports all builtin tools for testing flexibility."""
206-
from ..builtin_tools import ACTIVE_BUILTIN_TOOL_IDS
207+
from ..builtin_tools import get_builtin_tool_types
207208

208-
return ACTIVE_BUILTIN_TOOL_IDS
209+
return get_builtin_tool_types()
209210

210211

211212
@dataclass(frozen=True, kw_only=True)

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import UnexpectedModelBehavior, _utils, usage
1414
from .._output import OutputObjectDefinition
1515
from .._run_context import RunContext
16-
from ..builtin_tools import CodeExecutionTool, ImageGenerationTool, WebFetchTool, WebSearchTool
16+
from ..builtin_tools import AbstractBuiltinTool, CodeExecutionTool, ImageGenerationTool, WebFetchTool, WebSearchTool
1717
from ..exceptions import ModelAPIError, ModelHTTPError, UserError
1818
from ..messages import (
1919
BinaryContent,
@@ -230,9 +230,9 @@ def system(self) -> str:
230230
return self._provider.name
231231

232232
@classmethod
233-
def supported_builtin_tools(cls) -> frozenset[str]:
234-
"""Return the set of builtin tool IDs this model class can handle."""
235-
return frozenset({'web_search', 'code_execution', 'web_fetch', 'image_generation'})
233+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
234+
"""Return the set of builtin tool types this model class can handle."""
235+
return frozenset({WebSearchTool, CodeExecutionTool, WebFetchTool, ImageGenerationTool})
236236

237237
def prepare_request(
238238
self, model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters

pydantic_ai_slim/pydantic_ai/models/groq.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .._run_context import RunContext
1616
from .._thinking_part import split_content_into_text_and_thinking
1717
from .._utils import generate_tool_call_id, guard_tool_call_id as _guard_tool_call_id, number_to_datetime
18-
from ..builtin_tools import WebSearchTool
18+
from ..builtin_tools import AbstractBuiltinTool, WebSearchTool
1919
from ..exceptions import ModelAPIError, UserError
2020
from ..messages import (
2121
BinaryContent,
@@ -180,9 +180,9 @@ def system(self) -> str:
180180
return self._provider.name
181181

182182
@classmethod
183-
def supported_builtin_tools(cls) -> frozenset[str]:
184-
"""Return the set of builtin tool IDs this model class can handle."""
185-
return frozenset({'web_search'})
183+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
184+
"""Return the set of builtin tool types this model class can handle."""
185+
return frozenset({WebSearchTool})
186186

187187
async def request(
188188
self,

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .._run_context import RunContext
2020
from .._thinking_part import split_content_into_text_and_thinking
2121
from .._utils import guard_tool_call_id as _guard_tool_call_id, now_utc as _now_utc, number_to_datetime
22-
from ..builtin_tools import CodeExecutionTool, ImageGenerationTool, MCPServerTool, WebSearchTool
22+
from ..builtin_tools import AbstractBuiltinTool, CodeExecutionTool, ImageGenerationTool, MCPServerTool, WebSearchTool
2323
from ..exceptions import UserError
2424
from ..messages import (
2525
AudioUrl,
@@ -425,9 +425,9 @@ def system(self) -> str:
425425
return self._provider.name
426426

427427
@classmethod
428-
def supported_builtin_tools(cls) -> frozenset[str]:
429-
"""Return the set of builtin tool IDs this model class can handle."""
430-
return frozenset({'web_search'})
428+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
429+
"""Return the set of builtin tool types this model class can handle."""
430+
return frozenset({WebSearchTool})
431431

432432
@property
433433
@deprecated('Set the `system_prompt_role` in the `OpenAIModelProfile` instead.')
@@ -1097,9 +1097,9 @@ def system(self) -> str:
10971097
return self._provider.name
10981098

10991099
@classmethod
1100-
def supported_builtin_tools(cls) -> frozenset[str]:
1101-
"""Return the set of builtin tool IDs this model class can handle."""
1102-
return frozenset({'web_search', 'code_execution', 'mcp_server', 'image_generation'})
1100+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
1101+
"""Return the set of builtin tool types this model class can handle."""
1102+
return frozenset({WebSearchTool, CodeExecutionTool, MCPServerTool, ImageGenerationTool})
11031103

11041104
async def request(
11051105
self,

pydantic_ai_slim/pydantic_ai/models/test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .. import _utils
1515
from .._run_context import RunContext
16+
from ..builtin_tools import AbstractBuiltinTool
1617
from ..exceptions import UserError
1718
from ..messages import (
1819
BuiltinToolCallPart,
@@ -157,11 +158,11 @@ def system(self) -> str:
157158
return self._system
158159

159160
@classmethod
160-
def supported_builtin_tools(cls) -> frozenset[str]:
161+
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
161162
"""TestModel supports all builtin tools for testing flexibility."""
162-
from ..builtin_tools import ACTIVE_BUILTIN_TOOL_IDS
163+
from ..builtin_tools import get_builtin_tool_types
163164

164-
return ACTIVE_BUILTIN_TOOL_IDS
165+
return get_builtin_tool_types()
165166

166167
def gen_tool_args(self, tool_def: ToolDefinition) -> Any:
167168
return _JsonSchemaTestData(tool_def.parameters_json_schema, self.seed).generate()

pydantic_ai_slim/pydantic_ai/profiles/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing_extensions import Self
88

99
from .._json_schema import InlineDefsJsonSchemaTransformer, JsonSchemaTransformer
10+
from ..builtin_tools import AbstractBuiltinTool, get_builtin_tool_types
1011
from ..output import StructuredOutputMode
1112

1213
__all__ = [
@@ -65,11 +66,11 @@ class ModelProfile:
6566
This is currently only used by `OpenAIChatModel`, `HuggingFaceModel`, and `GroqModel`.
6667
"""
6768

68-
supported_builtin_tools: frozenset[str] = field(default_factory=lambda: frozenset[str]())
69-
"""The set of builtin tool IDs that this model/profile supports.
69+
supported_builtin_tools: frozenset[type[AbstractBuiltinTool]] = field(default_factory=get_builtin_tool_types)
70+
"""The set of builtin tool types that this model/profile supports.
7071
71-
Defaults to empty (no builtin tools). Profile functions should explicitly
72-
set this based on model capabilities.
72+
Defaults to ALL builtin tools. Profile functions should explicitly
73+
restrict this based on model capabilities.
7374
"""
7475

7576
@classmethod

0 commit comments

Comments
 (0)