response_format parameter. It supports two modes:
This document focuses on the
json_schema mode of response_format (i.e., Structured Output), including parameter usage, model differences, common issues, and error handling. For the basics of JSON Mode, see JSON Mode.
response_format basic structure
- When
typeisjson_object, thejson_schemafield is not required. - When
typeisjson_schema, bothjson_schema.nameandjson_schema.schemaare required.
Advantages of Structured Output
Compared to JSON Mode, Structured Output offers the following advantages:- Strictly controlled structure: The model output must fully follow the JSON Schema you define, with field names, types, and nesting levels matching one-to-one.
- No need to repeatedly describe the format in the prompt: Decouple format requirements from the schema, reducing prompt-engineering complexity.
- More reliable downstream integration: Output can be directly parsed by
json.loadsinto strongly-typed objects without extra fault-tolerance handling.
Model-difference note: Different models have different levels of JSON Schema support.
kimi-k3reliably supports Structured Output, including nested objects, arrays, andanyOf.kimi-k2.7-codehas the most stable Structured Output support, including nested objects, arrays,anyOf/oneOf/$ref/additionalProperties: true, etc.kimi-k2.6occasionally behaves unstably with complex schemas; for example,$refmay return a Markdown code block,oneOfmay be ignored, andpartial=truemay output fields outside the schema. When usingkimi-k2.6, prefer simple schemas and add a second validation layer in your business logic.
Quick start
Basic usage
Settype to "json_schema" in response_format, and pass the json_schema object:
Example output
About reasoning_content
Thinking models such askimi-k3 and kimi-k2.7-code may return reasoning_content in addition to content. Only parse choices[0].message.content as the final JSON; do not call json.loads on the entire response object.
Parameter description
Note: Whetherstrictistrue,false, or omitted,kimi-k2.7-codegenerally adheres to the schema well;kimi-k2.6is more likely to output fields outside the schema whenstrict=falseor omitted. Always explicitly setstrict: true.
strict mode
json_schema.strict is recommended to be set to true, meaning the model output must fully match the schema definition. In this case, your schema must comply with the MFJS (Moonshot Flavored JSON Schema) specification.
MFJS model differences:If
kimi-k2.7-codealready has relatively complete support for features such asanyOf/oneOf/$ref/additionalProperties: true, and usually will not trigger MFJS errors.kimi-k2.6is more likely to hit MFJS limits with complex schemas; keep schemas simple.
strict is set to false, the API only guarantees that the output is a valid JSON object, but does not strictly enforce the internal field structure. This can be used when the schema is complex or you want to give the model more flexibility.
How to validate schema compliance with MFJS
You can use thewalle CLI tool to quickly self-check schema compatibility:
Even if the schema containsanyOf/oneOf/$ref, the API often returns200, and the response does not contain awarningfield. Therefore,walleis better suited as a static-check entry point; actual compatibility should be verified via live calls against the target model.
Nested objects and arrays example
Structured Output supports arbitrarily deep nested objects and arrays, which is stable onkimi-k2.7-code:
Comparison with JSON Mode
The structural guarantee of constrained decoding holds when the schema complies with the MFJS specification; complex schemas may still be unstable on models such as
kimi-k2.6 — see the model-difference note above. For structured data consumed downstream, always use json_schema with strict: true to avoid writing extensive defensive code in the business layer.
Notes
-
Schema must comply with MFJS: When
strict=true, use thewalleCLI tool to pre-validate the schema. Common MFJS constraints are greatly relaxed onkimi-k2.7-code, but may still trigger onkimi-k2.6. - Prompt still needs context: Although the format is constrained by the schema, the model still needs to understand the business content. Please clearly describe the task objective and data source in the system prompt or user prompt.
-
additionalProperties:- When set to
false, the model will not output fields not defined in the schema. - When set to
trueor omitted,kimi-k2.7-codeallows extra fields;kimi-k2.6may also output extra fields, but with less stability thankimi-k2.7-code.
- When set to
-
Use nullable union types for missing information: Fields declared in
requiredalways appear in the output. When the input lacks the corresponding information, a field declared with a single type (e.g."integer") may lead the model to fabricate content or return an empty string. Prefer a union type that allowsnull(e.g."type": ["integer", "null"]), so the model can usenullto explicitly mean “information missing” instead of the string"unknown"or a disappearing field — downstream code can thenjson.loadsand cast to typed objects without defensive handling. Note thatkimi-k2.6may still return empty strings (e.g."employee_id": ""), so keep a null-value check in the business layer. -
Error handling: When the schema is too complex or the prompt contradicts the schema, the model may output incomplete JSON (
finish_reason="length"). We recommend checkingfinish_reasonand appropriately increasingmax_tokens. -
Compatibility with Partial Mode:
kimi-k2.7-codeusually works withpartial=trueon simple schemas, but complex schemas may still break structural constraints.kimi-k2.6is more likely to output fields outside the schema withpartial=true, so it is not recommended to mix them on this model.
-
Prefix cache: setting
response_format(or not setting it) does not invalidate the prefix cache, so you can adjust it on a per-request basis without hurting cache hit rates.
Common errors
invalid_request_error
When the schema format itself is invalid (for example, json_schema.schema is not an object), the API returns 400 with error type invalid_request_error:
Output truncated (finish_reason="length")
The model reached the max_tokens limit before outputting the complete JSON. We recommend:
- Increasing
max_tokens(e.g., 4096 or higher) - Simplifying the nesting depth of the schema
- Shortening the input text length
Field type mismatch / Markdown code block output
On older models such askimi-k2.6, the following may occur:
- The returned
contentcontains a Markdown code block (e.g.,json ...), causingjson.loadsto fail. - Complex schemas such as
oneOf/$refare not strictly followed.
- Use
kimi-k2.7-codefor Structured Output calls. - If you must use
kimi-k2.6, strip Markdown markers in the business layer first, then validate the parsed result against the schema fields.