fix: sanitize ~standard property from Zod v4 JSON schemas#134
Merged
mattapperson merged 5 commits intomainfrom Jan 7, 2026
Merged
fix: sanitize ~standard property from Zod v4 JSON schemas#134mattapperson merged 5 commits intomainfrom
mattapperson merged 5 commits intomainfrom
Conversation
Zod v4's toJSONSchema() includes a ~standard property from the Standard Schema specification that causes 400 errors with downstream providers like Anthropic. Add sanitizeJsonSchema() function that recursively removes any keys prefixed with ~ from JSON schema objects before sending to the API. Fixes #131
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where Zod v4's toJSONSchema() method includes a ~standard property that causes 400 errors from downstream providers like Anthropic. The solution sanitizes JSON schemas by recursively removing all keys prefixed with ~ before sending requests.
Key Changes:
- Added
sanitizeJsonSchema()function that recursively removes~prefixed properties from objects and arrays - Updated
convertZodToJsonSchema()to apply sanitization before returning schemas - Added comprehensive unit and integration tests to verify the sanitization works correctly
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/lib/tool-executor.ts |
Implements sanitizeJsonSchema() function and integrates it into convertZodToJsonSchema() to remove metadata properties before sending to providers |
tests/unit/schema-sanitization.test.ts |
Adds unit tests covering edge cases for the sanitization function including nested objects, arrays, primitives, and the integration test for convertZodToJsonSchema() |
tests/e2e/callModel-tools.test.ts |
Adds integration test verifying that converted Zod schemas don't contain any ~ prefixed keys in the "Zod Schema Conversion" test suite |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
250ad6c to
cb0ea8e
Compare
Move duplicated helper function to tests/utils/schema-test-helpers.ts for consistency across unit and e2e tests.
The test for tool.preliminary_result events involves making an API call, waiting for the model to invoke the tool, executing the generator, and streaming results back. CI environments are slower than local development, so increased the timeout from 45s to 90s to prevent flaky failures.
Switch from openai/gpt-4o-mini to anthropic/claude-haiku-4.5 for more reliable tool calling behavior in CI.
|
Should this be optional opt-in / out? |
Collaborator
Author
|
@seawatts I don't think so, this is just fixing he conversion of zod to what our api needs |
louisgv
reviewed
Jan 7, 2026
louisgv
reviewed
Jan 7, 2026
louisgv
previously approved these changes
Jan 7, 2026
Member
louisgv
left a comment
There was a problem hiding this comment.
lgtm beside the null cast issue
Address PR feedback about unsafe `as Record<string, unknown>` cast that could hide null returns. Now uses: - Function overloads to preserve type information - isNonNullObject typeguard for safe type narrowing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
sanitizeJsonSchema()function that recursively removes any keys prefixed with~from JSON schema objectsconvertZodToJsonSchema()to sanitize schemas before returning~prefixed keys exist in converted schemasProblem
Zod v4's
toJSONSchema()method includes a hidden~standardproperty (from the Standard Schema specification) that:Object.keys()but doesn't appear inJSON.stringify()outputSolution
Sanitize JSON schemas by removing any keys prefixed with
~before sending requests to downstream providers.Fixes #131
Test plan
sanitizeJsonSchema()cover edge cases (root level, nested objects, arrays, primitives)convertZodToJsonSchema()produces clean schemas