Type-Safe Structured Output Schema Generator (Zod / JSON Schema Strict Mode)
TypeScript generator converting Zod schemas into OpenAI and Anthropic JSON Schema Strict Mode formats with automated property validation.
Why Strict Mode Prevents Schema Hallucinations
OpenAI Structured Outputs guarantee 100% adherence to supplied schemas by constraining model decoding tokens. Generating schemas with explicit required keys and additionalProperties disabled eliminates JSON parse exceptions in production.
Implementation Code & Script
Recursively sets additionalProperties: false and ensures all keys are marked required per API specifications.
export function makeSchemaStrict(schema: any): any {
if (!schema || typeof schema !== 'object') return schema;
if (schema.type === 'object' && schema.properties) {
schema.additionalProperties = false;
schema.required = Object.keys(schema.properties);
for (const key of Object.keys(schema.properties)) {
schema.properties[key] = makeSchemaStrict(schema.properties[key]);
}
}
if (schema.type === 'array' && schema.items) {
schema.items = makeSchemaStrict(schema.items);
}
return schema;
}How to cite and attribute this tool
MIT LicenceThis resource is free, open and un-gated under the MIT Open Source Licence. You are encouraged to use, integrate and cite it with attribution:
Geraghty, G. (2026). Type-Safe Structured Output Schema Generator (Zod / JSON Schema Strict Mode). Gordon Geraghty Resources Hub. https://gordongeraghty.com/resources/ai-engineering/structured-output-schema-generator
BibTeX Format
@misc{geraghty_structured_output_schema_generator,
author = {Geraghty, Gordon},
title = {Type-Safe Structured Output Schema Generator (Zod / JSON Schema Strict Mode)},
year = {2026},
url = {https://gordongeraghty.com/resources/ai-engineering/structured-output-schema-generator},
note = {Head of Performance, Empire Amplify}
}Changelog & Version History
v1.0.0Initial release of Zod to JSON Schema strict converter.