Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.

fix(kosong): raise a clear error on circular $ref in deref_json_schema - #2506

Closed
Sreekant13 wants to merge 1 commit into
MoonshotAI:mainfrom
Sreekant13:fix/deref-circular-ref
Closed

Sreekant13 wants to merge 1 commit into
MoonshotAI:mainfrom
Sreekant13:fix/deref-circular-ref

Conversation

@Sreekant13

@Sreekant13 Sreekant13 commented Jul 18, 2026 •

Copy link
Copy Markdown

Related Issue

None. This is a small self-contained bug fix, well under the 100-line guideline in CONTRIBUTING. Happy to open an issue first if you prefer that flow.

Description

kosong.utils.jsonschema.deref_json_schema inlines every local $ref and recursively traverses the target, but it has no cycle detection. A self-referential schema, which Pydantic emits for a recursive model, therefore recursed until a RecursionError, despite the docstring promising expansion "without infinite recursion". A tool whose parameter model is recursive crashes tool conversion with a stack overflow:

from pydantic import BaseModel
from kosong.utils.jsonschema import deref_json_schema

class Node(BaseModel):
    value: str
    children: list["Node"] = []

deref_json_schema(Node.model_json_schema())  # RecursionError

This change tracks the $ref targets currently being inlined along the active chain and raises a clear ValueError naming the circular reference when a $ref points back into that chain. Schemas that reference the same definition several times in different branches are unaffected, since the tracking is scoped to the active chain.

Adds a regression test (test_deref_circular_ref_raises); it fails with RecursionError on the current code. All 13 tests in test_json_schema_deref.py and the kimi tool-conversion tests pass, and ruff is clean.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any. (No existing issue; explained above.)
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog. (Entry added to the Unreleased section by hand in the existing style; make gen-changelog drives Kimi CLI itself, which I could not run locally.)
  • I have run make gen-docs to update the user documentation. (N/A: no user-facing docs change.)

Open in Devin Review

deref_json_schema inlined every local $ref and recursively traversed the
target, but had no cycle detection. A self-referential schema (which
Pydantic emits for a recursive model, for example a tree Node with
children: list[Node]) therefore recursed until a RecursionError, despite
the docstring promising expansion "without infinite recursion".

Track the $ref targets currently being inlined along the active chain and
raise a clear ValueError when a $ref points back into that chain. Schemas
that reference a definition several times in different branches are
unaffected, since the tracking is scoped to the active chain. Adds a
regression test.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@Sreekant13

Copy link
Copy Markdown
Author

Closing this, as the same issue was fixed in another PR. Thanks all the same.

@Sreekant13 Sreekant13 closed this Aug 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant