Switch language

Structured Outputs

Force model output to conform to a JSON Schema for reliable structured data.

Structured Outputs ensure the model generates responses that match your supplied JSON Schema. This is essential for applications that need to parse model output programmatically.

Two approaches

Moderesponse_format.typeDescription
JSON Schema (recommended)json_schemaModel strictly follows your schema definition
JSON Object (legacy)json_objectModel outputs valid JSON but without schema enforcement

Example: Extract structured data

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api-inference.bitdeer.ai/v1",
    api_key="YOUR_API_KEY",
)

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[
        {
            "role": "system",
            "content": "You are a data extraction assistant. Extract expense information from the user's input.",
        },
        {
            "role": "user",
            "content": "I spent $120 on dinner last Friday and bought office supplies for $45 on Monday.",
        },
    ],
    max_tokens=1024,
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "expense_report",
            "schema": {
                "type": "object",
                "properties": {
                    "expenses": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "description": {"type": "string"},
                                "amount": {"type": "number"},
                                "date": {"type": "string"},
                                "category": {"type": "string"},
                            },
                            "required": ["description", "amount"],
                        },
                    },
                    "total": {"type": "number"},
                },
                "required": ["expenses", "total"],
            },
            "strict": True,
        },
    },
)

result = json.loads(response.choices[0].message.content)
print(json.dumps(result, indent=2))

Expected output

{
  "expenses": [
    {"description": "Dinner", "amount": 120, "date": "last Friday", "category": "Food"},
    {"description": "Office supplies", "amount": 45, "date": "Monday", "category": "Office"}
  ],
  "total": 165
}

Schema requirements

When strict: true:

  • Supported types: string, number, integer, boolean, array, object, enum, anyOf
  • All object properties must be explicitly listed
  • Unsupported schemas will return an error

JSON Object mode (simpler alternative)

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": "List 3 colors as JSON."}],
    max_tokens=256,
    response_format={"type": "json_object"},
)

This guarantees valid JSON output but does not enforce a specific structure.

Last updated on

On this page

Structured Outputs · Bitdeer AI