Skip to content

Built-in tool should allow for dynamic configuration #3555

@hayescode

Description

@hayescode

Description

I would like to configure the built-in WebSearchTool with the user's location set WebSearchUserLocation from RunContext. We can do this with toolsets and tools, but the not built-in tools.

  • tools: tools: Sequence[Tool[None] | ToolFuncContext[None, ...] | ToolFuncPlain[...]]
  • toolsets: toolsets: Sequence[AbstractToolset[None] | ToolsetFunc[None]] | None
  • builtin_tools: builtin_tools: Sequence[AbstractBuiltinTool]

I would be great to be able to do something like this:

from dataclasses import dataclass
from pydantic_ai import Agent, RunContext, Tool, ToolDefinition, WebSearchTool, WebSearchUserLocation

@dataclass
class UserContext:
    location: str


async def prepared_web_search(ctx: RunContext[UserContext]) -> WebSearchTool | None:
    return WebSearchTool(
        search_context_size="medium",
        user_location=WebSearchUserLocation(type="approximate", location=ctx.deps.location),
    )


user_context = UserContext(location="New York, NY, USA")

agent = Agent(builtin_tools=[prepared_web_search], deps_type=UserContext)
result = await agent.run("Search the web for the capital of France.", deps=user_context)

References

https://ai.pydantic.dev/builtin-tools/#web-search-tool

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions