Skip to content

Handle built-in tool errors better in tool registration #2252

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
merged 23 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pydantic_ai_slim/pydantic_ai/_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,20 @@ def function_schema( # noqa: C901
config = ConfigDict(title=function.__name__, use_attribute_docstrings=True)
config_wrapper = ConfigWrapper(config)
gen_schema = _generate_schema.GenerateSchema(config_wrapper)
errors: list[str] = []

sig = signature(function)
try:
sig = signature(function)
except ValueError as e:
errors.append(str(e))
sig = signature(lambda: None)

type_hints = _typing_extra.get_function_type_hints(function)

var_kwargs_schema: core_schema.CoreSchema | None = None
fields: dict[str, core_schema.TypedDictField] = {}
positional_fields: list[str] = []
var_positional_field: str | None = None
errors: list[str] = []
decorators = _decorators.DecoratorInfos()

description, field_descriptions = doc_descriptions(function, sig, docstring_format=docstring_format)
Expand Down Expand Up @@ -235,14 +239,19 @@ def _takes_ctx(function: TargetFunc[P, R]) -> TypeIs[WithCtx[P, R]]:
Returns:
`True` if the function takes a `RunContext` as first argument, `False` otherwise.
"""
sig = signature(function)
try:
sig = signature(function)
except ValueError: # pragma: no cover
return False # pragma: no cover
try:
first_param_name = next(iter(sig.parameters.keys()))
except StopIteration:
return False
else:
type_hints = _typing_extra.get_function_type_hints(function)
annotation = type_hints[first_param_name]
annotation = type_hints.get(first_param_name)
if annotation is None:
return False # pragma: no cover
return True is not sig.empty and _is_call_ctx(annotation)


Expand Down
20 changes: 20 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ async def invalid_tool(ctx: RunContext[None]) -> str: # pragma: no cover
)


def test_builtin_tool_registration():
"""
Test that built-in functions can't be registered as tools.
"""

with pytest.raises(
UserError,
match='Error generating schema for min:\n no signature found for builtin <built-in function min>',
):
agent = Agent(TestModel())
agent.tool_plain(min)

with pytest.raises(
UserError,
match='Error generating schema for max:\n no signature found for builtin <built-in function max>',
):
agent = Agent(TestModel())
agent.tool_plain(max)


def test_tool_ctx_second():
agent = Agent(TestModel())

Expand Down