Implementation:Openai Openai node ZodToJsonSchema
| Knowledge Sources | |
|---|---|
| Domains | Schema_Validation, Type_Safety |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Vendored tool for converting Zod schemas to JSON Schema format provided by the openai-node SDK's internal zod-to-json-schema library.
Description
The zodToJsonSchema function is a vendored (bundled) version of the zod-to-json-schema library, customized for OpenAI's strict mode requirements. For Zod v3, it uses this vendored converter with openaiStrictMode: true. For Zod v4, the SDK uses Zod's native z4.toJSONSchema() function followed by toStrictJsonSchema() post-processing.
The converter traverses the Zod schema tree via a parseDef dispatcher function that handles each Zod type (ZodObject, ZodArray, ZodString, ZodNumber, ZodEnum, ZodUnion, etc.) and produces the corresponding JSON Schema node.
Usage
This function is called internally by zodResponseFormat, zodTextFormat, zodFunction, and zodResponsesFunction. Direct usage is not typically needed.
Code Reference
Source Location
- Repository: openai-node
- File: src/_vendor/zod-to-json-schema/zodToJsonSchema.ts
- Lines: L7-17
Signature
export function zodToJsonSchema<Target extends 'jsonSchema7' | 'jsonSchema2019-09' = 'jsonSchema7'>(
schema: ZodSchema<any>,
options?: Partial<Options<Target>> | string,
): JsonSchema7Type & { $schema?: string; definitions?: Record<string, JsonSchema7Type> };
Import
// Internal usage within the SDK:
import { zodToJsonSchema } from '../_vendor/zod-to-json-schema';
// Zod v3 path (via vendored converter):
zodV3ToJsonSchema(schema, { name })
// Zod v4 path (via native + strictification):
toStrictJsonSchema(z4.toJSONSchema(schema, { target: 'draft-7' }))
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| schema | ZodSchema<any> | Yes | Any Zod schema (z.object, z.array, z.string, etc.) |
| options | string | No | Conversion options: name, target draft version, $ref strategy, openaiStrictMode |
Outputs
| Name | Type | Description |
|---|---|---|
| jsonSchema | JsonSchema7Type | JSON Schema Draft 7 object with optional $schema and definitions properties |
Usage Examples
Internal Usage by zodResponseFormat
import { z } from 'zod';
import { zodResponseFormat } from 'openai/helpers/zod';
// zodResponseFormat internally calls zodToJsonSchema:
const format = zodResponseFormat(
z.object({
name: z.string(),
age: z.number(),
}),
'person',
);
// Resulting JSON Schema (internally generated):
// {
// type: 'object',
// properties: { name: { type: 'string' }, age: { type: 'number' } },
// required: ['name', 'age'],
// additionalProperties: false,
// }