When performing information extraction or structured data generation tasks, a model may return extra text (such as ```json ) that breaks downstream parsing. Enabling structured output ensures the model returns a valid JSON string. The JSON Schema mode also gives you precise control over the output structure and types, eliminating extra validation or retries.
Usage
Structured output supports two modes: JSON Object and JSON Schema.
-
JSON Object mode: Ensures the output is a valid JSON string, but does not guarantee a specific structure. Usage:
- Set the
response_formatparameter: In the request body, setresponse_formatto{"type": "json_object"}. - Include the JSON keyword in your prompt: The system message or user message must contain the word "JSON" (case-insensitive), otherwise the API returns:
'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.
- Set the
-
JSON Schema mode: Ensures the output conforms to a specified structure. Usage: set
response_formatto{"type": "json_schema", "json_schema": {..., "strict": true}}.No JSON keyword required in the prompt.
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.
Feature comparison:
Feature | JSON Object mode | JSON Schema mode |
|---|---|---|
Outputs valid JSON | Yes | Yes |
Strictly follows schema | No | Yes |
Supported models | Most Qwen models | Only selected qwen-plus models |
|
|
|
Prompt requirement | Must include "JSON" | Recommended to describe explicitly |
Use case | Flexible JSON output | Precise schema validation |
Supported models
- JSON Object
- JSON Schema
- Qwen
- Kimi
- GLM
- DeepSeek
-
Text generation models
- Qwen-Max: 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-Turbo (non-thinking mode): Qwen-Turbo series
- Qwen-Coder: Qwen3-Coder series
- Qwen-Long: Qwen-Long series
- Qwen3.8 open-source series
- Qwen3.6 open-source series (non-thinking mode)
- Qwen3.5 open-source series (non-thinking mode)
- Qwen3 open-source series (non-thinking mode)
- Qwen3-Coder open-source series
- Qwen2.5 open-source series (excluding math and coder models)
-
Multimodal models
- Qwen-VL (non-thinking mode): Qwen3-VL-Plus series, Qwen3-VL-Flash series, Qwen-VL-Max series (excluding the latest and snapshot versions), Qwen-VL-Plus series (excluding the latest and snapshot versions)
- Qwen-Omni: Qwen3.8-Omni-Flash, Qwen3.5-Omni-Plus series
- Qwen3-VL open-source series (non-thinking mode)
Getting started
This example extracts structured information from a personal profile.
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.Obtain an API key and export the API key as an environment variable. If you use the OpenAI SDK or DashScope SDK to make calls, install the SDK.
- OpenAI-compatible Chat Completions API
- OpenAI-Compatible Responses API
- DashScope API
- Python
- Node.js
- curl
Response
Image and video data processing
Multimodal models also support structured output for images and videos. Use JSON mode to extract structured data from visual content, such as field values from receipts, object locations in images, or events in video.
For image and video file limits, see Image and video understanding .
- OpenAI compatible
- DashScope
- Python
- Node.js
- curl
Response
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.
- OpenAI compatible
- DashScope
- Python
- Node.js
Response
Getting structured output
Setting response_format type to json_object returns a valid JSON string, but the structure may not match your expectations - suitable for simple scenarios. For automated parsing, API interoperability, and other complex scenarios requiring strict type constraints, set type to json_schema to force the model to output content that strictly conforms to a specified format. The response_format format and example:
json_schema and will automatically fall back to json_object; the schema constraints will not take effect.name and age) and an optional email field.
Singapore region models are not supported yet.
How to use
With the OpenAI SDK parse method, you can pass a Python Pydantic class or Node.js Zod object directly. The SDK automatically converts it to a JSON Schema - no need to write complex JSON manually. For the DashScope SDK, construct the JSON Schema manually following the format above.
- OpenAI-compatible Chat Completions API
- OpenAI-Compatible Responses API
- DashScope API
Python
Node.js
Configuration guide
Follow these guidelines when using JSON Schema for more reliable structured output:
-
Required field declaration
It is recommended to list required fields in the
requiredarray. Optional fields can be omitted, for example:
-
Implementing optional fields
Besides omitting from
required, you can also allow thenulltype:
email field, but its value may be null.
- additionalProperties configuration Controls whether to allow extra fields not defined in the schema:
"I'm Zhang San, 25 years old"; output: {"name": "Zhang San", "age": 25} (includes the undefined age field).
Value | Behavior | Use case |
|---|---|---|
| Only output defined fields | Precise structure control |
| Allow extra fields | Capture more information |
-
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 keywordsKeyword
Description
typeSupports
string,number,integer,boolean,null,array,object; union types such as["string", "null"]are also supportedenum/constenumrestricts the value to a set of allowed values;constrestricts it to a single fixed value$defs+$refLocal references are supported (e.g.,
#/$defs/address); external URL references do not cause errors but have no effectanyOf/oneOf/allOf/notComposition and conditional constraints are supported. Note that
oneOfdoes not strictly guarantee exactly-one semanticsif/then/elseConditional schemas are supported, enabling dynamic validation rules based on field values
title/description/default/examplesAccepted as semantic hints for the model; not enforced as hard constraints
$schema/$id/$commentMeta identifiers; accepted without affecting validation behavior
deprecated/readOnly/writeOnlyAnnotation keywords; accepted without affecting validation behavior
Type
Supported keywords
Unsupported keywords (400 error)
stringminLength,maxLength,pattern,format(e.g.,email,date),contentMediaType,contentEncoding-
number/integerminimum,maximum,exclusiveMinimum,exclusiveMaximummultipleOfarrayitems,prefixItems(tuple mode),minItems,maxItems,unevaluatedItemsuniqueItems,contains,minContains,maxContains,additionalItemsobjectproperties,required,additionalProperties,minProperties,maxProperties,dependentRequired,dependentSchemas,unevaluatedPropertiespatternProperties,propertyNames
Going live
- Validate before passing downstream When using JSON Object mode, validate the output before passing it to downstream services. Use a library such as jsonschema (Python), Ajv (JavaScript), or Everit (Java) to ensure it conforms to the expected JSON Schema, preventing downstream parsing failures, data loss, or business logic disruptions due to missing fields, type errors, or malformed formats. On failure, retry the request or use a model to rewrite the output.
-
Do not set max_tokens
Do not set
max_tokenswhen structured output is enabled. This parameter caps the number of output tokens and defaults to the model's maximum. Setting it may truncate the JSON string mid-output, producing invalid JSON that fails to parse. -
Use SDK to generate schemas
Use the SDK to auto-generate schemas. This avoids errors from manual maintenance and provides automatic validation and parsing.
Python