LiteLLM Structured Output

Request JSON mode and JSON Schema through LiteLLM, then validate responses with MovieScript.

The first source model does not enable schema support, so its JSON-mode request only supplies instructions and omits response_format. Apply the model edits below to send JSON Object mode for the first agent and JSON Schema for the second. Both require a provider/model that supports the requested format.

structured_output.py
"""
Litellm Structured Output
=========================

Cookbook example for `litellm/structured_output.py`.
"""

from typing import List

from agno.agent import Agent, RunOutput  # noqa
from agno.models.litellm import LiteLLM
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 JSON mode
json_mode_agent = Agent(
    model=LiteLLM(id="gpt-5.6-luna"),
    description="You write movie scripts.",
    output_schema=MovieScript,
    use_json_mode=True,
)

# Agent that uses native structured outputs.
# Set supports_native_structured_outputs=True for the providers that support it.
structured_output_agent = Agent(
    model=LiteLLM(id="gpt-5.6-luna", supports_native_structured_outputs=True),
    description="You write movie scripts.",
    output_schema=MovieScript,
    structured_outputs=True,
)

json_mode_agent.print_response("New York")
structured_output_agent.print_response("New York")

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass

output_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/activate

Install dependencies

uv pip install -U agno litellm

Set your OpenAI credentials

Use an OpenAI API key with access to the requested model. The LiteLLM SDK calls the provider directly. An existing LITELLM_API_KEY overrides provider-specific credentials, so clear it for this example.

unset LITELLM_API_KEY
export OPENAI_API_KEY="your_provider_api_key_here"

Set compatible sampling options

Add temperature=None, top_p=None to every LiteLLM(...) using id="gpt-5.6-luna" or id="openai/gpt-5.6-luna" in your saved file. The adapter defaults to temperature=0.7 and top_p=1.0; the LiteLLM SDK rejects those sampling settings for this model's default reasoning mode before sending a request.

Enable the JSON-mode request format

Add supports_native_structured_outputs=True to the first LiteLLM model too. Keep the first agent's use_json_mode=True and the second model's existing support flag, along with the sampling edits above.

Run the example

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

python structured_output.py

Full source: cookbook/90_models/litellm/structured_output.py