fix(transform schema): preserve $defs when schema root is a $ref#1642
Merged
benlehrburger-ant merged 1 commit intoJun 4, 2026
Merged
Conversation
26c79b0 to
05d4be3
Compare
transform_schema() early-returns when the schema root contains $ref,
which discards a sibling $defs and produces a schema with a dangling
reference. Pydantic v2 emits exactly this shape for RootModel types
({"$ref": "#/$defs/X", "$defs": {...}}), so any RootModel output
type hits this.
Process $defs before the $ref early-return so referenced definitions
are preserved. Bare $ref schemas (no siblings) are unchanged.
05d4be3 to
b775b2a
Compare
$defs when schema root is a $ref in transform_schema
craigie-ant
approved these changes
Jun 4, 2026
Merged
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
transform_schema()drops a top-level$defswhen the schema root is a$ref, leaving a dangling reference that the API rejects.The early return at the top of
transform_schemacopies$refand discards every sibling key — including$defs— before the$defshandling below it can run:That's fine for the recursive case (a nested property that is just
{"$ref": ...}), but wrong at the root:$refwith a sibling$defsis valid JSON Schema (draft 2019-09+), and it's exactly what Pydantic v2 emits forRootModeltypes:Any
RootModeloutput type fed through the strict-mode bindings hits this. Reported by a user; verified present from at least v0.84.0 through v0.105.2.Repro
Fix
Process
$defsbefore the$refearly return. Bare-$refschemas (no siblings) are unchanged —$defsis only emitted when present in the input.Not addressed here: other root-
$refsiblings (e.g.title) are still dropped by the early return.$defsis the one that makes the schema invalid; the rest are cosmetic.Tests
test_ref_schema_with_defscovering theRootModelshape (root$ref+ sibling$defs); the existingtest_ref_schemacovers the bare-$refcase.tests/lib/_parse/test_transform.py: 14/14 pass.