Structured Output

Code

1from typing import List
2from kern.agent import Agent, RunOutput # noqa
3from kern.models.ollama import Ollama
4from pydantic import BaseModel, Field
5from rich.pretty import pprint # noqa
6
7class MovieScript(BaseModel):
8 name: str = Field(..., description="Give a name to this movie")
9 setting: str = Field(
10 ..., description="Provide a nice setting for a blockbuster movie."
11 )
12 ending: str = Field(
13 ...,
14 description="Ending of the movie. If not available, provide a happy ending.",
15 )
16 genre: str = Field(
17 ...,
18 description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
19 )
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
25# Agent that returns a structured output
26structured_output_agent = Agent(
27 model=Ollama(id="llama3.2"),
28 description="You write movie scripts.",
29 output_schema=MovieScript,
30)
31
32# Get the response in a variable
33# json_mode_response: RunOutput = json_mode_agent.run("New York")
34# pprint(json_mode_response.content)
35# structured_output_response: RunOutput = structured_output_agent.run("New York")
36# pprint(structured_output_response.content)
37
38# Run the agent
39structured_output_agent.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

Install Ollama

Follow the Ollama installation guide and run:

1ollama pull llama3.2

Install dependencies

1uv pip install -U ollama kern-ai

Run Agent

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