Skip to content

fix(zod): pass forceResolution through parseBrandedDef to prevent circular $ref#1796

Open
s-zx wants to merge 1 commit intoopenai:masterfrom
s-zx:fix/1739-zod-text-format-circular-ref
Open

fix(zod): pass forceResolution through parseBrandedDef to prevent circular $ref#1796
s-zx wants to merge 1 commit intoopenai:masterfrom
s-zx:fix/1739-zod-text-format-circular-ref

Conversation

@s-zx
Copy link
Copy Markdown

@s-zx s-zx commented Mar 26, 2026

Summary

zodTextFormat / zodResponseFormat produce a self-referencing $ref in the definitions block when a z.string().brand<...>() type is reused across multiple schema fields. The resulting JSON schema is unresolvable and causes a cryptic SyntaxError: Unexpected end of JSON input from responses.parse().

Root Cause

parseBrandedDef in the vendored zod-to-json-schema code does not accept or forward the forceResolution parameter when calling parseDef. On the second encounter of the branded type's inner def (already in refs.seen), parseDef returns a $ref pointing back to the definition currently being built — a cycle.

parseEffectsDef already handles this correctly by accepting and forwarding forceResolution.

Fix

  • Add forceResolution: boolean parameter to parseBrandedDef
  • Forward it to parseDef(_def.type._def, refs, forceResolution)
  • Update the selectParser call site to pass forceResolution through

Test plan

  • Schema with a branded type reused in multiple fields now produces valid JSON schema with inline definitions instead of self-referencing $ref
  • No change for schemas without branded types

Closes #1739

…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
@s-zx s-zx requested a review from a team as a code owner March 26, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zodTextFormat generates invalid circular $ref in JSON schema definitions

2 participants