fix: drop unresolved union members in zod schemas #150
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
This change enables the
strictUnions
option in zod-to-json-schema so that any union members that cannot be resolved are excluded from the resulting JSON Schema anyOf array.Previously, a schema like this:
Before
Prints:
After
Prints:
I picked this example in particular because I have modelled file uploads using a permissive schema that accepts streams, byte arrays / blobs and base64-encoded strings. In the context of MCP, base64 would be most applicable so I wanted to drop the other union members that couldn't be represented in JSON Schema.
How Has This Been Tested?
Not tested in a real world application yet. I was mostly observing how
zod-to-json-schema
converted various schemas to JSON Schema and noticed the odd behavior described above.Breaking Changes
I'm not sure if this is a breaking change. It's possible that some schemas that were serializing to
{}
were interpreted in some interesting way by a language model but I could not assert if that was a meaningful interpretation.Types of changes
Checklist
Additional context