JSON Schema vs. Zod v4 vs. Pydantic v2 for LLM Structured Outputs
How OpenAI Strict Mode, Anthropic Tool Use, Zod v4, and Pydantic v2 map through a Unified Type IR—and how to generate all three locally.
Use JSON Schema (additionalProperties: false, all fields required) as the wire contract for OpenAI/Anthropic Structured Outputs, Zod v4 for TypeScript runtime validation and MCP servers, and Pydantic v2 for Python FastAPI/agent pipelines. DataTo compiles between all three through a single Unified Type IR.
Why LLM Structured Outputs reject standard JSON Schemas
OpenAI's strict: true constrained-decoding engine requires every object schema to set additionalProperties: false, list every property inside required (using ["string", "null"] for optional fields), keep nesting depth <= 10, and avoid unsupported validation keywords like minimum or maxLength.
DataTo JSON to OpenAI Function automatically applies those rules while relocating stripped constraints into description with non-fatal diagnostics so the LLM still respects your bounds semantically.
| Feature | OpenAI Strict Schema | Zod v4 (.ts) | Pydantic v2 (.py) |
|---|---|---|---|
| Runtime Enforcement | Token-level LLM decoding | Node / Browser JS | Python (Rust core) |
| Optional Fields | Required + nullable union | .optional() | str | None = None |
| Extra Keys | Must set additionalProperties: false | .strict() | ConfigDict(extra='forbid') |
Constraints (min/max) | Moved to description | .min() / .max() | Field(ge=..., le=...) |
Generating Zod v4 and Pydantic v2 from the same JSON payload
Full-stack AI teams often build Next.js/TypeScript frontends alongside Python FastAPI inference services. Inferring both Zod v4 (z.email(), z.int(), z.infer) and topological Pydantic v2 BaseModel classes from one sample payload eliminates schema drift.
Tools used in this guide
Schemas & MCP →Convert sample JSON into strict OpenAI response_format (strict: true, additionalProperties: false) or Anthropic Tool Use schemas.
Convert JSON payloads into strict Zod v4 TypeScript validators (z.object, z.email(), z.int()) with z.infer type exports.
Infer topological Python Pydantic v2 BaseModel classes with list[...], | None unions, and v1 compatibility locally.
Compile JSON Schema ($defs, $ref, enum, minimum, minLength, description) into idiomatic Zod v4 TypeScript code.
Related formats
Related Comparisons
Frequently asked questions
What happens if my OpenAI strict schema root is an array instead of an object?
OpenAI requires an object root. DataTo automatically wraps non-object roots in { result: ... } and emits a ROOT_WRAPPED diagnostic warning.
Does JSON Schema to Zod preserve JSDoc descriptions and enums?
Yes. description strings map to .describe(...) and enum arrays map cleanly to z.enum([...]) or Python Literal[...].
Can I validate sample payloads against my JSON Schema right in the browser?
Yes. DataTo JSON Schema Validator runs full ajv Draft-07 and Draft 2020-12 checks locally with JSON Pointer error paths.
Authoritative References
More engineering guides
Automatically strip markdown code fences, fix single quotes, trailing commas, Python True/False literals, and truncated brackets from LLM JSON responses.
JSON Schema Draft 2020-12 to Zod v4, Pydantic v2 & TypeScriptCompile Draft 2020-12 and Draft-07 JSON Schemas with $defs, $ref, enums, and bounds into Zod v4, Pydantic v2, and TypeScript interfaces locally via AJV & IR.
OpenAPI to MCP: Exposing REST APIs as Model Context Protocol ToolsTurn internal OpenAPI 3.x YAML/JSON specs into Model Context Protocol (MCP) TypeScript tools and Zod v4 input schemas locally without uploading internal APIs.
Zero-Upload PII Redaction & Reversible Restore Before Sending Data to AIRedact emails, phone numbers, API keys, DB URIs, and salaries locally in browser RAM using [[TYPE-NN]] placeholders—then restore AI replies cleanly.