Make the model return valid JSON, and use JSON Schema to constrain the output structure precisely
When performing information extraction or structured data generation, a model may return extra text (such as a
JSON Object mode ensures the output is a valid JSON string, but does not guarantee a specific structure. To use it:
Qwen4.0-Max (Token Plan only), Qwen3.8-Max series, Qwen3.8-Flash series, Qwen3.7-Max series, Qwen3.7-Plus series, and Qwen3.7-Flash series, as well as DeepSeek:
This example extracts structured information from a personal profile using JSON Object mode.
SDK response:
Multimodal models support structured output for images and video. Use JSON mode to pull structured data out of visual content, such as field values from receipts, object locations in images, or events in video.
The following example extracts ticket and invoice fields from a scanned receipt.
Response:
Ambiguous prompts like "return user information" lead to unpredictable output structures. For reliable results, describe the expected schema in your prompt: specify field names, types, required vs. optional status, format constraints (such as date format), and include examples.
The system prompt below does all of this. It constrains field types, distinguishes required from optional fields, and uses four examples to show that the
Pass it as the system message:
Response:
JSON Object mode only guarantees that the output is valid JSON — field names, types, and nesting may still differ from what you expect. For automated parsing, API interoperability, and other cases that need strict type constraints, set
This example forces the model to output a JSON object with the required fields
The OpenAI SDK's
Response:
Validate before passing downstream
In JSON Object mode the output is guaranteed to be valid JSON, but not to match your business schema. Validate it with a library such as jsonschema (Python), Ajv (JavaScript), or Everit (Java) before handing it to downstream services, so missing fields or type errors don't cause parsing failures, data loss, or broken business logic. On failure, retry the request or have a model rewrite the output.
Do not set max_tokens
Leave
If a call fails and returns an error message, see Error messages for resolution.
```json wrapper) that breaks downstream parsing. Enabling structured output ensures the model returns a valid JSON string. JSON Schema mode goes further and gives you precise control over the output structure and types, eliminating extra validation or retries.
Two modes
| Feature | JSON Object mode | JSON Schema mode |
|---|---|---|
| Outputs valid JSON | Yes | Yes |
| Strictly follows schema | No | Yes |
| Supported models | Most Qwen models, Kimi, GLM, DeepSeek | Selected models only |
response_format setting | {"type": "json_object"} | {"type": "json_schema", "json_schema": {..., "strict": true}} |
| Prompt requirement | Must include "JSON" | Recommended to describe explicitly |
| Use case | Flexible JSON output | Precise schema validation |
- Set
response_formatin the request body to{"type": "json_object"}. - Include the word "JSON" (case-insensitive) in the system message or user message. Otherwise the API returns:
'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.
response_format to {"type": "json_schema", "json_schema": {..., "strict": true}}.
The parameters above apply to the Chat Completions API. If you use the Responses API, set text.format instead; the value format is the same. See the examples below. In JSON Object mode, the Responses API also requires the prompt to contain the word "JSON"; otherwise, a 400 error is returned.
JSON Schema mode does not require the "JSON" keyword in the prompt.
Supported models
JSON Object
View the full model list
View the full model list
Qwen
Text generation models- Qwen-Max: Qwen4.0-Max (Token Plan only), Qwen3.8-Max series, Qwen3.7-Max series
- Qwen-Max (non-thinking mode): Qwen3.6-Max series, Qwen3-Max series, Qwen-Max series
- Qwen-Plus: Qwen3.7-Plus series
- Qwen-Plus (non-thinking mode): Qwen3.6-Plus series, Qwen3.5-Plus series, Qwen-Plus series
- Qwen-Flash: Qwen3.8-Flash series, Qwen3.7-Flash series
- Qwen-Flash (non-thinking mode): Qwen3.6-Flash series, Qwen3.5-Flash series, Qwen-Flash series
- Qwen-Coder: Qwen3-Coder series
- Qwen-Long: Qwen-Long series
- Qwen3.8 open-source series
- Open-source series (non-thinking mode): Qwen3.6 open-source series, Qwen3.5 open-source series, Qwen3 open-source series
- Open-source series: Qwen3.8 open-source series (
qwen3.8-2.4t-a95b), Qwen3-Coder open-source series, Qwen2.5 open-source series (excluding math and coder models)
- Qwen-VL: Qwen3-VL-Plus series
- Qwen-Omni: Qwen3.8-Omni-Flash, Qwen3.5-Omni-Plus series
- Open-source series: Qwen3-VL open-source series
Kimi
kimi-k3.1-prokimi-k3
GLM
glm-5.1- Non-thinking mode:
glm-5(China North 2 (Beijing) only)
DeepSeek
deepseek-v4.1-flash,deepseek-v4.1-flash-1017,deepseek-v4-pro-0813,deepseek-v4-pro,deepseek-v4-flash-0731
Models labeled "non-thinking mode" support structured output only in non-thinking mode.
JSON Schema
Qwen4.0-Max (Token Plan only), Qwen3.8-Max series, Qwen3.8-Flash series, Qwen3.7-Max series, Qwen3.7-Plus series, and Qwen3.7-Flash series, as well as DeepSeek: deepseek-v4.1-flash, deepseek-v4.1-flash-1017, and deepseek-v4-pro. More models are coming soon.
Getting started
This example extracts structured information from a personal profile using JSON Object mode.
JSON Object mode does not guarantee stable key names or field types. Results may vary across different prompts or calls. To enforce a fixed structure, use JSON Schema mode.
Before calling, obtain an API key and export it as an environment variable. To call through the OpenAI SDK or DashScope SDK, install the SDK first.
- OpenAI compatible
- OpenAI compatible - Responses
- DashScope
Full curl response
Full curl response
Extract structured data from images and video
Multimodal models support structured output for images and video. Use JSON mode to pull structured data out of visual content, such as field values from receipts, object locations in images, or events in video.
The following example extracts ticket and invoice fields from a scanned receipt.
For image and video file limits, see Image and video understanding.
- OpenAI compatible
- DashScope
Optimize prompts
Ambiguous prompts like "return user information" lead to unpredictable output structures. For reliable results, describe the expected schema in your prompt: specify field names, types, required vs. optional status, format constraints (such as date format), and include examples.
The system prompt below does all of this. It constrains field types, distinguishes required from optional fields, and uses four examples to show that the hobby field is omitted entirely when hobbies are not mentioned.
System prompt
- OpenAI compatible
- DashScope
Constrain output precisely with JSON Schema
JSON Object mode only guarantees that the output is valid JSON — field names, types, and nesting may still differ from what you expect. For automated parsing, API interoperability, and other cases that need strict type constraints, set type to json_schema. The model then follows the schema exactly.
The response_format structure looks like this:
Multimodal inputs (image, video, audio, etc.) do not support
json_schema and will automatically fall back to json_object; the schema constraints will not take effect.name and age, plus the optional field email.
How to use
The OpenAI SDK's parse method accepts a Python Pydantic class or a Node.js Zod object directly and converts it to a JSON Schema for you, so you never write the schema by hand. With the DashScope SDK, construct the JSON Schema manually using the format above.
- OpenAI compatible
- OpenAI compatible - Responses
- DashScope
Configuration guide
Declaring required fields
Declaring required fields
List required fields in the If the input does not provide email information, the output omits the field.
required array and leave optional fields out:Implementing optional fields
Implementing optional fields
Besides leaving a field out of The output then always includes the
required, you can allow the null type:email field, but its value may be null.additionalProperties configuration
additionalProperties configuration
Controls whether the model may output fields not defined in the schema:For the input "I'm Zhang San, 25 years old", the output is
{"name": "Zhang San", "age": 25} — including the undefined age field.| Value | Behavior | Use case |
|---|---|---|
false | Only output defined fields | Precise structure control |
true | Allow extra fields | Capture more information |
Supported data types
Supported data types
string, number, integer, boolean, object, array, enum.Supported JSON Schema keywords
Supported JSON Schema keywords
The following lists the keywords supported by JSON Schema mode (strict mode). Keywords marked as "unsupported" return a 400 error when submitted. For the complete keyword definitions, see the JSON Schema specification.Schema-level common keywords
Type-specific keywords
| Keyword | Description |
|---|---|
type | Supports string, number, integer, boolean, null, array, object; union types such as ["string", "null"] are also supported |
enum / const | enum restricts the value to a set of allowed values; const restricts it to a single fixed value |
$defs + $ref | Local references are supported (e.g., #/$defs/address); external URL references do not cause errors but have no effect |
anyOf / oneOf / allOf / not | Composition and conditional constraints are supported. Note that oneOf does not strictly guarantee exactly-one semantics |
if / then / else | Conditional schemas are supported, enabling dynamic validation rules based on field values |
title / description / default / examples | Accepted as semantic hints for the model; not enforced as hard constraints |
$schema / $id / $comment | Meta identifiers; accepted without affecting validation behavior |
deprecated / readOnly / writeOnly | Annotation keywords; accepted without affecting validation behavior |
| Type | Supported keywords | Unsupported keywords (400 error) |
|---|---|---|
string | minLength, maxLength, pattern, format (e.g., email, date), contentMediaType, contentEncoding | - |
number / integer | minimum, maximum, exclusiveMinimum, exclusiveMaximum | multipleOf |
array | items, prefixItems (tuple mode), minItems, maxItems, unevaluatedItems | uniqueItems, contains, minContains, maxContains, additionalItems |
object | properties, required, additionalProperties, minProperties, maxProperties, dependentRequired, dependentSchemas, unevaluatedProperties | patternProperties, propertyNames |
Going live
Validate before passing downstream
In JSON Object mode the output is guaranteed to be valid JSON, but not to match your business schema. Validate it with a library such as jsonschema (Python), Ajv (JavaScript), or Everit (Java) before handing it to downstream services, so missing fields or type errors don't cause parsing failures, data loss, or broken business logic. On failure, retry the request or have a model rewrite the output.
Do not set max_tokens
Leave max_tokens unset when structured output is enabled. This parameter caps the number of output tokens (the default is the model's maximum) and can truncate the JSON string mid-output, producing invalid JSON.
Use the SDK to generate schemas
Let the SDK generate the schema. This avoids errors from hand-maintained schemas and gives you automatic validation plus a type-safe parsed result.