Skip to main content
Set response_format to make the model answer in JSON. The output is constrained while it is generated, so it is always valid JSON and, with a schema, always matches it.

JSON schema

Python

Any JSON object

{"type": "json_object"} asks for valid JSON without a fixed shape. Say in the prompt what the object should contain.

Tips

  • Describe the fields in the prompt as well; the schema decides the shape, the prompt decides the content.
  • Set max_tokens high enough for the whole object. An answer cut off at max_tokens (finish_reason: "length") is not valid JSON.