Structured Output

Constrain a local LlamaCpp agent to return a MovieScript Pydantic object instead of free-form text.

Code

from typing import List

from agno.agent import Agent
from agno.models.llama_cpp import LlamaCpp
from agno.run.agent import RunOutput
from pydantic import BaseModel, Field
from rich.pretty import pprint  # noqa

class MovieScript(BaseModel):
    name: str = Field(..., description="Give a name to this movie")
    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.",
    )
    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 returns a structured output
structured_output_agent = Agent(
    model=LlamaCpp(id="ggml-org/gpt-oss-20b-GGUF"),
    description="You write movie scripts.",
    output_schema=MovieScript,
)

# Run the agent synchronously
structured_output_response: RunOutput = structured_output_agent.run("New York")
pprint(structured_output_response.content)

Usage

Set up your virtual environment

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

Start the llama.cpp server

Follow the installation steps, then start the server:

In a dedicated terminal, start an installed llama.cpp server:

llama-server -hf ggml-org/gpt-oss-20b-GGUF --ctx-size 0 --jinja -ub 2048 -b 2048

For a source build, run ./build/bin/llama-server from the llama.cpp directory with the same arguments. The command downloads and loads the GGUF model and serves http://127.0.0.1:8080/v1. Keep it running. Use a second terminal in your example directory, activate your Python environment there, and run the client steps below.

Install dependencies

uv pip install -U openai pydantic rich agno

Run Agent

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

python structured_output.py