Skip to content

Commit cbf5f81

Browse files
committed
fixes
1 parent 7be8d10 commit cbf5f81

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

examples/parsing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { zodResponseFormat } from 'openai/helpers/zod';
22
import OpenAI from 'openai/index';
3-
import { z } from 'zod/v3';
3+
import { z } from 'zod/v3'; // Also works for 'zod/v4'
44

55
const Step = z.object({
66
explanation: z.string(),

src/helpers/zod.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function zodToJsonSchema(schema: ZodType, options: { name: string }): Record<str
2525
});
2626
}
2727

28+
function nativeToJsonSchema(schema: ZodTypeV4): Record<string, unknown> {
29+
return toJSONSchema(schema, { target: 'draft-7' }) as Record<string, unknown>;
30+
}
31+
2832
function isZodV4(zodObject: ZodType | ZodTypeV4): zodObject is ZodTypeV4 {
2933
return '_zod' in zodObject;
3034
}
@@ -90,7 +94,7 @@ export function zodResponseFormat<ZodInput extends ZodType | ZodTypeV4>(
9094
strict: true,
9195
schema:
9296
isZodV4(zodObject) ?
93-
(toStrictJsonSchema(toJSONSchema(zodObject) as JSONSchema) as Record<string, unknown>)
97+
(toStrictJsonSchema(nativeToJsonSchema(zodObject)) as Record<string, unknown>)
9498
: zodToJsonSchema(zodObject, { name }),
9599
},
96100
},
@@ -121,7 +125,7 @@ export function zodTextFormat<ZodInput extends ZodType | ZodTypeV4>(
121125
strict: true,
122126
schema:
123127
isZodV4(zodObject) ?
124-
(toStrictJsonSchema(toJSONSchema(zodObject) as JSONSchema) as Record<string, unknown>)
128+
(toStrictJsonSchema(nativeToJsonSchema(zodObject)) as Record<string, unknown>)
125129
: zodToJsonSchema(zodObject, { name }),
126130
},
127131
(content) => zodObject.parse(JSON.parse(content)),
@@ -166,7 +170,7 @@ export function zodFunction<Parameters extends ZodType | ZodTypeV4>(options: {
166170
name: options.name,
167171
parameters:
168172
isZodV4(options.parameters) ?
169-
toJSONSchema(options.parameters)
173+
nativeToJsonSchema(options.parameters)
170174
: zodToJsonSchema(options.parameters, { name: options.name }),
171175
strict: true,
172176
...(options.description ? { description: options.description } : undefined),

tests/helpers/zod.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { zodResponseFormat } from 'openai/helpers/zod';
2-
import { z } from 'zod/v3';
2+
import { z as zv3 } from 'zod/v3';
3+
import { z as zv4 } from 'zod';
34

4-
describe('zodResponseFormat', () => {
5+
describe.each([
6+
{ version: 'v3', z: zv3 as any },
7+
{ version: 'v4', z: zv4 as any },
8+
])('zodResponseFormat (Zod $version)', ({ version, z }) => {
59
it('does the thing', () => {
610
expect(
711
zodResponseFormat(
@@ -286,7 +290,7 @@ describe('zodResponseFormat', () => {
286290
`);
287291
});
288292

289-
it('throws error on optional fields', () => {
293+
(version === 'v4' ? it.skip : it)('throws error on optional fields', () => {
290294
expect(() =>
291295
zodResponseFormat(
292296
z.object({
@@ -301,7 +305,7 @@ describe('zodResponseFormat', () => {
301305
);
302306
});
303307

304-
it('throws error on nested optional fields', () => {
308+
(version === 'v4' ? it.skip : it)('throws error on nested optional fields', () => {
305309
expect(() =>
306310
zodResponseFormat(
307311
z.object({

0 commit comments

Comments
 (0)