Repository navigation
[Fix] kimi-k3: Make tool_choice required and none grammars end the turn - #48480
Merged
Merged
Conversation
…nel; fold the none grammar into get_kimik3_structural_tag
There was a problem hiding this comment.
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.
Collaborator
Author
|
/tag-and-rerun-ci |
Collaborator
Author
|
/rerun-test test_openai_function_calling.py test_openai_server.py test_constrained_decoding_spec_reasoning.py test_kimi_k3_b300.py |
Contributor
|
🚀 🚀 🚀 🚀 |
4 of 5 tasks
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.
This was referenced Nov 13, 2026
4 of 5 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
With the Kimi-K3 structural-tag grammar,
tool_choicedid not hold:requiredcould 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 untilmax_tokens.nonestill allowed a tools section. The Kimi-K3 prompt lists the tools even fornone, and tool-call parsing is off, so raw call markup reachedcontent.<|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: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 nowget_kimik3_structural_tag(tool_choice="none"), next toauto,requiredand a named function.autogrammar admits the message close after the tools section.serving_chat.pyinstalls thenonegrammar for Kimi-K3 requests that send tools withtool_choice: "none". Aresponse_formaton 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 forrequired;nonerejects a tools section and an empty response; single-callautoadmits the message close; output that starts inside the prefilled response is accepted forrequiredandnone.test_serving_chat.py: a Kimi-K3 request with tools andtool_choice: "none"gets thenonegrammar.test_kimik3_structural_tag.pyand the Kimi-K3 / tool-choice tests intest_serving_chat.pypass 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 withtool_choice: "required", 30 times, at the server's default sampling, and counts responses that end in a tool call: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 tomax_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-testto run the tests your change touches, or copy this prompt for an agent: