Skip to content

Contravariance of Tool[AgentDepsT] does not seem to be respected by inferred Tool[None] #2977

@lukekh

Description

@lukekh

Initial Checks

Description

If my agent has any deps_type specified then tools that have no dependencies (e.g. the DuckDuckGoSearchTool from pydantic_ai.common_tools.duckduckgo) are type Tool[None] and cause the tool to complain by contravariance of the Tool type.

Shouldn't a Tool with no dependencies default to Tool[Any] to correctly use contravariant types?

I've gotten around this in my own code by just explicitly typing my tools like this:

ddgs_tool: Tool[Any] = duckduckgo_search_tool(max_results=5)

to avoid the type complaints from my linter (Pylance).

Example Code

from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.common_tools.duckduckgo import duckduckgo_search_tool

class AgentDeps(BaseModel):
    foo: str
    bar: int

agent = Agent(
    deps_type=AgentDeps,
    tools=[duckduckgo_search_tool()]
)

Python, Pydantic AI & LLM client version

Python 3.11.13
pydantic-ai==1.0.10
openai==1.108.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions