Error when using Agent.run() with response_model and stream=True

Hi Phidata community! I’ve encountered an interesting edge case when using an Agent with both a Pydantic response_model and streaming enabled. Here’s my setup:

from phi.agent import Agent
from phi.model.anthropic import Claude
from pydantic import BaseModel, Field
from typing import List

class Article(BaseModel):
    """Represents a complete article"""
    title: str
    sections: List[Section]
    main_sources: List[Source] = Field(default_factory=list)  
    internal_links: List[str] = Field(default_factory=list)

writer_agent = Agent(
    model=Claude(id=CLAUDE_MODEL_ID, api_key=ANTHROPIC_API_KEY),
    team=[planner_agent, researcher_agent, content_agent],
    response_model=Article,
    markdown=True,
    debug_mode=True
)

When using agent.run() with stream=True, I’m getting:

  1. Error: 'tuple' object has no attribute 'content'
  2. Warning: Failed to convert response to response_model

However, using agent.print_response() works fine and correctly handles the Pydantic model conversion. Issue #1871.

Hi @Southern_Push2935
Thank you for reaching out and using Phidata! I’ve tagged the relevant engineers to assist you with your query. We aim to respond within 48 hours.
If this is urgent, please feel free to let us know, and we’ll do our best to prioritize it.
Thanks for your patience!

Can tool calls at least be streamed while the response is being generated?

@Southern_Push2935
Unfortunately we don’t currently support streaming and response_model together. We are working on better error handling and a better developer experience here, because I agree it should be possible.

1 Like

I think you’re the same person who replied to me on the GitHub issue - thanks! I was wondering if there might be any alternative approaches, like catching events in the RunResponse since I noticed properties like tool_call_started = "ToolCallStarted" and tool_call_completed = "ToolCallCompleted". Not sure if these would work asynchronously though?

You can look at those events as you stream run_responses, but as to the structured output it is something we’d have to build to support streaming. I could imagine it should come as the last event of streaming.

1 Like