Engineering Guides6 min readUpdated: 2026-11-24

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.

Quick Answer

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.

FeatureOpenAI Strict SchemaZod v4 (.ts)Pydantic v2 (.py)
Runtime EnforcementToken-level LLM decodingNode / Browser JSPython (Rust core)
Optional FieldsRequired + nullable union.optional()str | None = None
Extra KeysMust set additionalProperties: false.strict()ConfigDict(extra='forbid')
Constraints (min/max)Moved to description.min() / .max()Field(ge=..., le=...)

Generate OpenAI / Anthropic tool schemas →

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.

Generate Zod v4 & Pydantic v2 locally →

Tools used in this guide

Schemas & MCP →

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