Skip to content

[Feat] kimi-k3: Stream tool-call arguments incrementally - #48775

Merged
hnyls2002 merged 4 commits into
mainfrom
lsyin/k3-tool-call-parsing
Nov 10, 2026
Merged

hnyls2002 merged 4 commits into
mainfrom
lsyin/k3-tool-call-parsing

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Port of #48414

Takes the Kimi-K3 tool-call streaming changes from #48414:

  • From function_call/kimik3_detector.py, the incremental streaming, with its tests.
  • ToolCallItem.source_text in core_types.py, the XTML each streamed delta came from. It is excluded from serialization; [Feat] kimi-k3: Shape chat responses to the K3 wire format #48777's K3 token provenance reads it.
  • From serving_chat.py, only the skip of incomplete calls in _check_for_unstreamed_tool_args.

Left on the branch:

  • The forced-call grammar flag response_channel_open. Main's grammar already lets the output start inside the open response channel, and it still requires the tools section after that channel closes.
  • The $dynamicRef strip before check_schema. The metaschema gives $dynamicRef the same format-only rule as $ref.
  • The non-ASCII property-name fallback in kimik3_structural_tag.py. With xgrammar 0.2.7, main's strict grammar accepts a raw UTF-8 property name and rejects only its \u-escaped spelling, which constrained decoding never emits. The fallback dropped the whole argument to its bare type, so a wrong key, a wrong type, or a missing required property passed.

Summary

  • Incremental streaming. KimiK3Detector.parse_streaming_increment converts XTML to JSON as it arrives. A string argument streams character by character, so it no longer waits for <|close|>call<|sep|>. A non-string argument is buffered until its close marker, which keeps the non-streaming decoder's string fallback for invalid JSON. A call with no tool name is skipped whole, as the non-streaming decoder does. Each ToolCallItem carries its XTML source_text.
  • Incomplete calls. A call whose close marker has not arrived is tracked in incomplete_tool_call_indices. _check_for_unstreamed_tool_args now skips such a call at stream end instead of synthesizing its remaining arguments.

Changes to existing code

  • Detectors that track incomplete_tool_call_indices (the response-template-driven detectors on main, besides K3): at stream end, a call that never closed is no longer completed with synthesized arguments. /v1/responses already skips such calls.
  • No other change for other models.

Verification

Unit tests, run locally (macOS, CPU, xgrammar 0.2.7 as pinned):

cd test
python -m pytest -q registered/function_call/test_kimik3_detector.py
python -m pytest -q registered/unit/entrypoints/openai/test_serving_chat.py
python -m pytest -q registered/unit/function_call registered/function_call
  • K3 detector tests: 22 passed. On main: 16 passed.
  • test_serving_chat.py: 181 passed and 1 failed. On main: 180 passed and the same 1 failed (test_streaming_created_is_stable_across_chunks), because tml-renderers is not installed locally.
  • All function_call tests: 753 passed and 4 failed. On main: 747 passed and the same 4 failed (test_deepseekv41_detector.py).
  • Streaming against non-streaming: 3,000 random tool-call texts, fed in random chunks, decode to the same calls and arguments as detect_and_parse. Every truncation point of a two-call stream leaves each closed call's arguments as valid JSON.
  • pre-commit run passes on the changed files.

CI States

Latest PR Test (Base): 🚫 Run #42781532883
Latest PR Test (Extra): ❌ Run #42781532801
Latest PR Test (AMD ROCm 10): ⏳ Run #42781533054

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 hnyls2002 changed the title [Feat] kimi-k3: Stream tool-call arguments incrementally and fix forced-call grammars [Feat] kimi-k3: Stream tool-call arguments incrementally Nov 10, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_kimik3_detector.py test_serving_chat.py

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/function_call/test_kimik3_detector.py  # base-a
cd test/ && python3 registered/unit/entrypoints/openai/test_serving_chat.py  # base-a

@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 10, 2026
@hnyls2002
hnyls2002 merged commit d50f98e into main Nov 10, 2026
189 of 232 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/k3-tool-call-parsing branch November 10, 2026 08:47
kpham-sgl added a commit that referenced this pull request Nov 10, 2026
ihciah added a commit to ihciah/sglang that referenced this pull request Nov 30, 2026
Port sgl-project#48775 to sglang-processor. SGLang now streams a Kimi-K3 call's name
and `{` once its header is complete, string values as they arrive and
typed values once they close; the port still sent each call whole once
`<|close|>call<|sep|>` arrived, so 28 of the 46 tool-parity cases had
drifted. `kimi_k3.rs` streams through a port of `_stream_calls`,
including skipping malformed XTML, and headers a call or tools close
cuts short, as the one-shot parse does.

A call the stream leaves open is incomplete, and a stop then does not
finish as `tool_calls`: `ToolDetector::has_incomplete_call` carries that
into `openai/chat.rs` and `parser/tools.rs`, as `serving_chat`'s
`_incomplete_tool_call_indices` does.

Regenerate `tool_parity/kimik3.json` and add malformed, cut-header and
split-value inputs. In `openai_parity/kimi-k3.json`, update the two streamed tool
cases and add `tools_stream_unclosed`, generated with Kimi-K3 on the
OpenAI wire and its pre-sgl-project#45972 ids, which this path still writes.
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