Why LLM Structured Output Strict Mode Rejects Valid JSON Schema (additionalProperties & Required Keys)
Switching from prompt-engineered JSON to strict Structured Outputs ( response_format: { type: "json_schema", strict: true } ) often triggers 400 Invalid schema errors on schemas that pass standard JSON Schema validators. Two strict-mode constraints cause almost every failure. 1. Every Object—Including Nested Ones—Needs additionalProperties: false Setting additionalProperties: false only on the…
When transitioning from prompt-engineered JSON to strict Structured Outputs, using response_format: { type: "json_schema", strict: true } often results in 400 Invalid schema errors. This occurs even when the schema passes standard JSON Schema validators. Two main constraints trigger these errors: every object, including nested objects, must have additionalProperties set to false, and every key in properties must also appear in required.
Setting additionalProperties: false only on the root object is insufficient. Strict mode mandates that every nested object inside properties, items, or anyOf explicitly prevents extra keys. Furthermore, optional properties cannot be omitted from required. When modeling an optional field, it must remain in required and its type should be made nullable, such as { "type": "object", "additionalProperties": false, "properties": { "summary": { "type": "string" }, "errorCode": { "type": ["string", "null"] } }, "required": ["summary", "errorCode"] }.
Another crucial consideration is the variability of format keywords (uri, custom formats, complex regex patterns) across different providers like OpenAI, Gemini, and Anthropic. Identifying strict-mode violations locally before submitting to the API can save time and effort. Tools such as openapi-zod-lint (MIT licensed, node-based), a Prompt/Structured Output Schema Converter, an LLM Token Calculator, and a cURL Converter are available to assist in this process.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to