Skip to content

Commit 2de35a1

Browse files
author
wangln19
committed
Fix a robust parsing issue in KimiK2ToolParser that causes IndexError when function_id format doesn't match the hardcoded assumption. Signed-off-by: wangln19 <[email protected]>
Signed-off-by: wangln19 <[email protected]>
1 parent 7c2bdb8 commit 2de35a1

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

tests/tool_use/test_kimi_k2_tool_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def assert_tool_calls(
3737
assert actual_tool_call.type == "function"
3838
assert actual_tool_call.function == expected_tool_call.function
3939

40-
# assert tool call id format
41-
assert actual_tool_call.id.startswith("functions.")
40+
# assert tool call id format: should contain function name and numeric index
41+
# Format can be either "functions.func_name:0" or "func_name:0"
4242
assert actual_tool_call.id.split(":")[-1].isdigit()
4343
assert (
44-
actual_tool_call.id.split(".")[1].split(":")[0]
44+
actual_tool_call.id.split(":")[0].split(".")[-1]
4545
== expected_tool_call.function.name
4646
)
4747

vllm/entrypoints/openai/tool_parsers/kimi_k2_tool_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def extract_tool_calls(
9696
tool_calls = []
9797
for match in function_call_tuples:
9898
function_id, function_args = match
99-
# function_id: functions.get_weather:0
100-
function_name = function_id.split(".")[1].split(":")[0]
99+
# function_id: functions.get_weather:0 or get_weather:0
100+
function_name = function_id.split(":")[0].split(".")[-1]
101101
tool_calls.append(
102102
ToolCall(
103103
id=function_id,
@@ -254,7 +254,7 @@ def extract_tool_calls_streaming(
254254
)
255255
if current_tool_call_matches:
256256
tool_id, tool_args = current_tool_call_matches.groups()
257-
tool_name = tool_id.split(".")[1].split(":")[0]
257+
tool_name = tool_id.split(":")[0].split(".")[-1]
258258
current_tool_call["id"] = tool_id
259259
current_tool_call["name"] = tool_name
260260
current_tool_call["arguments"] = tool_args
@@ -264,7 +264,7 @@ def extract_tool_calls_streaming(
264264
)
265265
if current_tool_call_name_matches:
266266
(tool_id_str,) = current_tool_call_name_matches.groups()
267-
tool_name = tool_id_str.split(".")[1].split(":")[0]
267+
tool_name = tool_id_str.split(":")[0].split(".")[-1]
268268
current_tool_call["id"] = tool_id_str
269269
current_tool_call["name"] = tool_name
270270
current_tool_call["arguments"] = ""

0 commit comments

Comments
 (0)