@techery/zod-to-openai-schema
Version:
Convert Zod schemas to OpenAI structured output compatible schemas
36 lines • 1.4 kB
TypeScript
import { z, ZodTypeAny } from 'zod';
type JSONSchemaType = 'string' | 'number' | 'boolean' | 'integer' | 'object' | 'array' | 'null';
/**
* Minimal JSON Schema for OpenAI structured outputs,
* excluding unsupported fields (minItems, maxItems, min/max, etc.).
*/
export interface OpenAIStructuredOutputSchema {
$ref?: string;
type?: JSONSchemaType;
enum?: any[];
anyOf?: OpenAIStructuredOutputSchema[];
description?: string;
properties?: Record<string, OpenAIStructuredOutputSchema>;
required?: string[];
items?: OpenAIStructuredOutputSchema;
additionalProperties?: boolean;
$defs?: Record<string, OpenAIStructuredOutputSchema>;
}
export interface Definition {
name: string;
schema: z.ZodObject<any>;
}
export declare function definition(name: string, schema: z.ZodObject<any>): Definition;
interface Config {
definitions?: Definition[];
}
/**
* 2) Convert the root Zod schema into JSONSchema:
* - If a ZodObject node is repeated (usage>1), place it in $defs and reference it.
* - All other node types (array, union, lazy, etc.) are inlined, even if repeated.
* - The root schema is never just "$ref".
* - additionalProperties=false for objects.
*/
export declare function zodToOpenAISchema(rootSchema: ZodTypeAny, config?: Config): OpenAIStructuredOutputSchema;
export {};
//# sourceMappingURL=zod-to-openai-schema.d.ts.map