Siliconflow Structured Output

Request JSON through SiliconFlow and validate a MovieScript with Pydantic.

After the argument correction in the run steps, this example requests JSON Object mode and validates the response locally. JSON Object mode does not enforce the Pydantic schema on the server.

structured_output.py
"""
Siliconflow Structured Output
=============================

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

from typing import List

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


json_mode_agent = Agent(
    model=Siliconflow(id="openai/gpt-oss-120b"),
    description="You help people write movie scripts.",
    response_model=MovieScript,
    use_json_mode=True,
)

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

# json_mode_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 openai

Export your SiliconFlow API key

export SILICONFLOW_API_KEY="your_siliconflow_api_key_here"

Select the SiliconFlow region and model

Use an API key from your global SiliconFlow account, matching the international endpoint. Add base_url="https://api.siliconflow.com/v1" to each Siliconflow(...) constructor in the saved file; the adapter default otherwise selects the .cn endpoint.

Replace the source model ID with a model enabled for your account and the example's required capabilities. For tool use, choose a currently supported function-calling model; for JSON output, check JSON mode support.

Use the current output schema argument

Replace response_model=MovieScript with output_schema=MovieScript in the saved file. response_model is no longer accepted by Agent; the preserved source raises TypeError before making a request. Keep use_json_mode=True.

Run the example

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

python structured_output.py

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