WRITE · TRACE · UNDERSTAND

Validate a closed structured-output schema without coercion

Return ordered path errors from root $.

Read lesson: Validate a closed structured-output schema without coercion →

Your experiment

Return ordered path errors from root $. Support only type (object,array,string,integer,number,boolean,null), enum, closed-object properties/required/additionalProperties=False, array items/maxItems, string maxLength. Unknown or inapplicable schema keywords raise ValueError before checking data. Object children visit sorted keys, missing requirements first; arrays visit index order. Errors use :type,:enum,:missing,:extra,:length. Numeric booleans are not integers/numbers and numbers must be finite. enum uses exact Python type and equality. Omitted additionalProperties means closed. This is an explicit JSON-like subset, not a general JSON Schema implementation.

Python runs in your browser. Your draft stays on this device.

What to look for

Review schema-version compatibility and unsupported vocabulary. Structural validity alone does not establish permission, factual correctness or safe execution.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Validate a closed structured-output schema without coercion works

The idea

Structured output is only useful if the program that receives it can rely on its shape. A strict validator walks the model's value and the schema together and reports every place where they disagree, without repairing anything. The string "5" is not an integer, True is not a number, and a key that the schema does not name is an error.

validate_output(value, schema) checks the schema first and raises ValueError for any keyword outside its small supported set, so an unsupported rule is never silently skipped. It then recurses from the root path $, building paths such as $.tags[0], and returns a list of errors tagged :type, :enum, :missing, :extra or :length in a fixed order.

How it works

  1. Check the schema with check_schema: a supported type, only keywords that apply to that type, closed objects, nonnegative integer limits.
  2. At each node test the exact Python type, and on failure record path:type and go no deeper.
  3. If the node has enum, require a choice of the same type with an equal value, otherwise record path:enum.
  4. For an object, report absent required keys first, then visit the keys in sorted order and flag unknown ones as :extra.
  5. For an array, check maxItems and visit each element in index order with a path such as $.tags[1].
  6. For a string, check maxLength; the returned error list is empty when the value conforms.

What it costs

Scales with
value and schema size
Passes
schema once, value once
Memory
depth plus error list

Each schema node is checked once and each value node is visited once, and object keys are sorted so that the errors always come out in the same order.

Where it is used

  • OpenAI's Structured Outputs in strict mode require additionalProperties: false and every property listed in required.
  • The jsonschema Python package validates JSON documents against a schema and reports each error with the path to the failing value.
  • Pydantic's strict mode refuses to coerce types, so the string "123" fails an int field instead of being converted.

Watch out for

  • Testing integers with isinstance(x, int): True passes, because bool is a subclass of int in Python.
  • Ignoring schema keywords the validator does not support, which lets output through unchecked instead of raising ValueError.
  • Comparing enum values with == alone: 1 == True and 1 == 1.0 in Python, so the types have to match as well.