JSON Schema Output Guard
A schema preflight and bounded-repair receipt for model output contracts. Call guard(raw,schema) at the application boundary, or repair(generate,schema,maxAttempts) where generate receives structured Issue[] and a one-based attempt number.
A schema preflight and bounded-repair receipt for model output contracts.
- Call guard(raw,schema) at the application boundary, or repair(generate,schema,maxAttempts) where generate receives structured Issue[] and a one-based attempt number.
- Node 22.18+ and Python 3 for the grader
- TypeScript unions, recursive functions, JSON and async callbacks.
JSON Schema Output Guard in 4 stages
A schema preflight and bounded-repair receipt for model output contracts.
Parse the untrusted boundary
A model response is a byte string. Parse the entire string as JSON, reject markdown wrappers and trailing text, and cap its UTF-8 byte size before parsing. Accept JSON primitives as well as objects: the schema, not the parser, determines whether a primitive is useful. Do not remove text until it happens to parse because that hides the actual output contract.
starter · ~2 hours · TypeScript
parseJSONWalk a schema recursively
Implement object, array and primitive validation. Required properties are checked with own-property semantics. An inherited property does not satisfy the contract. Treat integer as a refinement of number, reject non-finite numbers, and preserve a path for every failure. Bound recursive descent at depth 32 so a schema and value cannot consume the stack indefinitely.
core · ~2 hours · TypeScript
validateReject ambiguous and unsupported contracts
Add numeric bounds, enum membership, minimum Unicode string length, array bounds, and closed objects. Escape slash and tilde inside property paths. A keyword outside this educational subset is a configuration error, not silent success. This implementation intentionally does not claim complete JSON Schema conformance: references, formats and combinators require additional work.
core · ~2 hours · TypeScript
validateguardRepair with a finite budget
Feed only structured validation feedback into a generation callback. Count each call before interpreting its output. Stop immediately after acceptance and return an explicit exhausted state after the final rejected attempt. Provider failures propagate instead of being disguised as schema failures. The trace records each attempt so a successful third response does not conceal two earlier contract violations.
core · ~2 hours · TypeScript
repair