JSON mode
Force the model to return strict JSON.
Usage
resp = client.chat.completions.create(
model="deepseek-v3.2",
messages=[
{"role": "system", "content": "Respond only with JSON."},
{"role": "user", "content": "Three colors as RGB"}
],
response_format={"type": "json_object"},
)
import json
data = json.loads(resp.choices[0].message.content)Required for JSON mode
- The system prompt must mention "JSON" — most models enforce this
- Include schema constraints in the prompt for best results
Schema-constrained mode
Pass a JSON schema for stricter validation:
response_format={
"type": "json_schema",
"json_schema": {
"name": "color_palette",
"schema": {
"type": "object",
"properties": {
"colors": {
"type": "array",
"items": {"type": "string", "pattern": "^#[0-9A-Fa-f]{6}$"}
}
}
}
}
}Supported by:
gpt-4o,o1claude-sonnet-4-6,claude-opus-4-7kimi-k2.5,glm-4.6(best-effort)