Zod to JSON Schema conversion
zodToJsonSchema(schema, options) converts common Zod types into JSON Schema used for prompt injection and provider-native structured output.
const generic = zodToJsonSchema(schema)
const strict = zodToJsonSchema(schema, { strict: true })
Generic mode
Generic mode is optimized for prompt injection and JSON-mode providers.
- Optional/default/catch properties are not added to
required. nullable(T)becomesTplusnullable: true.- Objects set
additionalProperties: false.
Strict mode
Strict mode is used for json_schema and tool_call requests.
- Every object key is listed in
required. - Optional/default/catch properties become
anyOf [T, { type: 'null' }]. - Nullable properties become
anyOf [T, null]instead of the non-standardnullablekeyword.
Supported types
| Zod | JSON Schema |
|---|---|
string() | type: 'string' plus min/max/length/email/url/uuid/regex checks |
number() | type: 'number'; .int() becomes integer; min/max/multipleOf mapped |
boolean() | type: 'boolean' |
literal(v) | enum: [v] |
enum / nativeEnum | enum: [...] |
array(T) | type: 'array', items, min/max/exact length |
object({...}) | properties, required, additionalProperties: false |
record(V) | type: 'object', additionalProperties: V |
union([...]) | anyOf |
discriminatedUnion | oneOf |
tuple([...]) | prefixItems, minItems, maxItems; optional rest mapped to items |
intersection(A, B) | allOf |
date() | type: 'string', format: 'date-time' |
| wrappers | optional/default/catch/readonly/branded/effects/pipeline are unwrapped with description preserved |
Unsupported types fall back to a description marker instead of throwing, so prompt-based flows can still run while native strict flows should be tested before rollout.