fix: add resolveJsonSchemaRefs utility for recursive Zod schemas#1023
Open
mvanhorn wants to merge 1 commit intocoinbase:mainfrom
Open
fix: add resolveJsonSchemaRefs utility for recursive Zod schemas#1023mvanhorn wants to merge 1 commit intocoinbase:mainfrom
mvanhorn wants to merge 1 commit intocoinbase:mainfrom
Conversation
zodToJsonSchema() produces $ref pointers for recursive Zod types (z.lazy), which OpenAI and other LLM function-calling APIs reject. This utility inlines $ref references up to a configurable depth (default: 3), replacing deeper levels with a permissive empty schema. Exported from @coinbase/agentkit for use in custom integrations.
🟡 Heimdall Review Status
|
2 tasks
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
Adds a
resolveJsonSchemaRefs()utility that inlines$refpointers in JSON Schema output, fixing theBadRequestError: 400 Invalid schemawhen using recursive Zod schemas with OpenAI's function-calling API.Closes #815
Problem
zodToJsonSchema()produces$refpointers for recursive Zod types (z.lazy). OpenAI's function-calling API rejects schemas containing$refwith "object schema missing properties" (actionProvider.ts:9stores the rawz.ZodSchema, and framework extensions like LangChain pass it throughzodToJsonSchema()to build tool definitions).Solution
resolveJsonSchemaRefs(schema, maxDepth?)post-processes JSON Schema fromzodToJsonSchema():$refreferences by substituting the definition bodymaxDepth(default: 3) to handle recursive types{}(permissive empty schema)$defs/definitionsfrom outputUsage:
Tests
7 unit tests covering: simple refs, recursive refs with depth limiting,
definitionskey (not just$defs), stripping definitions from output, union types with refs, and passthrough for schemas without refs.Lint and format checks pass.
Checklist
This contribution was developed with AI assistance (Claude Code).