response_format parameter:
Enable JSON Mode with response_format
If you only tell the Kimi large language model in the prompt: โPlease output content in JSON format,โ the model can understand your request and generate a JSON document as required. However, the generated content often has some flaws: for instance, in addition to the JSON document, Kimi might output extra text to explain the JSON document โsummary field on the last line):
response_format parameter constrains the output format. Its default value is {"type": "text"}, which means ordinary text content with no formatting constraints. Set response_format to {"type": "json_object"} to enable JSON Mode, and the Kimi large language model will output a valid, correctly parsable JSON document as required.
Using JSON Mode takes three steps:
- Define the output JSON format in the system or user prompt, including specific field names and field types; the best practice is to provide a concrete output example and explain the meaning of each field;
- Set the
response_formatparameter to{"type": "json_object"}; - Parse the
contentin the message returned by the Kimi large language model;message.contentis a valid JSON Object serialized as a string.
Full example: a customer-service bot with mixed message types
Imagine a WeChat intelligent robot customer service (referred to as intelligent customer service): it uses the Kimi large language model to answer customer questions, and can reply not only with text messages but also with images, link cards, voice messages, and other types of messages, mixing different types of messages in a single response. For example, for customer product inquiries, it provides a text reply, a product image, and finally a purchase link (in the form of a link card). The following code demonstrates how to use JSON Mode in this scenario to make the model output replies in a fixed structure, and how to parse each type of message in the returned content:The examples on this page use the latest model
kimi-k3 by default. K3 configures reasoning effort with the top-level reasoning_effort request field (supports "low" / "high" / "max", default "max"). To use another model such as kimi-k2.6 or kimi-k2.5, just replace the model field โ parameter configurations differ across models. See the Model Parameter Reference.- python
- node.js
Troubleshoot truncated JSON output
If you have correctly set theresponse_format parameter and specified the format of the JSON document in the prompt, but the JSON document you receive is incomplete or truncated and cannot be parsed correctly, check whether the finish_reason field in the return value is length.
A smaller max_tokens value will cause the modelโs output to be truncated, and this rule also applies when using JSON Mode. We recommend estimating the size of the output JSON document and setting a reasonable max_tokens value, so that you can correctly parse the JSON document returned by the Kimi large language model.
For a more detailed explanation of incomplete or truncated output from the Kimi large language model, see Troubleshooting.
Notes
- The Kimi large language model only generates JSON Object type JSON documents; do not prompt it to generate JSON Array or other types of JSON documents.
- If you do not correctly inform the Kimi large language model of the required JSON Object format, it will generate unexpected results.