1. Use Cases

Currently, the SiliconFlow large model API platform, SiliconCloud, by default generates unstructured text. However, in certain application scenarios, you may want the model to output content in a structured format, but simply instructing the model via prompts might not yield the desired structured output.

As a standardized and lightweight data exchange format, JSON mode is an important feature for enabling structured outputs from large model APIs. When you make a request to the large model API, the results returned by the model are presented in JSON format, which is easy for humans to read and write, and also easy for machines to parse and generate.

Now, on the SiliconCloud platform, apart from the DeepSeek R1 series and V3 models, most other major language models support JSON mode. This allows the model to output strings in JSON format, ensuring that the model produces outputs in the expected structure, facilitating subsequent logical parsing of the output content.

For example, you can now use the SiliconCloud API to attempt structured outputs for the following cases:

  • Build a news database from company-related reports, including news titles, links, etc.
  • Extract sentiment analysis structures from product purchase reviews, including sentiment polarity (positive, negative, neutral), sentiment intensity, sentiment keywords, etc.
  • Extract product lists from purchase histories, including product information, reasons for recommendation, prices, promotional details, etc.

2. How to Use

Add the following to your request:

response_format={"type": "json_object"}

3. Supported Models

Currently, online models, except for the DeepSeek R1 series and V3 models, support the above parameter.

Note: The list of supported models may change. Please refer to this document for the latest list of supported models.
Your application must detect and handle edge cases that may cause the model to output incomplete JSON objects.
Please set max_tokens appropriately to prevent JSON strings from being truncated.

4. Example Usage

Below is an example using OpenAI:

import json  
from openai import OpenAI

client = OpenAI(
    api_key="Your APIKEY", # Obtain from https://cloud.siliconflow.com/account/ak
    base_url="https://api.ap.siliconflow.com/v1"
)

response = client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V2.5",
        messages=[
            {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
            {"role": "user", "content": "Who were the men's and women's singles champions in table tennis at the 2020 World Olympics? "
             "Please respond in the format {\"Men's Champion\": ..., \"Women's Champion\": ...}"}
        ],
        response_format={"type": "json_object"}
    )

print(response.choices[0].message.content)

The model will output:

{"Men's Champion": "Ma Long", "Women's Champion": "Chen Meng"}