Structured Output
Code
1from typing import List2from kern.agent import Agent, RunOutput # noqa3from kern.models.ollama import Ollama4from pydantic import BaseModel, Field5from rich.pretty import pprint # noqa67class 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 )2425# Agent that returns a structured output26structured_output_agent = Agent(27 model=Ollama(id="llama3.2"),28 description="You write movie scripts.",29 output_schema=MovieScript,30)3132# Get the response in a variable33# 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)3738# Run the agent39structured_output_agent.print_response("New York")Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall Ollama
Follow the Ollama installation guide and run:
1ollama pull llama3.2Install dependencies
1uv pip install -U ollama kern-aiRun Agent
1python cookbook/11_models/ollama/structured_output.py