fix: resolve $ref pointers from recursive Zod schemas#1054
fix: resolve $ref pointers from recursive Zod schemas#10544444J99 wants to merge 3 commits 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
|
|
Hi — I noticed #1023 by @mvanhorn addresses the same The main differences in this PR:
Happy to defer to whichever PR the maintainers prefer, or to collaborate with @mvanhorn on merging the best parts of both. Just let us know which direction works. |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Hi team — just following up on this. I noticed #1023 addresses the same recursive Zod schema issue. To help with evaluation, here's how the two approaches compare:
The deeper default (5) handles real-world schemas like Let me know if there's anything I can do to help move this forward. Thanks! |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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