Skip to content

Python: Kernel JSON Schema Builder support for Forward Refs #6464

Closed

Description

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.

https://github.com/microsoft/semantic-kernel/blob/fbb26b5cf6a71d0cbfaa9c9e9a53997d2e34cbfb/python/semantic_kernel/schema/kernel_json_schema_builder.py#L55C9-L55C27

Activity

  1. added
    pythonPull requests for the Python Semantic Kernel
    kernelIssues or pull requests impacting the core kernel
    on May 31, 2024
  2. github-actions commented on Aug 30, 2024

    @github-actions
    Contributor

    This issue is stale because it has been open for 90 days with no activity.

  3. added
    staleIssue is stale because it has been open for a while and has no activity
    on Aug 30, 2024
  4. grvnmttl commented on Jul 26, 2026

    @grvnmttl
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

kernelIssues or pull requests impacting the core kernelpythonPull requests for the Python Semantic KernelstaleIssue is stale because it has been open for a while and has no activity

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions