Use response_format to get machine-readable output.
JSON Schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"context_tokens": {"type": "integer"},
},
"required": ["name", "context_tokens"],
"additionalProperties": False,
}
completion = client.chat.completions.create(
model="Qwen/Qwen3.8-27B",
messages=[{"role": "user", "content": "Describe DeepSeek V4 Flash."}],
response_format={"type": "json_schema", "json_schema": {"name": "model_card", "schema": schema, "strict": True}},
)
JSON mode
{"type": "json_object"} asks for valid JSON without a schema. Tell the model in your prompt what shape you expect.
Notes
- Support varies by model — look for Structured outputs in Models.
- Always parse and validate the result; if generation hits
max_tokens, the JSON can be truncated (finish_reason: "length").