Skip to content

MyPy issues when using DeferredToolRequests #3403

@andrzej-pomirski-yohana

Description

Initial Checks

Description

See sample code. The mypy typechecker is not able to infer the correct output type. I am unsure if it's even possible to do anything about it due to the runtime (probably) required to infer agent.output_type, so it might just be a case where a type ignore is required. I also tried the union operator instead of list elements, but then I encountered another bug (No overload variant of "run_stream" of "AbstractAgent" matches argument type "UnionType")

The code below results in error: Need type annotation for "result" [var-annotated].

Example Code

from pydantic_ai import Agent, DeferredToolRequests


async def run() -> None:
    agent = Agent(
        name="Chat Agent",
    )
    async with agent.run_stream(
        output_type=[agent.output_type, DeferredToolRequests]
    ) as result:
        async for _response in result.stream_output():
            pass


if __name__ == "__main__":
    import asyncio

    asyncio.run(run())

Python, Pydantic AI & LLM client version

Python 3.12
pydantic-ai-slim 1.14.1
mypy 1.18.2

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