Skip to content

[Fix] kimi-k3: Validate message-level tools and let a forced call override JSON response_format - #48481

Merged
hnyls2002 merged 3 commits into
mainfrom
lsyin/kimi-k3-request-checks
Nov 5, 2026
Merged

hnyls2002 merged 3 commits into
mainfrom
lsyin/kimi-k3-request-checks

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Two Kimi-K3 request shapes are handled differently from the Kimi-K3 API:

  1. Message-level tools are not validated. Kimi-K3 lets a system message declare tools. The server accepts tools on any message and never checks them, so a request that the encoding cannot render (tools on a user or assistant message, tools next to content, a non-function tool type, a tool name the format does not allow) is served instead of rejected.
  2. A forced tool call with a JSON response_format is rejected. For every model, tool_choice: "required" or a named function plus response_format returns 400, because both constraints cannot be honored. The Kimi-K3 encoding renders response_format into the prompt, so the forced call can take the grammar and the request can be served.

Both changes apply only to the Kimi-K3 encoding (chat_encoding_spec == "kimi_k3"). Other models and encodings are unchanged.

Modifications

  • serving_chat.py: _validate_kimi_k3_message_tools, called from _validate_request for the Kimi-K3 encoding. A message that declares tools must be a system or developer message without content, each tool must have type: "function", and each name must match [A-Za-z_][A-Za-z0-9_-]{0,255}. A violation returns 400 through the existing error path. Missing fields, duplicate names and tool messages without tool_call_id were already rejected and are unchanged.
  • protocol.py:
    • ChatCompletionRequest records which raw messages carried a non-empty tools (_message_tools_indices), since the user-message model drops the field. It validates nothing.
    • to_sampling_params: when a forced tool call meets a json_object / json_schema response_format and the renderer states response_format in the prompt (the existing renderer_handles_response_format, true only for Kimi-K3), the tool-call constraint replaces the JSON grammar. regex, ebnf and structural_tag conflicts still return 400, and so does every other encoding.

Tests

  • test_kimi_k3_rejects_message_tools_the_encoding_cannot_render: a table of 12 requests, run with the encoding unset and with Kimi-K3. Each case is rejected only under Kimi-K3; with the encoding unset everything is accepted, including root-role message tools.
  • test_kimi_k3_forced_tool_call_overrides_json_response_format: under Kimi-K3, required + json_object and a named tool + json_schema are accepted with the tool-call grammar and no JSON grammar, and the encoder still receives response_format; a regex conflict is still 400; a non-Kimi-K3 encoding is still 400.
  • Both tests fail on main and pass here. test/registered/unit/entrypoints/openai/ gives the same results as main apart from the two new tests.

Accuracy Tests

Kimi-K3, 8x B300, TP8, with this change and the Kimi-K3 tool_choice grammar fix (#48480): a request-conformance suite for Kimi-K3 API behavior passes its message-level tool cases (10 cases, streaming and non-streaming) and the forced tool call + json_object prompt-token case, which failed on main. Prompt-token cases: 74/74. Strict tool-call schema cases: 428/428.

Speed Tests and Profiling

Request validation only.


CI States

Latest PR Test (Base): 🚫 Run #41944363877
Latest PR Test (Extra): ❌ Run #41944363781
Latest PR Test (AMD ROCm 10): 🚫 Run #41944363964

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 dac9614 into main Nov 5, 2026
208 of 273 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/kimi-k3-request-checks branch November 5, 2026 01:14
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