From 0e76b18805aabaf7287bafd360000c07b003ab92 Mon Sep 17 00:00:00 2001 From: LeizhenZhang Date: Sat, 21 Nov 2026 06:20:32 -0600 Subject: [PATCH] [None][fix] Kimi K2 tool parser: skip a call to an undeclared function in streaming too detect_and_parse skips a call to a function the request does not declare, as test_undefined_tool expects. Streaming reported the same call, so a streamed response carried it with finish_reason "tool_calls" while the non-streaming response had no call. Streaming now holds such a call until its end token and drops it without giving it a tool index. Signed-off-by: LeizhenZhang --- .../serve/tool_parser/kimi_k2_tool_parser.py | 12 ++++++++++ .../unittest/llmapi/apps/test_tool_parsers.py | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tensorrt_llm/serve/tool_parser/kimi_k2_tool_parser.py b/tensorrt_llm/serve/tool_parser/kimi_k2_tool_parser.py index 0ea0b9ec9ca1..2bf63f1dc5b0 100644 --- a/tensorrt_llm/serve/tool_parser/kimi_k2_tool_parser.py +++ b/tensorrt_llm/serve/tool_parser/kimi_k2_tool_parser.py @@ -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 diff --git a/tests/unittest/llmapi/apps/test_tool_parsers.py b/tests/unittest/llmapi/apps/test_tool_parsers.py index 6065d9f97b45..b792faa4da1c 100644 --- a/tests/unittest/llmapi/apps/test_tool_parsers.py +++ b/tests/unittest/llmapi/apps/test_tool_parsers.py @@ -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.""" @@ -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 ":" suffix. "id_without_index_then_a_call": ("<|tool_calls_section_begin|><|tool_call_begin|>functions.get_weather"