Skip to content

[Fix] kimi-k3: Make tool_choice required and none grammars end the turn - #48480

Merged
hnyls2002 merged 5 commits into
mainfrom
lsyin/kimi-k3-forced-tool-call
Nov 5, 2026
Merged

hnyls2002 merged 5 commits into
mainfrom
lsyin/kimi-k3-forced-tool-call

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

With the Kimi-K3 structural-tag grammar, tool_choice did not hold:

  • required could fail to end the turn. Kimi-K3 emits <|close|>message<|sep|> before EOS, and EOS is masked until the grammar completes. A model that wanted no tool repeated the message close until max_tokens.
  • none still allowed a tools section. The Kimi-K3 prompt lists the tools even for none, and tool-call parsing is off, so raw call markup reached content.
  • Without thinking, the grammar started in the wrong place. The generation prompt then ends with <|open|>response<|sep|>, so the output starts inside the response channel. A grammar that expects the output to open the response itself rejects the model's natural path (text, response close, tools).

This takes over #46644, whose branch cannot take maintainer pushes. Its commit is kept with its author; the second commit adds the no-thinking case on top.

Modifications

All grammar changes are in function_call/kimik3_structural_tag.py:

  • Text outside a tools section is limited to the response channel, whose body admits no control token.
  • required / named function: an optional response, then the tools section, then an optional message close. The model can no longer close the message or end the turn before the call.
  • none: a non-empty response and an optional message close, with no tools section. It is now get_kimik3_structural_tag(tool_choice="none"), next to auto, required and a named function.
  • A single-call auto grammar admits the message close after the tools section.
  • The response may start inside a channel the prompt already opened, or open it itself (after reasoning).

serving_chat.py installs the none grammar for Kimi-K3 requests that send tools with tool_choice: "none". A response_format on such a request keeps precedence, as before. Other models and encodings are unchanged.

Tests

  • test_kimik3_structural_tag.py: message close before the tools section is rejected for required; none rejects a tools section and an empty response; single-call auto admits the message close; output that starts inside the prefilled response is accepted for required and none.
  • test_serving_chat.py: a Kimi-K3 request with tools and tool_choice: "none" gets the none grammar.
  • 41 tests in test_kimik3_structural_tag.py and the Kimi-K3 / tool-choice tests in test_serving_chat.py pass with xgrammar 0.2.7.

Accuracy Tests

Kimi-K3, 8x B300, TP8, --reasoning-parser kimi_k3 --tool-call-parser kimi_k3. Each cell sends one tool with tool_choice: "required", 30 times, at the server's default sampling, and counts responses that end in a tool call:

thinking prompt main this PR
default weather question 29/30 30/30
default unrelated chat 17/30 30/30
default short weather question (zh) 26/30 30/30
off weather question 29/30 30/30
off unrelated chat 5/30 30/30
off short weather question (zh) 16/30 29/30
on weather question 30/30 30/30
on unrelated chat 17/30 30/30
on short weather question (zh) 27/30 30/30

Total: 196/270 on main, 269/270 here. On main the misses run to max_tokens (65) or stop with text and no call (9). Here the one miss sampled a low-probability first token and rambled to max_tokens; the grammar allowed it to close the response at any point.

Speed Tests and Profiling

Grammar construction only.


CI States

Latest PR Test (Base): 🚫 Run #41947265007
Latest PR Test (Extra): 🚫 Run #41947264699
Latest PR Test (AMD ROCm 10): 🚫 Run #41947264727

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

@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

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

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_openai_function_calling.py test_openai_server.py test_constrained_decoding_spec_reasoning.py test_kimi_k3_b300.py

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-h100 (1 test): ❌ View workflow run

cd test/ && python3 registered/openai_server/function_call/test_openai_function_calling.py

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/openai_server/basic/test_openai_server.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/spec/test_constrained_decoding_spec_reasoning.py

🚀 8-gpu-b300 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_kimi_k3_b300.py

@hnyls2002
hnyls2002 merged commit ebeed89 into main Nov 5, 2026
38 of 42 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/kimi-k3-forced-tool-call branch November 5, 2026 01:15
Zhangmj0621 added a commit to Infrawaves/sglang-for-sail that referenced this pull request Nov 10, 2026
Ported from Infrawaves/sglang infrawaves/prod/v0.5.22, in the form
infrawaves/dev/v0.5.23-k31 adapted them to the K3.1 parser:
- 732be4e allowed_tools in Chat Completions and Responses
- c1f3fa5 defer the allowed_tools required check past prefill handoff
- 5f887b6 grammar-only allowed_tools enforcement
- e1db688 (k31) reject history tool calls without function names
- 1facaf1 nested reasoning_tokens: in k31's shape, which keeps the
  top-level reasoning_tokens and adds completion_tokens_details only when
  there are reasoning tokens

Superseded, not ported:
- a6d2221 incremental K3 tool-argument streaming: K3.1's detector
  streams arguments itself (_stream_calls).
- 6eab4f2 required-mode close-token loop: replaced by upstream
  sgl-project#48480 (ebeed89), ported here with its tests.
- c791aa3 (k31): this base already maps custom tool choices to
  function choices in effective_tool_choice().

kimik3_detector.py, kimik3_structural_tag.py and their tests are taken
from infrawaves/dev/v0.5.23-k31, whose upstream base is identical to
this branch's K3.1 import for those files.
Zhangmj0621 added a commit to Infrawaves/sglang-for-sail that referenced this pull request Nov 11, 2026
Ported from Infrawaves/sglang infrawaves/prod/v0.5.22, in the form
infrawaves/dev/v0.5.23-k31 adapted them to the K3.1 parser:
- 732be4e allowed_tools in Chat Completions and Responses
- c1f3fa5 defer the allowed_tools required check past prefill handoff
- 5f887b6 grammar-only allowed_tools enforcement
- e1db688 (k31) reject history tool calls without function names
- 1facaf1 nested reasoning_tokens: in k31's shape, which keeps the
  top-level reasoning_tokens and adds completion_tokens_details only when
  there are reasoning tokens

Superseded, not ported:
- a6d2221 incremental K3 tool-argument streaming: K3.1's detector
  streams arguments itself (_stream_calls).
- 6eab4f2 required-mode close-token loop: replaced by upstream
  sgl-project#48480 (ebeed89), ported here with its tests.
- c791aa3 (k31): this base already maps custom tool choices to
  function choices in effective_tool_choice().

kimik3_detector.py, kimik3_structural_tag.py and their tests are taken
from infrawaves/dev/v0.5.23-k31, whose upstream base is identical to
this branch's K3.1 import for those files.
ihciah added a commit to ihciah/sglang that referenced this pull request Dec 3, 2026
…age close

SGLang sgl-project#48480 made the K3 grammars end the turn: with
`parallel_tool_calls: false` the triggered auto grammar completes with
the tools section, so `_auto_suffix` follows it with the optional
message close K3 emits before EOS. The Rust port of the auto tag
predates that and left the serial grammar bare, which the hand-written
serial fixture cases pinned instead of catching.

Wrap the serial auto tag as Python does, and regenerate the three
serial auto fixtures' `/generate` bodies from SGLang's own lowering.
ihciah added a commit to ihciah/sglang that referenced this pull request Dec 3, 2026
…age close

SGLang sgl-project#48480 made the K3 grammars end the turn: with
`parallel_tool_calls: false` the triggered auto grammar completes with
the tools section, so `_auto_suffix` follows it with the optional
message close K3 emits before EOS. The Rust port of the auto tag
predates that and left the serial grammar bare, which the hand-written
serial fixture cases pinned instead of catching.

Wrap the serial auto tag as Python does, and regenerate the three
serial auto fixtures' `/generate` bodies from SGLang's own lowering.
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.

2 participants