Skip to main content
The Kimi API constrains the output format of chat completions via the 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, common issues, and error handling. For the basics of JSON Mode, see JSON Mode.

response_format basic structure

  • When type is json_object, the json_schema field is not required.
  • When type is json_schema, both json_schema.name and json_schema.schema are 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.loads into strongly-typed objects without extra fault-tolerance handling.
Current models: kimi-k3.1-pro and kimi-k3 reliably support Structured Output, including nested objects, arrays, anyOf / oneOf / $ref / additionalProperties: true.

Quick start

Basic usage

Set type to "json_schema" in response_format, and pass the json_schema object:

Example output

About reasoning_content

Thinking models 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

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. If 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 the walle CLI tool to quickly self-check schema compatibility:
Even if the schema contains anyOf / oneOf / $ref, the API often returns 200, and the response does not contain a warning field. Therefore, walle is 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 nested objects and arrays:

Comparison with JSON Mode

The structural guarantee of constrained decoding holds when the schema complies with the MFJS specification. For structured data consumed downstream, always use json_schema with strict: true to avoid writing extensive defensive code in the business layer.

Notes

  1. Schema must comply with MFJS: When strict=true, use the walle CLI tool to pre-validate the schema.
  2. 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.
  3. additionalProperties:
    • When set to false, the model will not output fields not defined in the schema.
    • When set to true or omitted, fields not defined in the schema are allowed.
  4. Use nullable union types for missing information: Fields declared in required always 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 allows null (e.g. "type": ["integer", "null"]), so the model can use null to explicitly mean “information missing” instead of the string "unknown" or a disappearing field — downstream code can then json.loads and cast to typed objects without defensive handling.
  5. 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 checking finish_reason and appropriately increasing max_tokens.
  6. Compatibility with Partial Mode:
    • kimi-k3.1-pro and kimi-k3 can be used with partial=true on simple schemas. Avoid this combination with complex schemas.
  7. 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:
Please check that the schema is a valid JSON Schema object.

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

The following issues may occur when model output does not conform to the schema:
  • The returned content contains a Markdown code block (e.g., json ... ), causing json.loads to fail.
  • Complex schemas such as oneOf / $ref are not strictly followed.
Recommendations:
  • Prefer kimi-k3.1-pro or kimi-k3.
  • If the output contains Markdown markers, strip them in the business layer first, then validate the parsed result against the schema fields.