Fix a robust parsing issue in KimiK2ToolParser that causes IndexError (#27565)

Signed-off-by: wangln19 <wanglinian@dev.wanglinian.msh-dev.svc.cluster.local>
Co-authored-by: wangln19 <wanglinian@dev.wanglinian.msh-dev.svc.cluster.local>
This commit is contained in:
wangln19 2025-10-28 19:11:50 +08:00 committed by GitHub
parent 0291fbf65c
commit 2fa90bda27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 7 deletions

View File

@ -37,11 +37,11 @@ def assert_tool_calls(
assert actual_tool_call.type == "function"
assert actual_tool_call.function == expected_tool_call.function
# assert tool call id format
assert actual_tool_call.id.startswith("functions.")
# assert tool call id format: should contain function name and numeric index
# Format can be either "functions.func_name:0" or "func_name:0"
assert actual_tool_call.id.split(":")[-1].isdigit()
assert (
actual_tool_call.id.split(".")[1].split(":")[0]
actual_tool_call.id.split(":")[0].split(".")[-1]
== expected_tool_call.function.name
)

View File

@ -96,8 +96,8 @@ class KimiK2ToolParser(ToolParser):
tool_calls = []
for match in function_call_tuples:
function_id, function_args = match
# function_id: functions.get_weather:0
function_name = function_id.split(".")[1].split(":")[0]
# function_id: functions.get_weather:0 or get_weather:0
function_name = function_id.split(":")[0].split(".")[-1]
tool_calls.append(
ToolCall(
id=function_id,
@ -254,7 +254,7 @@ class KimiK2ToolParser(ToolParser):
)
if current_tool_call_matches:
tool_id, tool_args = current_tool_call_matches.groups()
tool_name = tool_id.split(".")[1].split(":")[0]
tool_name = tool_id.split(":")[0].split(".")[-1]
current_tool_call["id"] = tool_id
current_tool_call["name"] = tool_name
current_tool_call["arguments"] = tool_args
@ -264,7 +264,7 @@ class KimiK2ToolParser(ToolParser):
)
if current_tool_call_name_matches:
(tool_id_str,) = current_tool_call_name_matches.groups()
tool_name = tool_id_str.split(".")[1].split(":")[0]
tool_name = tool_id_str.split(":")[0].split(".")[-1]
current_tool_call["id"] = tool_id_str
current_tool_call["name"] = tool_name
current_tool_call["arguments"] = ""