When I run a sample agent with structured output, I get a correct response according to the pydantic model.
structured_output_agent.print_response("Write a movie about scarry AI agents")
However, when trying the agent in the Playground I get an error:
TypeError: async for' requires an object with __aiter__ method, got RunResponse
Does the Playground support structured output?
Here is the code:
from typing import List
from pydantic import BaseModel, Field
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.playground import Playground, serve_playground_app
class MovieScript(BaseModel):
setting: str = Field(..., description="Provide a nice setting for a blockbuster movie.")
ending: str = Field(..., description="Ending of the movie. If not available, provide a happy ending.")
genre: str = Field(
..., description="Genre of the movie. If not available, select action, thriller or romantic comedy."
)
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(..., description="3 sentence storyline for the movie. Make it exciting!")
# Agent that uses structured outputs
structured_output_agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
description="You write movie scripts.",
response_model=MovieScript,
structured_outputs=True,
stream_response=True,
)
# Get the response in a variable
structured_output_agent.print_response("Write a movie about scarry AI agents")
# Create the playground app
app = Playground(agents=[structured_output_agent]).get_app()
if __name__ == "__main__":
serve_playground_app("test_agent:app", reload=True)