You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: Kernel JSON Schema Builder support for Forward Refs #6464
The current Kernel JSON Schema Builder is missing support to properly handle Forward References when building a schema. This is marked as a TODO in code and is untested.
Picking this back up — it was auto-closed as stale but the gap is still present on main today.
I dug into the exact failure mode: plain (non-cyclic) forward references already resolve correctly in KernelJsonSchemaBuilder.build_model_schema today. The real bug is that a self-referential or mutually-recursive KernelBaseModel (e.g. a tree node, linked list, or two models that reference each other) causes KernelJsonSchemaBuilder.build() to recurse infinitely and crash with RecursionError: maximum recursion depth exceeded, rather than a NameError on the unresolved name.
I have a fix ready that detects the cycle and emits a standard JSON Schema $defs/$ref pair instead of recursing — the same mechanism Pydantic's own model_json_schema() uses for recursive models. It only activates when a cycle is actually present, so today's fully-inlined schema output for non-recursive models is unchanged. Added test coverage for the simple forward-ref case, self-reference, a mutual two-model cycle, and the structured_output=True path — none of which were covered before. Opening a PR shortly.
Activity
github-actions commentedon Aug 30, 2024
This issue is stale because it has been open for 90 days with no activity.
grvnmttl commentedon Jul 26, 2026
Picking this back up — it was auto-closed as stale but the gap is still present on
maintoday.I dug into the exact failure mode: plain (non-cyclic) forward references already resolve correctly in
KernelJsonSchemaBuilder.build_model_schematoday. The real bug is that a self-referential or mutually-recursiveKernelBaseModel(e.g. a tree node, linked list, or two models that reference each other) causesKernelJsonSchemaBuilder.build()to recurse infinitely and crash withRecursionError: maximum recursion depth exceeded, rather than aNameErroron the unresolved name.I have a fix ready that detects the cycle and emits a standard JSON Schema
$defs/$refpair instead of recursing — the same mechanism Pydantic's ownmodel_json_schema()uses for recursive models. It only activates when a cycle is actually present, so today's fully-inlined schema output for non-recursive models is unchanged. Added test coverage for the simple forward-ref case, self-reference, a mutual two-model cycle, and thestructured_output=Truepath — none of which were covered before. Opening a PR shortly.