Structured Output Agent

Code

1from typing import List
2
3from kern.agent import Agent, RunOutput # noqa
4from kern.models.portkey import Portkey
5from pydantic import BaseModel, Field
6
7class MovieScript(BaseModel):
8 setting: str = Field(
9 ..., description="Provide a nice setting for a blockbuster movie."
10 )
11 ending: str = Field(
12 ...,
13 description="Ending of the movie. If not available, provide a happy ending.",
14 )
15 genre: str = Field(
16 ...,
17 description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
18 )
19 name: str = Field(..., description="Give a name to this movie")
20 characters: List[str] = Field(..., description="Name of characters for this movie.")
21 storyline: str = Field(
22 ..., description="3 sentence storyline for the movie. Make it exciting!"
23 )
24
25agent = Agent(
26 model=Portkey(id="@first-integrati-707071/gpt-5-nano"),
27 output_schema=MovieScript,
28 markdown=True,
29)
30
31# Get the response in a variable
32# run: RunOutput = agent.run("New York")
33# print(run.content)
34
35agent.print_response("New York")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API keys

1export PORTKEY_API_KEY=***
2export PORTKEY_VIRTUAL_KEY=***

Install dependencies

1uv pip install -U kern-ai

Run Agent

1python cookbook/11_models/portkey/structured_output.py