response_format when your code needs to parse the model’s reply. The model then responds with JSON in message.content.
JSON schema
Settype to json_schema and pass a json_schema object with a name (1 to 64 ASCII letters, digits, underscores, or hyphens) and a JSON Schema schema with an object root. You can also add a description. A json_schema request without the json_schema object returns a 400 error with code missing_required_parameter.
message.content matches the schema:
Schema
Output
Strict mode
Setjson_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, and every array sets items.
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, pattern constructs, and format values are also refused.
A strict schema can also have at most:
- 10 levels of nested objects
- 5,000 properties
- 1,000
enumvalues - 120,000 characters of names and string literals
$ref can point only to the root schema or to one of its top-level definitions. minimum without maximum is allowed only as 0, or as 1 on an integer.
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 parsingcontent, check the response:
finish_reason: "length"means the output was cut off and is probably not valid JSON. Raisemax_completion_tokens.- A non-null
message.refusalmeans the model declined the request;contentdoes not contain your JSON.