-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(provider-utils): handle anyOf/allOf/oneOf and definitions in addAdditionalPropertiesToJsonSchema #11507
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
fix(provider-utils): handle anyOf/allOf/oneOf and definitions in addAdditionalPropertiesToJsonSchema #11507
Conversation
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.
Pull request overview
This PR fixes a critical issue where schemas with complex structures like anyOf, allOf, oneOf, and definitions were failing validation with OpenAI's API due to missing additionalProperties: false requirements. The fix extends the addAdditionalPropertiesToJsonSchema function to recursively process these schema constructs.
- Adds support for handling union types (e.g.,
["object", "null"]) - Adds recursive processing of
anyOf,allOf,oneOf, anddefinitionsschema constructs - Introduces a helper
visitfunction to handleJSONSchema7Definitiontypes (which can be boolean or schema objects)
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
packages/provider-utils/src/add-additional-properties-to-json-schema.ts |
Extended function to handle complex schema structures including union types, composition operators, and schema definitions with recursive processing |
packages/provider-utils/src/add-additional-properties-to-json-schema.test.ts |
Added three new test cases covering union types, anyOf, and recursive references via definitions |
packages/provider-utils/src/__snapshots__/schema.test.ts.snap |
Updated snapshots to reflect additionalProperties: false now being added to schemas in definitions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/provider-utils/src/add-additional-properties-to-json-schema.test.ts
Show resolved
Hide resolved
Added tests to ensure that `addAdditionalPropertiesToJsonSchema` correctly sets `additionalProperties: false` for object schemas within `allOf` and `oneOf` constructs in JSON Schema. This enhances the validation of complex schema structures.
Background
#11078 updated
z4. toJSONSchema()inpackages/provider-utils/src/schema.tsto useio: 'input'instead ofio: 'output'With
io: 'input', zod does not automatically setadditionalProperties: false, soaddAdditionalPropertiesToJsonSchemawas added as a post-processor.The current implementation only handles objects and arrays of object, so more complex schemas fail with errors like:
Summary
With heavy inspiration from @Jeto143's comment on #11369, this change adds support for schemas with:
"type": ["object", "null"]anyOfe.g., fromz.union()allOfe.g., fromz.intersection()oneOfNote, multiple types and
oneOfare not currently produced by zod, but I figured it made sense to future proof.Manual Verification
Running the script below from
packages/aiwill fail with the error above on main, but runs successfully with this change.Checklist
pnpm changesetin the project root)Related Issues
Fixes #11369