fix: resolve $ref pointers from recursive Zod schemas#1054
Open
4444J99 wants to merge 1 commit intocoinbase:mainfrom
Open
fix: resolve $ref pointers from recursive Zod schemas#10544444J99 wants to merge 1 commit intocoinbase:mainfrom
4444J99 wants to merge 1 commit intocoinbase:mainfrom
Conversation
zodToJsonSchema() and z.toJSONSchema() emit $ref pointers for recursive (z.lazy) and shared Zod types. OpenAI's function-calling API rejects schemas containing $ref with "object schema missing properties". Add resolveJsonSchemaRefs() utility that inlines $ref definitions up to a configurable depth. Integrate it into the MCP framework extension where AgentKit controls the JSON Schema conversion. Export from @coinbase/agentkit for LangChain and Vercel AI SDK users. - 10 unit tests for the core utility - 2 tests for MCP extension integration (including shared sub-schema case) - Lint and format clean Closes coinbase#815
🟡 Heimdall Review Status
|
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.
Description
Fixes #815
zodToJsonSchema()andz.toJSONSchema()emit$refpointers when a Zod type is referenced more than once (shared sub-schemas) or usesz.lazy()for recursion. OpenAI's function-calling API rejects schemas containing$refwithBadRequestError: 400 Invalid schema ... object schema missing properties.This PR adds a
resolveJsonSchemaRefs()utility that inlines$refdefinitions up to a configurable depth (default: 5), replacing deeper levels with a permissive empty schema ({}), and strips the$defs/definitionsblock from the output.Integration approach:
getMcpTools()where AgentKit controls thez.toJSONSchema()conversion, so ref resolution happens transparently for all MCP users.tool()functions, which handle JSON Schema conversion internally. The utility is exported from@coinbase/agentkitso users of these extensions can apply it to their ownzodToJsonSchema()output when needed.Usage for LangChain/Vercel AI SDK users with recursive schemas:
Tests
10 unit tests for the core utility covering:
$reffrom$defsanddefinitions$refpaths, null values2 integration tests for the MCP extension:
$refor$defsin outputLint and format checks pass.
Checklist