Skip to content
Open
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
12 changes: 12 additions & 0 deletions tensorrt_llm/serve/tool_parser/kimi_k2_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ def _parse_buffered_tool_call(self) -> List[ToolCallItem]:
return []
function_name = m.group("name")

if not self.current_tool_name_sent and function_name not in self._tool_indices:
# detect_and_parse skips a call to an undeclared function, so it is
# dropped here once its end token arrives, without a tool index.
call_end = current_text.find(
self.tool_call_end_token, match.start("function_arguments")
)
if call_end == -1:
return []
logger.warning(f"Model attempted to call undefined function: {function_name}")
self._buffer = current_text[call_end + len(self.tool_call_end_token) :]
return []

# Initialize state if this is the first tool call
if self.current_tool_id == -1:
self.current_tool_id = 0
Expand Down
22 changes: 22 additions & 0 deletions tests/unittest/llmapi/apps/test_tool_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ def test_undefined_tool(self, sample_tools, parser, tool_parser_test_cases):

assert len(result.calls) == 0

@pytest.mark.parametrize("chunk_size", [1, 7, 1000])
def test_undefined_tool_is_not_streamed(self, sample_tools,
tool_parser_test_cases, chunk_size):
"""Streaming filters undefined tools as detect_and_parse does."""
text = tool_parser_test_cases.undefined_tool
parser = KimiK2ToolParser()

calls = []
for start in range(0, len(text), chunk_size):
calls += parser.parse_streaming_increment(
text[start:start + chunk_size], sample_tools).calls
calls += parser.finish(sample_tools).calls

assert calls == []
assert parser.prev_tool_call_arr == []

def test_kimi_k2_format_compliance(self, sample_tools, parser):
"""Test that KimiK2ToolParser follows the documented format structure."""

Expand Down Expand Up @@ -863,6 +879,12 @@ def test_integer_too_long_to_convert_is_not_streamed_as_text(
("<|tool_calls_section_begin|><|tool_call_begin|>functions.get_weather:0"
"<|tool_call_argument_begin|>{}<|tool_call_end|>"
"<|tool_calls_section_end|>"),
# Both parses skip a call to a function the request does not declare.
"undeclared_call_between_calls":
("<|tool_calls_section_begin|>" + _KIMI_K2_WEATHER_CALL +
"<|tool_call_begin|>functions.undefined_func:1<|tool_call_argument_begin|>"
'{"arg": "any value"}<|tool_call_end|>' + _KIMI_K2_SEARCH_CALL +
"<|tool_calls_section_end|>"),
# Neither parse reads a call whose id lacks the ":<index>" suffix.
"id_without_index_then_a_call":
("<|tool_calls_section_begin|><|tool_call_begin|>functions.get_weather"
Expand Down
Loading