Handling structured output in Agent Teams

Hi Phidata community!
I’m working on building an agent team for content creation with structured output (using Pydantic models) and ran into a type validation error. Here’s my current setup:

from phi.agent import Agent
from phi.model.anthropic import Claude
from pydantic import BaseModel

class Article(BaseModel):
    title: str
    sections: List[Section]
    main_sources: List[Source]
    internal_links: List[str]

editor = Agent(
    name="Editor",
    model=Claude(id=CLAUDE_MODEL_ID, api_key=ANTHROPIC_API_KEY),
    team=[planner, researcher, writer],
    response_model=Article,
    show_tool_calls=True,
    markdown=True,
)

When trying to run the agent team, I’m getting a TypeError: issubclass() arg 1 must be a class error when the framework tries to validate the response against my Pydantic model.

Has anyone encountered this issue when working with structured outputs in agent teams?

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!

Traceback (most recent call last):
  File "c:\Users\username\Documents\agent-hub\agents\writer_agent.py", line 191, in <module>
    editor.print_response(f"Create a comprehensive article about {topic}")
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\agent\agent.py", line 2905, in print_response
    run_response = self.run(message=message, messages=messages, stream=False, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\agent\agent.py", line 1996, in run
    run_response: RunResponse = next(
                                ^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\agent\agent.py", line 1841, in _run
    model_response = self.model.response(messages=messages_for_model)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\model\anthropic\claude.py", line 542, in response
    if self.handle_tool_calls(assistant_message, messages, model_response, response_content, tool_ids):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\model\anthropic\claude.py", line 499, in handle_tool_calls
    for * in self.run*function_calls(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\model\base.py", line 244, in run_function_calls
    for item in function_call.result:
                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Roaming\Python\Python312\site-packages\phi\agent\agent.py", line 399, in *transfer*task_to_agent
    elif issubclass(member_agent_run_response.content, BaseModel):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 123, in **subclasscheck**
TypeError: issubclass() arg 1 must be a class

Thanks for raising. This is fixed in the next release.

1 Like