Skip to content

Add builtin_tools to Agent #1722

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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Add builtin_tools to Agent #1722

wants to merge 23 commits into from

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented May 14, 2025

No description provided.

Comment on lines +345 to +349
for tool in builtin_tools:
if tool == 'web-search':
self._builtin_tools.append(WebSearchTool())
else:
self._builtin_tools.append(tool)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to not have to handle string on the models, so we already do the transformation here.

"""


class UserLocation(TypedDict, total=False):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to handle this in the models if it's a TypedDict, since it matches the type.

Comment on lines +33 to +34
@dataclass
class WebSearchTool(AbstractBuiltinTool):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the DX POV, it's nicer for it to be a BaseModel or dataclass.

Copy link

github-actions bot commented May 14, 2025

Docs Preview

commit: 800a71a
Preview URL: https://88c45a71-pydantic-ai-previews.pydantic.workers.dev

Comment on lines 622 to +623
tools = list(model_settings.get('openai_builtin_tools', [])) + tools
tools = self._get_builtin_tools(model_request_parameters) + tools
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should deprecate the openai_builtin_tools in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we'll add support for FileSearchToolParam and ComputerToolParam as well?

Note that https://platform.openai.com/docs/guides/tools-file-search also results in a "type": "file_search_call" output item.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to allow people to use arbitrary built-in tools we haven't created a class for yet by passing the appropriate JSON?

class AbstractBuiltinTool(ABC):
"""A builtin tool that can be used by an agent.

This class is abstract and cannot be instantiated directly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think worth including a sentence here explaining how the code execution works to make use of them — something like "these are passed to the model as part of the ModelRequestParameters" or whatever. (Not sure if that's true, haven't gotten there yet ..). But I imagine it helping someone who is trying to figure out how they are different from normal tools.

Copy link
Contributor

@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than tests and docs

@Kludex Kludex marked this pull request as draft May 15, 2025 16:09
Copy link
Contributor

hyperlint-ai bot commented May 30, 2025

PR Change Summary

Enhanced documentation for the Agent class and its output handling, including updates on transport methods and output functions.

  • Updated transport method descriptions for MCPServerHTTP to reflect the Streamable HTTP transport.
  • Clarified the use of output functions and their role in agent runs.
  • Added examples demonstrating the use of output functions and structured outputs.

Modified Files

  • docs/mcp/client.md
  • docs/multi-agent-applications.md
  • docs/output.md
  • docs/tools.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

@Kludex Kludex force-pushed the add-builtin-tools branch from 65fa051 to 5f89444 Compare June 1, 2025 11:37
@mattbrandman
Copy link

https://github.com/mattbrandman/pydantic-ai/pull/1/files Working on a fork that incorporates the new responses api code interpreter and handles streaming. I'm sure code quality is questionable but feel free to incorporate anything thats useful!

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 622 to +623
tools = list(model_settings.get('openai_builtin_tools', [])) + tools
tools = self._get_builtin_tools(model_request_parameters) + tools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we'll add support for FileSearchToolParam and ComputerToolParam as well?

Note that https://platform.openai.com/docs/guides/tools-file-search also results in a "type": "file_search_call" output item.

user_location=user_location,
)
)
elif isinstance(tool, CodeExecutionTool):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want an else -> raise "Unsupported built-in tool" here as well (and in OpenAI and Groq, looks like only Google has it currently)

def _get_builtin_tools(self, model_request_parameters: ModelRequestParameters) -> list[responses.ToolParam]:
tools: list[responses.ToolParam] = []
for tool in model_request_parameters.builtin_tools:
if isinstance(tool, WebSearchTool):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to implement CodeExecutionTool here, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linked commit I have has a basic implementation. Not sure if it would be best to open a PR into this branch or add it after this is merged. There’s unfortunately a bunch of type errors (or were last I checked) from OpenAI where required fields are empty so need a series of guards that look redundant to checkers

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, chat completions doesn't have it.

Comment on lines 622 to +623
tools = list(model_settings.get('openai_builtin_tools', [])) + tools
tools = self._get_builtin_tools(model_request_parameters) + tools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to allow people to use arbitrary built-in tools we haven't created a class for yet by passing the appropriate JSON?

@@ -694,6 +721,20 @@ def _get_tools(self, model_request_parameters: ModelRequestParameters) -> list[r
tools += [self._map_tool_definition(r) for r in model_request_parameters.output_tools]
return tools

def _get_builtin_tools(self, model_request_parameters: ModelRequestParameters) -> list[responses.ToolParam]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case I was thinking of that I mentioned on the call this morning is that someone may use OpenAIModel with OpenRouterProvider and an Anthropic model, and it's possible that the built-in tool specifications should then match those for Anthropic rather than those for OpenAI. But from a quick search it doesn't look like OpenRouter currently supports built-in tools (at least there's no doc on it), and when they do they'll probably require a OR-specific specification that they'll translate to the model-specific specs behind the scenes, so we don't have to worry about that for now.

@mattbrandman
Copy link

besides having tests pass is there anything else holding back this PR seems like it will grow in importance as more builtin tools are introduced?

@mattbrandman
Copy link

@Kludex not sure if this will be helpful but have a branch based off this with merge conflicts handled and tests running properly. Don't know if I did everything right and also think I should probably have a cleaner one that could be merged into this but my git-foo is failing me late at night will see if I can make a cleaner version mattbrandman#6

@DouweM DouweM mentioned this pull request Jul 1, 2025
@DouweM
Copy link
Contributor

DouweM commented Jul 1, 2025

@mattbrandman Thanks for creating the #2102 PR! @Kludex is out this week but I'm sure he'll be happy to see the work you've saved him when he's back :)

@mattbrandman
Copy link

@DouweM no problem! I'm happy to have them merged back into here just couldn't figure out a clean PR strategy since I had to merge main to resolve the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants