Skip to content

[Fix] kimi-k3: Resolve strict tool $refs into the root parameters schema - #48357

Merged
hnyls2002 merged 2 commits into
mainfrom
lsyin/kimi-k3-root-refs
Nov 4, 2026
Merged

hnyls2002 merged 2 commits into
mainfrom
lsyin/kimi-k3-root-refs

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

A Kimi-K3 request with a strict tool fails with HTTP 400 when the tool's parameters use a $ref that points into the root structure instead of into $defs:

{
  "type": "object",
  "required": ["value"],
  "properties": {
    "value": {
      "type": "object",
      "properties": {
        "a": {"type": "string"},
        "b": {"$ref": "#/properties/value/properties/a"}
      }
    }
  }
}
Failed to compile structural_tag grammar: ... json_schema_converter.cc: Cannot find field value in #/properties/value/properties/a

The Kimi-K3 tool-call format emits each argument separately, so the structural tag compiles each argument value as its own JSON schema. A ref such as #/properties/value/... is then resolved against the argument schema, which no longer has that path. Refs into $defs / definitions are unaffected, because every argument schema already carries the root's definition pools.

Modifications

All changes are in function_call/kimik3_structural_tag.py:

  • _is_root_structure_ref tells a local ref into the root structure apart from a ref into $defs / definitions.
  • _localize_root_refs copies every root-structure ref target into the argument schema's $defs under a fresh root_ref_<n> name and rewrites the ref to point there.
    • Root-structure refs inside a copied target are copied the same way, so ref chains and recursive schemas resolve.
    • Each ref is copied once.
    • Refs that do not resolve are left in place for the compiler to report.
  • _argument_value_variants applies it to each type-restricted argument schema before compiling it. A schema without root-structure refs is returned unchanged.

Tests

test_kimik3_structural_tag.py gains two tests:

  • test_strict_schema_resolves_refs_into_the_root_structure covers three kinds of ref:
    • a ref to a sibling property,
    • a ref to an array's items,
    • a chain that mixes $defs and root-structure refs.
  • test_strict_schema_resolves_recursive_root_structure_refs covers an items ref back to the argument itself.

Each test checks that a valid value is accepted and a wrongly typed one is rejected. Both tests fail on main and pass here with the pinned xgrammar 0.2.7. The rest of test/registered/unit/function_call/ gives the same results before and after the change.


CI States

Latest PR Test (Base): 🚫 Run #41800920928
Latest PR Test (Extra): ❌ Run #41800920680
Latest PR Test (AMD ROCm 10): 🚫 Run #41800920710

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

The Kimi-K3 structural tag compiles each argument value as its own JSON
schema. A $ref into the root structure, such as
"#/properties/value/properties/a", is then looked up in the argument schema,
which no longer has that path, and the grammar fails to compile, so the
request gets a 400.

Before compiling an argument, copy every root-structure ref target (and,
transitively, the root-structure refs inside it) under the argument's $defs
and rewrite the ref to it. Refs into $defs/definitions already resolve,
since every argument schema carries the root's pools, and are left alone.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_kimik3_structural_tag.py test_kimik3_detector.py test_function_call_parser.py test_serving_chat.py

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (4 tests): ✅ View workflow run

cd test/ && python3 registered/unit/function_call/test_kimik3_structural_tag.py
cd test/ && python3 registered/function_call/test_kimik3_detector.py
cd test/ && python3 registered/unit/function_call/test_function_call_parser.py
cd test/ && python3 registered/unit/entrypoints/openai/test_serving_chat.py

@hnyls2002
hnyls2002 merged commit a16830d into main Nov 4, 2026
192 of 231 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/kimi-k3-root-refs branch November 4, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant