Skip to main content
Structured outputs let you request responses that follow a specific, pre-defined JSON shape. When you supply a schema, the model is far less likely to invent keys or return malformed JSON than when you ask for a format in the system prompt. Deepshi uses the OpenAI response_format field, so the schema format matches the OpenAI structured outputs guide.
Not every model supports schemas. Check the models catalog (GET /v1/models) for models that report response_format support.

JSON mode

For simple “just give me valid JSON” cases, set response_format to json_object:
resp = client.chat.completions.create(
    model="deepshi-3.0",
    messages=[
        {"role": "system", "content": "Reply with JSON only."},
        {"role": "user", "content": "Give me a user with name and age."},
    ],
    response_format={"type": "json_object"},
)

JSON schema

To enforce an exact shape, pass a json_schema. This example asks the model to show its steps solving an equation:
curl https://api.deepshi.ai/v1/chat/completions \
  -H "Authorization: Bearer $DEEPSHI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepshi-3.0",
    "messages": [
      { "role": "system", "content": "You are a helpful math tutor." },
      { "role": "user", "content": "solve 8x + 31 = 2" }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "math_response",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "steps": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "explanation": { "type": "string" },
                  "output": { "type": "string" }
                },
                "required": ["explanation", "output"],
                "additionalProperties": false
              }
            },
            "final_answer": { "type": "string" }
          },
          "required": ["steps", "final_answer"],
          "additionalProperties": false
        }
      }
    }
  }'
The model returns content matching the schema exactly:
{
  "steps": [
    { "explanation": "Subtract 31 from both sides.", "output": "8x = -29" },
    { "explanation": "Divide both sides by 8.", "output": "x = -29 / 8" }
  ],
  "final_answer": "x = -29 / 8"
}
Beyond math, use this for data extraction, classification, UI generation, and any case where you parse the model’s output downstream.

Gotchas

  • Every property must be listed in required. To make a field optional, allow null in its type: "type": ["string", "null"].
  • additionalProperties must be set to false for strict schemas.
  • strict must be true for the schema to be enforced.
  • A schema constrains the shape, not the correctness. The values still depend on the prompt and the model.
  • Very large schemas can hit max_tokens or time out before completing. Keep schemas focused.