Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/tool_parsers/test_kimi_k2_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def test_extract_tool_calls(
# id format: "something:digit"
assert tc.id.split(":")[-1].isdigit()

def test_invalid_json_still_extracted(self, parser):
"""Tool calls with invalid JSON are still returned (arguments as-is)."""
def test_invalid_json_skipped(self, parser):
"""Tool calls with invalid JSON are skipped; valid ones kept."""
model_output = (
"Help. "
+ SECTION_BEGIN
Expand All @@ -158,9 +158,9 @@ def test_invalid_json_still_extracted(self, parser):
+ SECTION_END
)
content, tool_calls = run_tool_extraction(parser, model_output, streaming=False)
assert len(tool_calls) == 2
assert tool_calls[0].function.name == "bad"
assert tool_calls[1].function.name == "good"
assert len(tool_calls) == 1
assert tool_calls[0].function.name == "good"
assert json.loads(tool_calls[0].function.arguments) == {"city": "Shanghai"}

def test_invalid_funcall_id_skipped(self, parser):
"""Tool calls with malformed id (no colon+digit) are skipped."""
Expand Down
10 changes: 10 additions & 0 deletions vllm/tool_parsers/kimi_k2_tool_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import json
from collections.abc import Sequence

import regex as re
Expand Down Expand Up @@ -96,6 +97,15 @@ def extract_tool_calls(
function_id, function_args = match
# function_id: functions.get_weather:0 or get_weather:0
function_name = function_id.split(":")[0].split(".")[-1]
try:
json.loads(function_args)
except (json.JSONDecodeError, TypeError):
logger.warning(
"Skipping tool call '%s' with invalid JSON: %s",
function_name,
function_args,
)
continue
Comment on lines +100 to +108

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

While validating that the arguments are valid JSON is a good first step, tool call arguments are strictly expected to be a JSON object (dictionary) according to the OpenAI-compatible protocol. If the model generates a valid JSON primitive (like a string, number, or null) instead of an object, downstream clients expecting a dictionary will likely crash when attempting to access parameters. Consider adding a check to ensure the parsed JSON is a dictionary.

                    try:
                        if not isinstance(json.loads(function_args), dict):
                            raise ValueError("Tool call arguments must be a JSON object")
                    except (json.JSONDecodeError, TypeError, ValueError):
                        logger.warning(
                            "Skipping tool call '%s' with invalid JSON: %s",
                            function_name,
                            function_args,
                        )
                        continue

tool_calls.append(
ToolCall(
id=function_id,
Expand Down
Loading