Structured Outputs

Structured Output Schema

You can set the response_format parameter to your defined schema to ensure the model produces a JSON object that matches your specified structure.

import openai
import json

# Define the OpenAI client
client = openai.OpenAI(
    base_url="https://api.scx.ai/v1", 
    api_key="your-scx-api-key"
)

MODEL = "DeepSeek-V3.1"

# Define the schema
response_format = {
    "type": "json_schema",
    "json_schema": {
        "name": "data_extraction",
        "schema": {
            "type": "object",
            "properties": {
                "section": {
                    "type": "string"
                },
                "products": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                }
            },
            "required": ["section", "products"],
            "additionalProperties": False
        },
        "strict": False 
    }
}

# Call the API
completion = client.chat.completions.create(
    model=MODEL,
    messages=[
        {
            "role": "system",
            "content": "You are an expert at structured data extraction. You will be given unstructured text and should convert it into the given structure."
        },
        {
            "role": "user",
            "content": "the section 24 has appliances, and videogames"
        }
    ],
    response_format=response_format
)

# Print the parsed result
print(completion)

JSON mode

You can set the response_format parameter to json_object in your request to ensure that the model outputs a valid JSON. In case the mode is not able to generate a valid JSON, an error will be returned.

import openai

# Define the OpenAI client
client = openai.OpenAI(
    base_url="https://api.scx.ai/v1", 
    api_key="your-scx-api-key"
)

MODEL = 'Meta-Llama-3.3-70B-Instruct'


def run_conversation(user_prompt):
    # Initial conversation with user input
    messages = [
        {
            "role": "system",
            "content": "Always provide the response in this JSON format: {\"country\": \"name\", \"capital\": \"xx\"}"
        },

        {
            "role": "user",
            "content": user_prompt,
        }
    ]

    # First API call to get model's response
    response = client.chat.completions.create(
        model=MODEL,
        messages=messages,
        max_tokens=500,
        response_format = { "type": "json_object"},
        # stream = True
    )
    
    response_message = response.choices[0].message
    print(response_message)


run_conversation('what is the capital of Austria')
javascript
ChatCompletionMessage(content='{"country": "Austria", "capital": "Vienna"}', role='assistant', function_call=None, tool_calls=None)

Other methods of structured outputs

Beyond JSON mode, structured outputs can be generated using the Instructor library. Learn more on the Instructor integration page.