fix(zod): pass forceResolution through parseBrandedDef to prevent circular $ref#1796
Open
s-zx wants to merge 1 commit intoopenai:masterfrom
Open
fix(zod): pass forceResolution through parseBrandedDef to prevent circular $ref#1796s-zx wants to merge 1 commit intoopenai:masterfrom
s-zx wants to merge 1 commit intoopenai:masterfrom
Conversation
…cular $ref When a branded Zod type is reused across multiple fields, zodTextFormat/zodResponseFormat generates a JSON schema definition that references itself because parseBrandedDef does not forward forceResolution to parseDef. On the second encounter the inner def is already in refs.seen, so parseDef emits a $ref pointing back to the definition being built — an unresolvable cycle. Mirror the existing pattern in parseEffectsDef: accept forceResolution as a third parameter and forward it to parseDef. Closes openai#1739
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
zodTextFormat/zodResponseFormatproduce a self-referencing$refin the definitions block when az.string().brand<...>()type is reused across multiple schema fields. The resulting JSON schema is unresolvable and causes a crypticSyntaxError: Unexpected end of JSON inputfromresponses.parse().Root Cause
parseBrandedDefin the vendored zod-to-json-schema code does not accept or forward theforceResolutionparameter when callingparseDef. On the second encounter of the branded type's inner def (already inrefs.seen),parseDefreturns a$refpointing back to the definition currently being built — a cycle.parseEffectsDefalready handles this correctly by accepting and forwardingforceResolution.Fix
forceResolution: booleanparameter toparseBrandedDefparseDef(_def.type._def, refs, forceResolution)selectParsercall site to passforceResolutionthroughTest plan
$refCloses #1739