Skip to content

fix(zod): convert oneOf unions to anyOf#1904

Open
FU-max-boop wants to merge 2 commits into
openai:masterfrom
FU-max-boop:fix/zod-oneof-unions
Open

fix(zod): convert oneOf unions to anyOf#1904
FU-max-boop wants to merge 2 commits into
openai:masterfrom
FU-max-boop:fix/zod-oneof-unions

Conversation

@FU-max-boop
Copy link
Copy Markdown

@FU-max-boop FU-max-boop commented May 28, 2026

Summary

Fixes #1709.

Zod v4 emits oneOf for discriminated unions, but OpenAI strict JSON schemas do not allow oneOf. The Zod v4 helper path already runs schemas through toStrictJsonSchema, so this converts oneOf unions into strict-compatible anyOf unions during that normalization pass.

The regression test covers a Zod discriminated union through zodResponseFormat and asserts the final schema no longer contains oneOf.

Tests

  • npx --yes [email protected] test tests/helpers/zod.test.ts --runInBand
  • npx --yes [email protected] --check src/lib/transform.ts tests/helpers/zod.test.ts
  • ./node_modules/.bin/eslint src/lib/transform.ts tests/helpers/zod.test.ts
  • git diff --check

Note: I also ran ./node_modules/typescript/bin/tsc --noEmit; it surfaced missing optional example dependencies in this checkout (@azure/identity, express, next). The only PR-local type issue it reported was fixed before the checks above were rerun.

@FU-max-boop FU-max-boop requested a review from a team as a code owner May 28, 2026 22:54
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9cae2ab456

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/lib/transform.ts Outdated
// OpenAI strict schemas only support anyOf.
const oneOf = jsonSchema.oneOf;
if (Array.isArray(oneOf)) {
jsonSchema.anyOf = [...(jsonSchema.anyOf ?? []), ...oneOf];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve existing anyOf constraints when converting oneOf

When a schema node already has an anyOf, JSON Schema applies the existing anyOf and oneOf as separate constraints, but this merge turns all branches into alternatives. For callers of toStrictJsonSchema that pass a valid schema containing both keywords on the same node, values that satisfy only a former oneOf branch can now be accepted even if they fail the original anyOf constraint, broadening the response schema instead of preserving it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8882406f: when a schema node already has anyOf, the converted oneOf is kept as a separate allOf constraint instead of merging branches into the existing anyOf. I also added a regression test covering the mixed anyOf + oneOf case.

@FU-max-boop FU-max-boop force-pushed the fix/zod-oneof-unions branch from 9cae2ab to 9eee578 Compare May 29, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zod schema conversion broken for unions in Zod 4.1.13+

1 participant