IBM Structured Output

Request JSON from a watsonx agent and validate it locally as a MovieScript.

structured_output.py
"""
Ibm Structured Output
=====================

Cookbook example for `ibm/watsonx/structured_output.py`.
"""

from typing import List

from agno.agent import Agent, RunOutput  # noqa
from agno.models.ibm import WatsonX
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!"
    )


movie_agent = Agent(
    model=WatsonX(id="mistralai/mistral-small-3-1-24b-instruct-2503"),
    description="You help people write movie scripts.",
    output_schema=MovieScript,
)

# Get the response in a variable
# movie_agent: RunOutput = movie_agent.run("New York")
# pprint(movie_agent.content)

movie_agent.print_response("New York")

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

if __name__ == "__main__":
    pass

The current adapter requests JSON Object mode and Agno attempts local validation against MovieScript.

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 ibm-watsonx-ai

Configure your watsonx project and region

Use a project associated with a watsonx.ai Runtime service and an IBM Cloud API key authorized to access it. Copy the project ID and service URL from your watsonx environment. Check regional model availability before selecting a model.

The example URL below is Frankfurt, which is also the adapter default. Change it to match your project and service region.

export IBM_WATSONX_API_KEY="your_ibm_cloud_api_key"
export IBM_WATSONX_PROJECT_ID="your_project_id"
export IBM_WATSONX_URL="https://eu-de.ml.cloud.ibm.com"

Run the example

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

python structured_output.py

Full source: cookbook/90_models/ibm/watsonx/structured_output.py