Skip to content

fix kimi k2 tool parser issue#26918

Draft
luojiaxuan wants to merge 8 commits intovllm-project:mainfrom
luojiaxuan:feat/kimi-k2-parser-gavin
Draft

fix kimi k2 tool parser issue#26918
luojiaxuan wants to merge 8 commits intovllm-project:mainfrom
luojiaxuan:feat/kimi-k2-parser-gavin

Conversation

@luojiaxuan
Copy link
Copy Markdown

@luojiaxuan luojiaxuan commented Oct 15, 2025

Purpose

Test Plan

Test Result


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

@github-actions
Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors.

You ask your reviewers to trigger select CI tests on top of fastcheck CI.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

🚀

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant changes to the KimiK2ToolParser to improve its robustness in handling tool calls, especially for malformed JSON and non-standard token sequences from the Kimi K2 model. A new normalize_quotes_for_json function is added to handle complex string cleaning, and the logic for both streaming and non-streaming tool call extraction has been heavily modified. While the changes seem to address the core issue, there are several critical and high-severity issues related to code quality and maintainability, such as the use of bare except clauses, local imports, and leftover commented-out code and merge markers. These should be addressed to ensure the code is clean, correct, and maintainable.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

luojiaxuan and others added 7 commits October 15, 2025 11:51
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Jiaxuan Luo <jluo50@jhu.edu>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Jiaxuan Luo <jluo50@jhu.edu>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Jiaxuan Luo <jluo50@jhu.edu>
@luojiaxuan luojiaxuan marked this pull request as draft October 15, 2025 16:22
saifmb0 added a commit to saifmb0/vllm that referenced this pull request Mar 28, 2026
…ewline in tool call ID (vllm-project#38441)

The model occasionally emits a stray \n between <|tool_call_begin|>
and the function name, e.g.:

    <|tool_call_begin|>
    functions.edit:15<|tool_call_argument_begin|>{...}

Because Python regex does not match \n with . by default, both
stream_tool_call_portion_regex and stream_tool_call_name_regex
silently failed to match, causing the entire tool call to be dropped
during streaming.

Fix:
- Add a leading \s* to both streaming regexes so any leading
  whitespace/newlines before the tool_call_id are consumed.
- Compile both regexes with re.DOTALL so . inside the capture group
  spans newlines.

This is distinct from PR vllm-project#37384 which only adds re.DOTALL (without
leading \s*) to the portion regex and does not fix stream_tool_call_name_regex.

Tests added:
- test_stream_tool_call_portion_regex_handles_leading_newline: unit
  test that both regexes match inputs with a leading \n.
- test_streaming_tool_call_with_newline_after_begin_token: end-to-end
  streaming simulation reproducing the exact scenario in the issue.

Why this is not a duplicate: checked open PRs vllm-project#37384, vllm-project#37445, vllm-project#32504,
vllm-project#24847, vllm-project#26918, vllm-project#36891. None add the leading \s* prefix to handle
whitespace/newlines preceding the tool_call_id capture group, and none
fix stream_tool_call_name_regex with re.DOTALL.

Co-authored-by: GitHub Copilot
saifmb0 added a commit to saifmb0/vllm that referenced this pull request Mar 28, 2026
…ewline in tool call ID (vllm-project#38441)

The model occasionally emits a stray \n between <|tool_call_begin|>
and the function name, e.g.:

    <|tool_call_begin|>
    functions.edit:15<|tool_call_argument_begin|>{...}

Because Python regex does not match \n with . by default, both
stream_tool_call_portion_regex and stream_tool_call_name_regex
silently failed to match, causing the entire tool call to be dropped
during streaming.

Fix:
- Add a leading \s* to both streaming regexes so any leading
  whitespace/newlines before the tool_call_id are consumed.
- Compile both regexes with re.DOTALL so . inside the capture group
  spans newlines.

This is distinct from PR vllm-project#37384 which only adds re.DOTALL (without
leading \s*) to the portion regex and does not fix stream_tool_call_name_regex.

Tests added:
- test_stream_tool_call_portion_regex_handles_leading_newline: unit
  test that both regexes match inputs with a leading \n.
- test_streaming_tool_call_with_newline_after_begin_token: end-to-end
  streaming simulation reproducing the exact scenario in the issue.

Why this is not a duplicate: checked open PRs vllm-project#37384, vllm-project#37445, vllm-project#32504,
vllm-project#24847, vllm-project#26918, vllm-project#36891. None add the leading \s* prefix to handle
whitespace/newlines preceding the tool_call_id capture group, and none
fix stream_tool_call_name_regex with re.DOTALL.

Co-authored-by: GitHub Copilot
saifmb0 added a commit to saifmb0/vllm that referenced this pull request Mar 28, 2026
…ewline in tool call ID (vllm-project#38441)

The model occasionally emits a stray \n between <|tool_call_begin|>
and the function name, e.g.:

    <|tool_call_begin|>
    functions.edit:15<|tool_call_argument_begin|>{...}

Because Python regex does not match \n with . by default, both
stream_tool_call_portion_regex and stream_tool_call_name_regex
silently failed to match, causing the entire tool call to be dropped
during streaming.

Fix:
- Add a leading \s* to both streaming regexes so any leading
  whitespace/newlines before the tool_call_id are consumed.
- Compile both regexes with re.DOTALL so . inside the capture group
  spans newlines.

This is distinct from PR vllm-project#37384 which only adds re.DOTALL (without
leading \s*) to the portion regex and does not fix stream_tool_call_name_regex.

Tests added:
- test_stream_tool_call_portion_regex_handles_leading_newline: unit
  test that both regexes match inputs with a leading \n.
- test_streaming_tool_call_with_newline_after_begin_token: end-to-end
  streaming simulation reproducing the exact scenario in the issue.

Why this is not a duplicate: checked open PRs vllm-project#37384, vllm-project#37445, vllm-project#32504,
vllm-project#24847, vllm-project#26918, vllm-project#36891. None add the leading \s* prefix to handle
whitespace/newlines preceding the tool_call_id capture group, and none
fix stream_tool_call_name_regex with re.DOTALL.

Co-authored-by: GitHub Copilot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant