DEV Community

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 root object is not enough. Structured Output engines compile a constrained grammar ahead of decoding, so every nested object inside properties, items, or anyOf must explicitly forbid extra keys.

2. Optional Properties Cannot Be Omitted From required

In strict mode, every key in properties must appear in required. To model an optional field, keep it in required and make its type nullable:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "summary": { "type": "string" },
    "errorCode": { "type": ["string", "null"] }
  },
  "required": ["summary", "errorCode"]
}

3. Cross-Provider Format Quirks

Format keywords (uri, custom formats, complex regex pattern) behave differently across OpenAI, Gemini, and Anthropic. Catching strict-mode violations locally before hitting the API saves trial-and-error cycles.

Local CLI & Zero-Upload Browser Tools

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.