-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Add support for zod@4 schemas #1666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8f56474
7be8d10
cbf5f81
6b4ae77
a1a4718
19713bf
747c0df
284cfe7
7a98adb
98e5b8c
a324c80
a532410
f3ab503
946a7b0
d4aaef9
2ddb9c9
704ba4c
1192cb2
646c820
dfed1f0
195a6ed
79a8c53
2fbb1ce
ddeb05e
95ce1a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { ResponseFormatJSONSchema } from '../resources/index'; | ||
| import type { infer as zodInfer, ZodType } from 'zod/v3'; | ||
| import { z as z3 } from 'zod/v3'; | ||
| import { z as z4 } from 'zod/v4'; | ||
| import { | ||
| AutoParseableResponseFormat, | ||
| AutoParseableTextFormat, | ||
|
|
@@ -11,8 +12,15 @@ import { | |
| import { zodToJsonSchema as _zodToJsonSchema } from '../_vendor/zod-to-json-schema'; | ||
| import { AutoParseableResponseTool, makeParseableResponseTool } from '../lib/ResponsesParser'; | ||
| import { type ResponseFormatTextJSONSchemaConfig } from '../resources/responses/responses'; | ||
| import { toStrictJsonSchema } from '../lib/transform'; | ||
| import { JSONSchema } from '../lib/jsonschema'; | ||
|
|
||
| function zodToJsonSchema(schema: ZodType, options: { name: string }): Record<string, unknown> { | ||
| type InferZodType<T> = | ||
| T extends z4.ZodType ? z4.infer<T> | ||
| : T extends z3.ZodType ? z3.infer<T> | ||
| : never; | ||
|
|
||
| function zodV3ToJsonSchema(schema: z3.ZodType, options: { name: string }): Record<string, unknown> { | ||
| return _zodToJsonSchema(schema, { | ||
| openaiStrictMode: true, | ||
| name: options.name, | ||
|
|
@@ -22,6 +30,18 @@ function zodToJsonSchema(schema: ZodType, options: { name: string }): Record<str | |
| }); | ||
| } | ||
|
|
||
| function zodV4ToJsonSchema(schema: z4.ZodType): Record<string, unknown> { | ||
| return toStrictJsonSchema( | ||
| z4.toJSONSchema(schema, { | ||
| target: 'draft-7', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious how we picked this value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've lost the context here a little bit, but looking at it again, it seems like we shouldn't set the concrete target. I'll remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I think I did that because the current vendored version that parses Zod schema to JSON Schema is using draft-07 by default |
||
| }) as JSONSchema, | ||
| ) as Record<string, unknown>; | ||
| } | ||
|
|
||
| function isZodV4(zodObject: z3.ZodType | z4.ZodType): zodObject is z4.ZodType { | ||
| return '_zod' in zodObject; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a chat completion `JSONSchema` response format object from | ||
| * the given Zod schema. | ||
|
|
@@ -59,37 +79,37 @@ function zodToJsonSchema(schema: ZodType, options: { name: string }): Record<str | |
| * This can be passed directly to the `.create()` method but will not | ||
| * result in any automatic parsing, you'll have to parse the response yourself. | ||
| */ | ||
| export function zodResponseFormat<ZodInput extends ZodType>( | ||
| export function zodResponseFormat<ZodInput extends z3.ZodType | z4.ZodType>( | ||
| zodObject: ZodInput, | ||
| name: string, | ||
| props?: Omit<ResponseFormatJSONSchema.JSONSchema, 'schema' | 'strict' | 'name'>, | ||
| ): AutoParseableResponseFormat<zodInfer<ZodInput>> { | ||
| ): AutoParseableResponseFormat<InferZodType<ZodInput>> { | ||
| return makeParseableResponseFormat( | ||
| { | ||
| type: 'json_schema', | ||
| json_schema: { | ||
| ...props, | ||
| name, | ||
| strict: true, | ||
| schema: zodToJsonSchema(zodObject, { name }), | ||
| schema: isZodV4(zodObject) ? zodV4ToJsonSchema(zodObject) : zodV3ToJsonSchema(zodObject, { name }), | ||
| }, | ||
| }, | ||
| (content) => zodObject.parse(JSON.parse(content)), | ||
| ); | ||
| } | ||
|
|
||
| export function zodTextFormat<ZodInput extends ZodType>( | ||
| export function zodTextFormat<ZodInput extends z3.ZodType | z4.ZodType>( | ||
| zodObject: ZodInput, | ||
| name: string, | ||
| props?: Omit<ResponseFormatTextJSONSchemaConfig, 'schema' | 'type' | 'strict' | 'name'>, | ||
| ): AutoParseableTextFormat<zodInfer<ZodInput>> { | ||
| ): AutoParseableTextFormat<InferZodType<ZodInput>> { | ||
| return makeParseableTextFormat( | ||
| { | ||
| type: 'json_schema', | ||
| ...props, | ||
| name, | ||
| strict: true, | ||
| schema: zodToJsonSchema(zodObject, { name }), | ||
| schema: isZodV4(zodObject) ? zodV4ToJsonSchema(zodObject) : zodV3ToJsonSchema(zodObject, { name }), | ||
| }, | ||
| (content) => zodObject.parse(JSON.parse(content)), | ||
| ); | ||
|
|
@@ -100,23 +120,26 @@ export function zodTextFormat<ZodInput extends ZodType>( | |
| * automatically by the chat completion `.runTools()` method or automatically | ||
| * parsed by `.parse()` / `.stream()`. | ||
| */ | ||
| export function zodFunction<Parameters extends ZodType>(options: { | ||
| export function zodFunction<Parameters extends z3.ZodType | z4.ZodType>(options: { | ||
| name: string; | ||
| parameters: Parameters; | ||
| function?: ((args: zodInfer<Parameters>) => unknown | Promise<unknown>) | undefined; | ||
| function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined; | ||
| description?: string | undefined; | ||
| }): AutoParseableTool<{ | ||
| arguments: Parameters; | ||
| name: string; | ||
| function: (args: zodInfer<Parameters>) => unknown; | ||
| function: (args: InferZodType<Parameters>) => unknown; | ||
| }> { | ||
| // @ts-expect-error TODO | ||
| return makeParseableTool<any>( | ||
| { | ||
| type: 'function', | ||
| function: { | ||
| name: options.name, | ||
| parameters: zodToJsonSchema(options.parameters, { name: options.name }), | ||
| parameters: | ||
| isZodV4(options.parameters) ? | ||
| zodV4ToJsonSchema(options.parameters) | ||
| : zodV3ToJsonSchema(options.parameters, { name: options.name }), | ||
| strict: true, | ||
| ...(options.description ? { description: options.description } : undefined), | ||
| }, | ||
|
|
@@ -128,21 +151,24 @@ export function zodFunction<Parameters extends ZodType>(options: { | |
| ); | ||
| } | ||
|
|
||
| export function zodResponsesFunction<Parameters extends ZodType>(options: { | ||
| export function zodResponsesFunction<Parameters extends z3.ZodType | z4.ZodType>(options: { | ||
| name: string; | ||
| parameters: Parameters; | ||
| function?: ((args: zodInfer<Parameters>) => unknown | Promise<unknown>) | undefined; | ||
| function?: ((args: InferZodType<Parameters>) => unknown | Promise<unknown>) | undefined; | ||
| description?: string | undefined; | ||
| }): AutoParseableResponseTool<{ | ||
| arguments: Parameters; | ||
| name: string; | ||
| function: (args: zodInfer<Parameters>) => unknown; | ||
| function: (args: InferZodType<Parameters>) => unknown; | ||
| }> { | ||
| return makeParseableResponseTool<any>( | ||
| { | ||
| type: 'function', | ||
| name: options.name, | ||
| parameters: zodToJsonSchema(options.parameters, { name: options.name }), | ||
| parameters: | ||
| isZodV4(options.parameters) ? | ||
| zodV4ToJsonSchema(options.parameters) | ||
| : zodV3ToJsonSchema(options.parameters, { name: options.name }), | ||
| strict: true, | ||
| ...(options.description ? { description: options.description } : undefined), | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karpetrosyan any chance you could run all these examples with v3 on this PR just to make sure things are back-compatible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah thats a good callout, I think I asked to switch them to v4 🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked all of them locally, all worked with v3!