Skip to content

How to stream tool usage and final answers in MCP calls with pydantic-ai? #2497

@gscfwid

Description

@gscfwid

Question

I’m trying to achieve real-time display of tool usage during MCP calls in pydantic-ai, and also stream the final answer as it is generated.

Here’s what I’ve tried so far:
• Using ‎agent.iter allows me to display tool usage in real time, but it doesn’t support streaming the final answer.
• Using the following pattern:

async with agent.run_stream(prompt) as result:
    for message in result.new_messages():
        for part in message.parts:
            # handle output

This outputs all tool usage results at once after all calls are finished, and only then streams the final answer. It does not show tool usage in real time.

Expected vs Actual Behavior
• Expected: I want tool usage to be displayed as each tool is called, and the final answer to be streamed as it is generated.
• Actual: Either I get real-time tool usage but no streamed answer, or I get a streamed answer but tool usage is only shown after all calls are complete.

Use Case
I need users to see which tools are being called as the agent works, and for the answer to be streamed, not output all at once.

Code Snippet

async with agent.run_stream(prompt) as result:
    for message in result.new_messages():
        for part in message.parts:
            # handle output

Questions
• Is there a recommended way to achieve both real-time tool usage display and streaming of the final answer?
• Are there APIs or patterns in pydantic-ai that support this?
• If not currently supported, is this feature planned for future releases?

Additional Context

Pydantic AI version: 0.6.2
Python version: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions