Skip to main content
Use response_format when your code needs to parse the model’s reply. The model then responds with JSON in message.content.

JSON schema

Set type to json_schema and pass a json_schema object with a name (up to 64 characters) and a JSON Schema schema. You can also add a description. A json_schema request without the json_schema object returns a 400 error with code missing_required_parameter.
Output

Strict mode

Set json_schema.strict to true to enable strict schema adherence. In strict mode, schema must be an object schema in which every object sets additionalProperties: false and lists all of its properties in required, as the example above does. Strict schemas can’t use these keywords: allOf, oneOf, not, if, then, else, contains, minContains, maxContains, uniqueItems, unevaluatedItems, propertyNames, minProperties, maxProperties, unevaluatedProperties, dependentRequired, dependentSchemas, patternProperties, multipleOf, exclusiveMinimum, and exclusiveMaximum. Some keyword combinations and pattern constructs are also refused. The same rules apply to a tool’s parameters when the tool sets strict: true. See Tool calling.

JSON mode

{"type": "json_object"} asks for any valid JSON object, without a schema. Describe the shape you want in the prompt, and say that the answer should be JSON. JSON mode doesn’t apply a schema, so a request that sends json_schema with type: "json_object" or type: "text" returns a 400 error with code unsupported_parameter. To have the output match a schema, use type: "json_schema".

Handle edge cases

Before parsing content, check the response:
  • finish_reason: "length" means the output was cut off and is probably not valid JSON. Raise max_completion_tokens.
  • A non-null message.refusal means the model declined the request; content does not contain your JSON.
To have the model return arguments for your own functions rather than a final answer, use tool calling instead.