From 8a9aa86ab77995de44e1835d79bbddf70744909f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20ERDO=C4=9EAN?= Date: Wed, 12 Aug 2026 22:16:15 +0300 Subject: [PATCH 1/2] [#17574][fix] Complete zero-argument tool calls in the streaming tool parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BaseToolParser.parse_streaming_increment gated the argument-streaming and call-completion branch on the truthiness of the parsed argument object. An empty object is falsy, so a tool call with no arguments never emitted its arguments and was never consumed from the buffer: the client was left with arguments="", which is not valid JSON, and has_tool_call stayed true for the rest of the request, so every later chunk was routed back into the tool-call branch instead of being emitted as content. Gate on "is not None" instead. The partial path is unaffected because the elif prev_arguments branch keeps its own falsy guard, so no premature "{}" is emitted while the JSON is still incomplete. Qwen3ToolParser is the parser that reaches this code, through _wrapped_streaming. Glm4ToolParser and Glm47ToolParser already assert the expected behaviour for their own streaming paths, so the new test mirrors theirs. Signed-off-by: Yiğit ERDOĞAN --- .../serve/tool_parser/base_tool_parser.py | 5 ++- .../unittest/llmapi/apps/test_tool_parsers.py | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tensorrt_llm/serve/tool_parser/base_tool_parser.py b/tensorrt_llm/serve/tool_parser/base_tool_parser.py index ece736749d2f..6fc1b7d5e16d 100644 --- a/tensorrt_llm/serve/tool_parser/base_tool_parser.py +++ b/tensorrt_llm/serve/tool_parser/base_tool_parser.py @@ -223,7 +223,10 @@ def parse_streaming_increment(self, new_text: str, cur_arguments = current_tool_call.get("arguments") res = StreamingParseResult() - if cur_arguments: + # An empty argument object is falsy but still has to be streamed and + # completed, otherwise a zero-argument tool call never finishes and its + # text stays in the buffer forever. + if cur_arguments is not None: # Calculate how much of the arguments we've already streamed sent = len( self.streamed_args_for_tool[self.current_tool_id]) diff --git a/tests/unittest/llmapi/apps/test_tool_parsers.py b/tests/unittest/llmapi/apps/test_tool_parsers.py index 7b24d4554337..891465fa7844 100644 --- a/tests/unittest/llmapi/apps/test_tool_parsers.py +++ b/tests/unittest/llmapi/apps/test_tool_parsers.py @@ -1016,6 +1016,46 @@ def test_streaming_wrapped_form_unregressed(self, sample_tools, parser): assert len(r_args.calls) == 1 assert json.loads(r_args.calls[0].parameters) == {"location": "SF"} + def test_streaming_zero_arg_tool(self, parser): + """Test streaming a zero-argument tool call.""" + tools = [ + ChatCompletionToolsParam( + type="function", + function=FunctionDefinition( + name="get_time", + description="Get current time", + parameters={ + "type": "object", + "properties": {}, + }, + ), + ) + ] + chunks = [ + "\n", + '{"name": "get_time"', + ', "arguments": {}}', + "\n", + ] + + results = [ + parser.parse_streaming_increment(chunk, tools) for chunk in chunks + ] + + names = [c.name for r in results for c in r.calls if c.name] + assert "get_time" in names + + # An empty argument object still has to be streamed, otherwise the client + # is left with arguments="", which is not valid JSON. + params = "".join(c.parameters for r in results for c in r.calls) + assert params == "{}", f"Expected '{{}}', got {params!r}" + + # The completed call must also be consumed from the buffer, otherwise the + # parser stays in the tool-call branch and swallows the rest of the output. + assert parser.detect_and_parse("".join(chunks), + tools).calls[0].parameters == "{}" + assert "" not in parser._buffer + class TestQwen3CoderToolParser(BaseToolParserTestClass): """Test suite for Qwen3CoderToolParser class.""" From d58427d9c96b4dd1abffc0081f348aa7969b7b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20ERDO=C4=9EAN?= Date: Sat, 15 Aug 2026 11:22:26 +0300 Subject: [PATCH 2/2] [#17574][fix] Complete zero-argument calls that omit or null the arguments key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up. Gating on "is not None" fixed the empty-object shape but left two equivalent ones dead-ending in exactly the same way: a model can express "no arguments" by omitting the key entirely, or by emitting an explicit null. Both make current_tool_call.get("arguments") return None, so the completion branch is skipped, the buffer never advances past the call, and _buffer only grows for the rest of the request. Normalize a missing or null arguments object to {} once the call's JSON is complete. Restricting it to is_current_complete keeps the partial path intact: while the JSON is still incomplete a missing key only means "not streamed yet", so it must not be mistaken for an empty object and flushed early. This matches parse_base_json, which already resolves a missing key to {} on the non-streaming path. It still dumps an explicit null as "null" there; the new parametrization asserts that so the divergence stays visible. Signed-off-by: Yiğit ERDOĞAN --- .../serve/tool_parser/base_tool_parser.py | 9 ++++++ .../unittest/llmapi/apps/test_tool_parsers.py | 31 ++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/tensorrt_llm/serve/tool_parser/base_tool_parser.py b/tensorrt_llm/serve/tool_parser/base_tool_parser.py index 6fc1b7d5e16d..f7d1055a418a 100644 --- a/tensorrt_llm/serve/tool_parser/base_tool_parser.py +++ b/tensorrt_llm/serve/tool_parser/base_tool_parser.py @@ -223,6 +223,15 @@ def parse_streaming_increment(self, new_text: str, cur_arguments = current_tool_call.get("arguments") res = StreamingParseResult() + # A finished call may carry no "arguments" key at all, or an + # explicit null. Both mean "no arguments" and are normalized to + # {} so the call still completes, matching what parse_base_json + # returns on the non-streaming path. While the JSON is still + # partial a missing key only means "not streamed yet", so it is + # left alone and the elif prev_arguments branch keeps handling it. + if is_current_complete and cur_arguments is None: + cur_arguments = {} + # An empty argument object is falsy but still has to be streamed and # completed, otherwise a zero-argument tool call never finishes and its # text stays in the buffer forever. diff --git a/tests/unittest/llmapi/apps/test_tool_parsers.py b/tests/unittest/llmapi/apps/test_tool_parsers.py index 891465fa7844..2a7bfcad819b 100644 --- a/tests/unittest/llmapi/apps/test_tool_parsers.py +++ b/tests/unittest/llmapi/apps/test_tool_parsers.py @@ -1016,8 +1016,25 @@ def test_streaming_wrapped_form_unregressed(self, sample_tools, parser): assert len(r_args.calls) == 1 assert json.loads(r_args.calls[0].parameters) == {"location": "SF"} - def test_streaming_zero_arg_tool(self, parser): - """Test streaming a zero-argument tool call.""" + @pytest.mark.parametrize( + "arguments_chunk,oneshot_params", + [ + (', "arguments": {}}', "{}"), + ("}", "{}"), + # detect_and_parse dumps an explicit null as-is; only the streaming + # path normalizes it. Asserted here so the divergence stays visible. + (', "arguments": null}', "null"), + ], + ids=["empty_object", "key_absent", "explicit_null"], + ) + def test_streaming_zero_arg_tool(self, parser, arguments_chunk, + oneshot_params): + """Test streaming a zero-argument tool call. + + A model can express "no arguments" as an empty object, by omitting the + key, or as an explicit null. All three have to complete the call and + stream "{}". + """ tools = [ ChatCompletionToolsParam( type="function", @@ -1034,7 +1051,7 @@ def test_streaming_zero_arg_tool(self, parser): chunks = [ "\n", '{"name": "get_time"', - ', "arguments": {}}', + arguments_chunk, "\n", ] @@ -1045,15 +1062,15 @@ def test_streaming_zero_arg_tool(self, parser): names = [c.name for r in results for c in r.calls if c.name] assert "get_time" in names - # An empty argument object still has to be streamed, otherwise the client - # is left with arguments="", which is not valid JSON. + # A zero-argument call still has to stream its arguments, otherwise the + # client is left with arguments="", which is not valid JSON. params = "".join(c.parameters for r in results for c in r.calls) assert params == "{}", f"Expected '{{}}', got {params!r}" # The completed call must also be consumed from the buffer, otherwise the # parser stays in the tool-call branch and swallows the rest of the output. - assert parser.detect_and_parse("".join(chunks), - tools).calls[0].parameters == "{}" + oneshot = parser.detect_and_parse("".join(chunks), tools) + assert oneshot.calls[0].parameters == oneshot_params assert "" not in parser._buffer