Llama OpenAI Structured Output
Return a MovieScript Pydantic model from Llama 4 Maverick through the OpenAI-compatible client.
At the linked source revision, LlamaOpenAI has a message-formatter signature mismatch and fails before sending a request. Apply the compatible adapter instructions below before running this example.
This current compatible configuration sends a JSON Schema request. Agno then parses the returned content against the Pydantic schema; check its type before reading model fields.
"""
Meta Structured Output
======================
Cookbook example for `meta/llama_openai/structured_output.py`.
"""
from typing import List
from agno.agent import Agent, RunOutput # noqa
from agno.models.meta import LlamaOpenAI
from pydantic import BaseModel, Field
from rich.pretty import pprint # noqa
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
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 that uses a JSON schema output
json_schema_output_agent = Agent(
model=LlamaOpenAI(id="Llama-4-Maverick-17B-128E-Instruct-FP8", temperature=0.1),
description="You are a helpful assistant. Summarize the movie script based on the location in a JSON object.",
output_schema=MovieScript,
)
json_schema_output_agent.print_response("New York")
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passoutput_schema describes the expected type. If parsing or validation fails, result.content can remain a string. Before accessing schema fields in a run result, use isinstance(result.content, YourSchema), replacing YourSchema with the class you passed as output_schema.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno llama-api-client openaiExport your Meta Llama API key
export LLAMA_API_KEY="your_llama_api_key_here"Use the compatible API adapter
Replace the LlamaOpenAI import (or Llama in the byte-image source) with this helper. Then replace every LlamaOpenAI(...) or Llama(...) construction in the saved file with llama_model(...). Keep the existing id, temperature, and any retry options inside those calls.
from os import getenv
from agno.models.openai.like import OpenAILike
def llama_model(**kwargs):
return OpenAILike(
api_key=getenv("LLAMA_API_KEY"),
base_url="https://api.llama.com/compat/v1/",
supports_native_structured_outputs=False,
supports_json_schema_outputs=True,
**kwargs,
)This uses Meta's OpenAI-compatible endpoint. You need a Meta API account with access to the selected model; check your account's current model catalog before running.
Run the example
Save the code above as structured_output.py, then run:
python structured_output.pyFull source: cookbook/90_models/meta/llama_openai/structured_output.py