Structured output
Request valid JSON or output that conforms to a specified schema.
Use structured output when your program needs to consume model-generated JSON directly, rather than relying only on a “return JSON” prompt. See supported capabilities in the catalog.
JSON mode
response_format: {"type": "json_object"} guarantees valid JSON without constraining its structure. Specify the expected fields in your prompt:
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[
{"role": "system", "content": "Extract the city and date from the user input. Return JSON with city and date fields."},
{"role": "user", "content": "Find tomorrow’s weather in Shanghai"},
],
response_format={"type": "json_object"},
)JSON Schema mode
response_format: {"type": "json_schema", ...} also constrains the output structure:
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-pro",
messages=[{"role": "user", "content": "Find tomorrow’s weather in Shanghai"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "extract_query",
"schema": {
"type": "object",
"properties": {
"city": {"type": "string"},
"date": {"type": "string"},
},
"required": ["city", "date"],
},
},
},
)Alternative with the Anthropic protocol
For Messages, force a tool call: define the required structure as a tool’s input_schema, set tool_choice: {"type": "tool", "name": "..."}, and read the structured data from input in the response’s tool_use block.
Recommendations
- Prefer flat schemas and add field
descriptionvalues for more reliable results. - With reasoning models, reasoning still appears in
reasoning_content/thinking; the answer body contains the JSON. - Some models have limited support for complex schemas. Test your actual schema during integration.