StackPractices

LLM Structured Output Generator

Turn example JSON into an OpenAI strict JSON Schema, a Zod schema, a Pydantic model and a TypeScript interface. The strict-compatibility report explains every inference instead of hiding the hard parts.

1. Example JSON

Privacy: inference and validation run entirely in your browser. Small inputs may be encoded in the URL hash so a share link restores the state; do not share that link if the JSON is sensitive.

2. Validate another document

Generated artifacts

Paste valid JSON to generate output.

What are Structured Outputs?

Structured Outputs make an LLM response conform to a machine-readable contract. Instead of asking the model to “return JSON” and hoping for the best, the API receives a JSON Schema and constrains decoding so the response matches that schema. This matters when the response is fed directly into typed application code, workflow automation or tool calls.

A useful schema starts from real data. That is why this json schema generator infers a contract from an example, but it does not pretend inference is magic: empty arrays, empty objects, mixed arrays and integer-versus-float evidence are all ambiguous. The warnings panel keeps those decisions visible.

OpenAI strict-mode rules, translated into practice

OpenAI strict mode is narrower than full JSON Schema. Objects must be closed with additionalProperties: false, every property must be listed in required, and unsupported validation keywords such as pattern, format, minimum, maximum, minLength, maxLength, minItems and maxItems must not be emitted. Nested objects follow the same rules.

The consequence is subtle: strict mode does not use ordinary optional object properties. When a field is absent from some objects in an array, the strict schema keeps it required and makes its type nullable. Zod, Pydantic and TypeScript can represent absence directly, so those tabs keep the evidence-based optional shape while the OpenAI tab uses the strict-compatible projection.

From inferred schema to production request

The OpenAI request tab wraps the strict schema in the API's json_schema response format, including a stable schema name and strict: true. That removes the usual final step of manually nesting the schema into the request payload.

Zod output is also strict: every inferred object uses .strict() so runtime validation rejects unknown keys just like the OpenAI schema. Optional format detection upgrades strings to email, URL, UUID or datetime validators only when every observed value for that field matches the format. Those hints are intentionally omitted from the OpenAI schema because format is outside the supported strict subset.

Zod vs Pydantic vs JSON Schema

Artifact Best use Key difference
OpenAI strict JSON SchemaStructured Outputs response contractsClosed objects, complete required lists, nullable projections for absence
ZodTypeScript runtime parsingComposable validators and static type inference
PydanticPython APIs and data pipelinesClass-based models, aliases and strict scalar types
TypeScript interfaceCompile-time documentationNo runtime validation by itself

Why heterogeneous arrays need a warning

Arrays often look uniform in a small example and become mixed in production. If one item is a number and another is a string, there is no single item schema that preserves both meanings. The generator uses a strict-compatible union and warns you, but the better fix is usually upstream: normalize the data contract, split the field or use tagged objects with a discriminator.

Dynamic maps have a similar problem. JSON objects whose keys are IDs, dates or hashes do not describe a fixed object shape. Because strict mode does not support patternProperties, this tool treats the observed keys as fixed evidence and warns that the result may be too narrow for a true dictionary.

Frequently asked questions

How do I convert JSON to Zod?

Paste a representative JSON document and open the Zod tab. The tool infers strings, numbers, booleans, arrays and nested objects, merges object shapes inside arrays and emits copyable TypeScript using z.object, z.array, z.union, .nullable and .optional where the evidence supports them.

How do I convert JSON to a Pydantic model?

Paste JSON and open the Pydantic tab. The generator creates Python classes with StrictStr, int, float, bool, List, Optional and Union. Keys that are not valid Python identifiers are converted to snake_case fields with Field aliases so the original JSON keys still round-trip.

What are OpenAI Structured Outputs?

Structured Outputs are API responses constrained by a JSON Schema supplied by the developer. In strict mode the model is constrained to produce JSON matching that schema instead of merely being prompted to try. The schema must stay inside the supported strict subset.

Why does strict mode make optional fields nullable?

OpenAI strict mode requires every property in an object schema to appear in required. A field that is absent from some sample objects cannot simply be omitted from required, so this tool keeps it required and adds null to its type, such as string or null. Zod and TypeScript still show the evidence-based optional marker because those languages model absence directly.

Why are heterogeneous arrays a problem?

A schema is most useful when array items have one stable shape. An array such as a number followed by a string has no single item type, so the generator represents it as a strict-compatible anyOf union and warns that application code will need narrower validation or a better data contract.

Can this JSON schema generator validate another document?

Yes. Paste a second JSON document in validate mode and the browser checks the supported subset locally: types, required properties, additionalProperties: false, nested objects, arrays and anyOf branches. It does not implement unsupported strict-mode keywords such as pattern, minimum, maximum, minItems or format.

Is it safe to paste private JSON here?

Generation and validation run entirely in client-side JavaScript. Input is not sent to a server. Small inputs can be encoded in the URL hash to restore state, so do not share the generated link if the JSON contains secrets or personal data.

Related resources