MiniMax Structured Output
Request JSON from MiniMax M3 and validate the response locally with Pydantic.
The source comment is misleading: use_json_mode=True sends response_format={"type": "json_object"}. The current MiniMax compatibility documentation does not document that mode. The Current Example uses a prompt and explicit local validation without sending response_format.
"""
MiniMax Structured Output
=========================
Cookbook example for `minimax/structured_output.py`.
"""
from typing import List
from agno.agent import Agent, RunOutput # noqa
from agno.models.minimax import MiniMax
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!"
)
# MiniMax does not implement OpenAI-style native `response_format` /
# `json_schema`, so we drive structured output through JSON mode.
agent = Agent(
model=MiniMax(id="MiniMax-M3"),
description="You write movie scripts.",
output_schema=MovieScript,
use_json_mode=True,
)
# Get the response in a variable
# response: RunOutput = agent.run("New York")
# pprint(response.content)
agent.print_response("New York")
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passCurrent Example
The prompt requests JSON; Pydantic checks the actual response. Invalid JSON or missing fields enter the validation-error branch. M3 supports disabling thinking so reasoning text is not mixed into the requested JSON.
import json
from agno.agent import Agent
from agno.models.minimax import MiniMax
from pydantic import BaseModel, ValidationError
class MovieScript(BaseModel):
setting: str
ending: str
genre: str
name: str
characters: list[str]
storyline: str
agent = Agent(
model=MiniMax(id="MiniMax-M3", extra_body={"thinking": {"type": "disabled"}}),
instructions="Write a movie script. Return only a JSON object matching this schema: "
+ json.dumps(MovieScript.model_json_schema()),
)
result = agent.run("Set the movie in New York.")
if not isinstance(result.content, str):
raise RuntimeError("Expected a JSON text response")
try:
script = MovieScript.model_validate_json(result.content)
except ValidationError as exc:
print(f"The response did not match MovieScript: {exc}")
else:
print(script.model_dump_json(indent=2))Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiExport your MiniMax API key
export MINIMAX_API_KEY="your_minimax_api_key_here"Run the example
Save the complete Current Example above as structured_output.py, then run:
python structured_output.pyFull source: cookbook/90_models/minimax/structured_output.py