From 665f9c42535c34e7d04b1c0424d34fe7677bb245 Mon Sep 17 00:00:00 2001 From: longguo <107740309+abinggo@users.noreply.github.com> Date: Thu, 14 May 2026 09:03:30 +0800 Subject: [PATCH] [Bugfix] Fix Gemma4ToolParser streaming float corruption (#42128) Signed-off-by: abinggo <107740309+abinggo@users.noreply.github.com> --- tests/tool_parsers/test_gemma4_tool_parser.py | 29 +++++++++++++++++++ vllm/tool_parsers/gemma4_tool_parser.py | 12 ++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/tool_parsers/test_gemma4_tool_parser.py b/tests/tool_parsers/test_gemma4_tool_parser.py index bc2aebb7b2..6f3709e19a 100644 --- a/tests/tool_parsers/test_gemma4_tool_parser.py +++ b/tests/tool_parsers/test_gemma4_tool_parser.py @@ -135,6 +135,26 @@ class TestParseGemma4Args: result = _parse_gemma4_args('name:<|"|>test<|"|>,flag:', partial=True) assert result == {"name": "test"} + def test_trailing_dot_float_partial_withheld(self): + """Bare float ending with '.' is withheld in partial mode. + + Regression test for #42047: float("108.") → 108.0 causes + streaming diff corruption (108.0 → 108.2 becomes 108.02). + """ + # Single key with trailing dot — withheld entirely + result = _parse_gemma4_args("left:108.,right:22.8", partial=True) + assert result == {} + + # Stable key before trailing-dot key — stable key is kept + result = _parse_gemma4_args( + 'name:<|"|>test<|"|>,score:3.,count:1', partial=True + ) + assert result == {"name": "test"} + + # Non-partial mode parses trailing dot normally + result = _parse_gemma4_args("left:108.,right:22.8", partial=False) + assert result == {"left": 108.0, "right": 22.8} + @pytest.mark.timeout(5) def test_malformed_partial_array(self): result = _parse_gemma4_args(":[t:[]") @@ -164,6 +184,15 @@ class TestParseGemma4Array: result = _parse_gemma4_array("42,]trailing") assert result == [42] + def test_trailing_dot_float_partial_withheld(self): + """Array elements with trailing dot withheld in partial mode.""" + result = _parse_gemma4_array("108.,22.8", partial=True) + assert result == [] + + # Stable elements before trailing-dot element are kept + result = _parse_gemma4_array("42,108.,3", partial=True) + assert result == [42] + # --------------------------------------------------------------------------- # Non-streaming extraction tests diff --git a/vllm/tool_parsers/gemma4_tool_parser.py b/vllm/tool_parsers/gemma4_tool_parser.py index 95ec7edcd8..9925284273 100644 --- a/vllm/tool_parsers/gemma4_tool_parser.py +++ b/vllm/tool_parsers/gemma4_tool_parser.py @@ -211,6 +211,14 @@ def _parse_gemma4_args(args_str: str, *, partial: bool = False) -> dict: i, ) break + if partial: + raw_val = args_str[val_start:i].strip() + if raw_val.endswith("."): + # Trailing dot means decimal digits may still arrive + # (e.g. "108." may become "108.2"). Parsing now would + # yield float("108.") == 108.0, whose json repr "108.0" + # corrupts the streaming diff when the true digit lands. + break result[key] = _parse_gemma4_value(args_str[val_start:i]) return result @@ -294,6 +302,10 @@ def _parse_gemma4_array(arr_str: str, *, partial: bool = False) -> list: i, ) break + if partial: + raw_val = arr_str[val_start:i].strip() + if raw_val.endswith("."): + break items.append(_parse_gemma4_value(arr_str[val_start:i])) return items