Dashscope Structured Output

Request JSON from Qwen Plus on DashScope and validate it locally as a MovieScript.

structured_output.py
"""
Dashscope Structured Output
===========================

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

from typing import List

from agno.agent import Agent
from agno.models.dashscope import DashScope
from pydantic import BaseModel, Field

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


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=DashScope(id="qwen-plus"),
    description="You write movie scripts and return them as structured JSON data.",
    output_schema=MovieScript,
)

structured_output_agent.print_response(
    "Create a movie script about llamas ruling the world. "
    "Return a JSON object with: name (movie title), setting, ending, genre, "
    "characters (list of character names), and storyline (3 sentences)."
)

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

if __name__ == "__main__":
    pass

Apply the JSON-mode change below before running. It requests a JSON object with Qwen Plus in non-thinking mode and lets Agno validate the result locally. The unmodified source requests JSON Schema mode, whose support depends on the selected model and region. See Alibaba's supported output modes.

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 openai

Configure regional DashScope access

Activate Alibaba Cloud Model Studio and create an API key for your region and workspace. Agno's default endpoint is Singapore: https://dashscope-intl.aliyuncs.com/compatible-mode/v1.

export DASHSCOPE_API_KEY="your_singapore_api_key"

For another region or a workspace-specific domain, pass the matching base_url to DashScope. Use the OpenAI-compatible endpoint guide to match the URL, key and available model. The documented Singapore default remains functional.

Use JSON mode with the default endpoint

Save the source as structured_output.py and add use_json_mode=True to structured_output_agent = Agent(...), alongside output_schema=MovieScript. Keep the JSON prompt and the default non-thinking mode.

Run the example

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

python structured_output.py

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