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

Conversation

fswair
Copy link
Contributor

@fswair fswair commented Jul 18, 2025

The built-in errors comes from _function_schema origin, related lines, 100, 238, 245.

The issue is basically comes from built-in functions. If we can register a built-in function as a tool, it raise errors.

Handle errors better in tool registry
@fswair fswair changed the title Handle tools errors better in tool registry Handle tool errors better in tool registry Jul 18, 2025
@fswair
Copy link
Contributor Author

fswair commented Jul 18, 2025

Tests:

import pytest


def test_builtin_tool_registry():
    """
    Test that built-in functions can be registered as tools.
    """
    from pydantic_ai import Agent, Tool
    from pydantic_ai.exceptions import UserError
    from pydantic_ai.models.test import TestModel
    
    with pytest.raises(UserError):
        Agent(TestModel(), tools=[max])
    
    with pytest.raises(UserError):
        agent = Agent(TestModel())
        agent.tool_plain(min)
    
    agent = Agent(TestModel(), tools=[print])
    assert 'print' in agent._function_toolset.tools
    
    agent._function_toolset.tools.pop('print', None)
    
    agent._function_toolset.add_tool(Tool(print))
    assert 'print' in agent._function_toolset.tools

@DouweM
Copy link
Collaborator

DouweM commented Jul 22, 2025

@fswair Thank you! Can you please add some tests?

@fswair
Copy link
Contributor Author

fswair commented Jul 22, 2025

@DouweM im so tired with this checks

@fswair
Copy link
Contributor Author

fswair commented Jul 22, 2025

can u check again? @DouweM

@fswair fswair requested a review from DouweM July 22, 2025 15:27
@fswair fswair requested a review from DouweM July 22, 2025 16:04
@fswair fswair requested a review from DouweM July 22, 2025 16:51
@fswair fswair changed the title Handle tool errors better in tool registry Handle tool errors better in tool registration Jul 22, 2025
@fswair fswair changed the title Handle tool errors better in tool registration Handle built-in tool errors better in tool registration Jul 22, 2025
@DouweM DouweM merged commit 1e18729 into pydantic:main Jul 23, 2025
18 checks passed
@DouweM
Copy link
Collaborator

DouweM commented Jul 23, 2025

@fswair Thanks Mert!

@fswair
Copy link
Contributor Author

fswair commented Jul 23, 2025

@fswair Thanks Mert!

Welcome 🔥🚀

@fswair fswair deleted the handle_builtin_tool_issues_on_registry branch July 23, 2025 14:00
KRRT7 pushed a commit to aseembits93/pydantic-ai that referenced this pull request Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle tool errors better in tool registry
2 participants