Structured Output Agent

Return a validated Pydantic object from a Portkey agent.

Create a provider integration in your Portkey Model Catalog, then copy an allowed model string in the form @provider_slug/model_name. Replace @your-provider-slug/gpt-5-nano in the example with that value. The slug is specific to your workspace. These catalog examples need PORTKEY_API_KEY; they do not require a separate virtual key. Pass the key explicitly as portkey_api_key, because the Agno adapter does not read that environment variable automatically.

Code

import os
from typing import List

from agno.agent import Agent, RunOutput  # noqa
from agno.models.portkey import Portkey
from pydantic import BaseModel, Field

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 = Agent(
    model=Portkey(
        id="@your-provider-slug/gpt-5-nano",
        portkey_api_key=os.getenv("PORTKEY_API_KEY"),
    ),
    output_schema=MovieScript,
    markdown=True,
)

# Get the response in a variable
# run: RunOutput = agent.run("New York")
# print(run.content)

agent.print_response("New York")

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Set your Portkey API key

export PORTKEY_API_KEY=***

Install dependencies

uv pip install -U portkey-ai openai agno

Run Agent

Save the code above as structured_output.py, then run:

python structured_output.py