Skip to content

[Kimi K3] Add reasoning, tool-call, and OpenAI serving support - #33025

Merged
hnyls2002 merged 22 commits into
mainfrom
xinyuan/kimi-k3-parsers
Aug 1, 2026
Merged

hnyls2002 merged 22 commits into
mainfrom
xinyuan/kimi-k3-parsers

Conversation

@JustinTong0323

@JustinTong0323 JustinTong0323 commented Jul 31, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • add Kimi K3 XTML reasoning and native tool-call parsers
  • add XGrammar structural constraints for automatic, required, and named tool choice
  • auto-detect both parsers from Kimi K3 model configuration
  • support Kimi K3 rendering in Chat Completions and Responses, including dynamic message tools, wire-format tool and response schemas, multimodal prompt IDs, and native tool-call IDs
  • preserve the reasoning state resolved during template processing across Chat and Responses generation, streaming, and background parsing
  • gate post-reasoning grammar and reasoning-token accounting on the complete multi-token think-end sequence

Motivation

This extracts the parser and directly related OpenAI serving work from #32541 so the model-support PR has a smaller review surface. The parser, constraint, auto-detection, Chat Completions, and Responses pieces are presented as one pull request because they share the same Kimi K3 wire format and need end-to-end validation together.

The serving changes also include #33003: literal <|kimi_image_placeholder|> text is neutralized in both string and list-form content so user text cannot be interpreted as an image slot.

Details

  • propagate normalized require_reasoning through Chat and Responses generation, builtin-tool continuations, streaming, and background requests
  • pass Responses tools, tool_choice, and reasoning effort through the Kimi K3 Chat encoder
  • accept system/developer message-level tools across all chat encodings, include them in validation and parsing, reject duplicate names across declaration sites, and default their tool_choice to auto
  • preserve client wire dictionaries for request and message tools and response_format
  • allow non-strict JSON response formats to reach the model renderer without installing a strict sampling constraint
  • preserve trailing assistant history and malformed historical arguments for Kimi K3 rendering
  • support parallel_tool_calls, strict per-tool schemas, and SGLANG_TOOL_STRICT_LEVEL
  • preserve Kimi K3's native per-argument encoding while enforcing JSON Schema constraints
  • prevent argument content from consuming parser-visible XTML close delimiters
  • let model-native parsers handle required tool choice without the incompatible JSON-only fallback, and keep Kimi K3 output unconstrained in its native format when structural-constraint construction fails instead of forcing a JSON-only schema the parser cannot read back
  • track partial and overlapping multi-token reasoning end markers across decode chunks and grammar rollback
  • remove comments and docstrings that only restated adjacent code or fields

Validation

  • 200 passed across protocol, Chat Completions, Responses, reasoner grammar, grammar backend, and reasoning-token accounting tests with the repository-pinned openai==2.6.1; one environment-gated grammar-manager module skipped
  • 35 passed in focused Kimi K3 structural-tag tests
  • 436 passed, 14 subtests passed across registered unit function-call tests
  • 73 passed, 7 subtests passed across registered function-call tests
  • message-level tool validation now covered for the default, dsv4, dsv32, and kimi_k3 encodings via subtests in the serving-chat suite
  • Python 3.10.20 round-trip validation passed for every SGLANG_TOOL_STRICT_LEVEL value
  • pre-commit passed for the parser and serving change set and the follow-up Python 3.10 test fix; git diff --check passed

Commit structure

  1. Add Kimi K3 reasoning parser
  2. Add Kimi K3 tool-call parser
  3. Add Kimi K3 structural tool constraints
  4. Detect Kimi K3 parsers automatically
  5. Fix Responses reasoning state propagation
  6. Add Kimi K3 OpenAI serving support
  7. Support multi-token reasoning end markers
  8. Fix tool strict tests on Python 3.10
  9. Fix message-level tools across chat encodings
  10. Keep native tool-call format when constraints fail ([Kimi K3] Keep native tool-call format when constraints fail #33192)

Supersedes #32768 and the temporary stacked drafts #33017, #33018, #33019, and #33020.


CI States

Latest PR Test (Base): ✅ Run #30693526825
Latest PR Test (Extra): ✅ Run #30693526820

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@JustinTong0323
JustinTong0323 marked this pull request as ready for review July 31, 2026 06:44
@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jul 31, 2026
@JustinTong0323 JustinTong0323 changed the title [Kimi K3] Add reasoning and tool-call parsers [Kimi K3] Add reasoning, tool-call, and OpenAI serving support Jul 31, 2026
@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

# Conflicts:
#	python/sglang/srt/entrypoints/openai/serving_chat.py
#	python/sglang/srt/managers/scheduler.py
@tancheng33

Copy link
Copy Markdown
Contributor

Heads-up: #32960 (literal <|kimi_image_placeholder|> in message text → 400 even with no image attached) traces to this branch's serving_chat.py:

template_kwargs["image_prompts"] = ["<|media_pad|>"] * image_count

With image_count == 0 this passes [], which puts the upstream encoding_k3.py renderer into scan-and-consume mode (immediate ValueError), whereas image_prompts=None makes it render the literal as inert plain text. Guarding with if image_count: fixes the text-only case — full analysis in #32960 (comment). Happy to send the patch + regression test here if useful.

@hnyls2002

Copy link
Copy Markdown
Collaborator

@tancheng33 This is fixed through #33003, and the fix is landed in this PR.

@tancheng33

Copy link
Copy Markdown
Contributor

@hnyls2002 Thanks — confirmed #33003's neutralization covers both message-content paths (structured text parts in _flatten_kimi_k3_content, plain-string content in _apply_jinja_template).

There's a residual path it doesn't reach, though: the upstream renderer also runs its placeholder scan (_append_text) over assistant tool_calls arguments (encoding_k3.py L440-455, both the json_block and the per-argument _xtml_value branches) and reasoning_content (L425). Neither goes through neutralize_kimi_k3_image_placeholder, and serving_chat.py still passes image_prompts=[] unconditionally for text-only requests — which is the renderer's scan-and-consume mode.

So a text-only agentic conversation still 400s when an assistant turn's tool arguments quote the literal — e.g. a coding agent whose tool args include the kimi_k3.py config source (the exact file #33003's description cites, since it defines image_placeholder: str = "<|kimi_image_placeholder|>"). Once that turn is in history, every later request in the conversation fails.

Two independent one-liners would close it:

  1. if image_count: around template_kwargs["image_prompts"] = ... — with no images the renderer gets None and renders any literal inertly (allow_special=False), fixing every text-only path including tool args and reasoning, with zero text mutation.
  2. (with-images completeness, optional) apply the existing neutralizer to tool_calls[].function.arguments / reasoning_content before templating, mirroring what fix(kimi-k3): neutralize a literal image placeholder in user text #33003 did for content.

Happy to send either as a small PR with a regression test if that helps.

@tancheng33

Copy link
Copy Markdown
Contributor

Verified on the current branch head — #33183 covers both residual paths: reasoning/nested tool-call arguments now go through the neutralizer before templating, and image_prompts is rebuilt with the if image_count: guard (plus dropping stale user-provided values, nice extra hardening). The literal-placeholder cases from #32960 all render inertly now. Thanks @JustinTong0323 @hnyls2002!

@hnyls2002 hnyls2002 added high priority run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fastfail labels Aug 1, 2026
@JustinTong0323

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@hnyls2002
hnyls2002 merged commit e2cf21b into main Aug 1, 2026
192 of 228 checks passed
@hnyls2002
hnyls2002 deleted the xinyuan/kimi-k3-parsers branch August 1, 2026 21:57
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
…roject#33025)

Co-authored-by: hnyls2002 <lsyincs@gmail.com>
Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com>
Co-authored-by: A-transformer <cl5743590921@gmail.com>
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
…roject#33025)

Co-authored-by: hnyls2002 <lsyincs@gmail.com>
Co-authored-by: Liangsheng Yin <hnyls2002@gmail.com>
Co-authored-by: A-transformer <cl5743590921@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail high priority run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants