zodTextFormat breaks with Zod 4
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [x] This is an issue with the Node library Describe the bug As per this issue, `zod-to-json` library, which I believe OpenAI SDK uses under the hood, is not compatible with Zod v4. So e.g. this official tutorial won't work. Also the error is not very descriptive, as we only get: [code block] To Reproduce Install latest zod (4.x.x) and openai [code block] Then this snippet will fail. [code block] then rerun the same code with zod 3 and it should work [code block] [code block]JavaScript ``` OS macOS Node version 22.14.0 Library version openai@5.11.0
Error Output
error is not very descriptive, as we only get:
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: zodTextFormat breaks with Zod 4
For anyone hitting this, you can work around like this: [code block] And then use your own `zodTextForrmat` instead of the broken one. TypeScript inference will work the same way.
Trust Score
5 verifications
- 1
For anyone hitting this, you can work around like this:
For anyone hitting this, you can work around like this:
tsimport { z } from "zod"; import type { zodTextFormat as openAIBrokenZodTextFormatButWhichHasCorrectTypeScriptTypes } from "openai/helpers/zod"; export const zodTextFormat: typeof openAIBrokenZodTextFormatButWhichHasCorrectTypeScriptTypes = ( zodSchema, name, ) => { return { type: "json_schema", schema: z.toJSONSchema(zodSchema), name, } as ReturnType<typeof openAIBrokenZodTextFormatButWhichHasCorrectTypeScriptTypes> as never; }; - 2
And then use your own `zodTextForrmat` instead of the broken one. TypeScript inf
And then use your own `zodTextForrmat` instead of the broken one. TypeScript inference will work the same way.
Validation
Resolved in openai/openai-node GitHub issue #1602. Community reactions: 9 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep