From 7f616c327d24a259dd81605e513c42ce2b9dc204 Mon Sep 17 00:00:00 2001 From: Ben Browning Date: Thu, 18 Jun 2026 19:17:18 -0400 Subject: [PATCH] [Bugfix] [Parser] Fix empty tool block silently dropping subsequent content (#46091) Signed-off-by: Ben Browning Co-authored-by: Flora Feng <4florafeng@gmail.com> --- tests/parser/engine/trace_builder.py | 19 +++++++++++++++++-- vllm/parser/engine/parser_engine.py | 2 +- vllm/parser/gemma4.py | 4 ++++ vllm/parser/qwen3.py | 4 ++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/parser/engine/trace_builder.py b/tests/parser/engine/trace_builder.py index 4817d3b900..bee3d5d8b2 100644 --- a/tests/parser/engine/trace_builder.py +++ b/tests/parser/engine/trace_builder.py @@ -143,6 +143,12 @@ SCENARIOS: list[Scenario] = [ tool_calls=[_READ_TOOL], after_tool_response=True, ), + Scenario( + id="empty-tool-block", + description="Empty tool block followed by content (edge case recovery)", + content="Content after empty tools.", + tool_calls=[], + ), ] @@ -344,8 +350,11 @@ def _qwen3_segments(scenario: Scenario) -> list[tuple[str, bool]]: segs: list[tuple[str, bool]] = [] if scenario.reasoning is not None: segs.append((scenario.reasoning, False)) - if scenario.content is not None or scenario.tool_calls: + if scenario.content is not None or scenario.tool_calls is not None: segs.append(("", True)) + if scenario.tool_calls is not None and not scenario.tool_calls: + segs.append(("", True)) + segs.append(("", True)) if scenario.content is not None: segs.append((scenario.content, False)) if scenario.tool_calls: @@ -437,8 +446,11 @@ def _minimax_m2_segments(scenario: Scenario) -> list[tuple[str, bool]]: segs: list[tuple[str, bool]] = [] if scenario.reasoning is not None: segs.append((scenario.reasoning, False)) - if scenario.content is not None or scenario.tool_calls: + if scenario.content is not None or scenario.tool_calls is not None: segs.append(("", True)) + if scenario.tool_calls is not None and not scenario.tool_calls: + segs.append(("", True)) + segs.append(("", True)) if scenario.content is not None: segs.append((scenario.content, False)) if scenario.tool_calls: @@ -534,6 +546,9 @@ def _gemma4_segments(scenario: Scenario) -> list[tuple[str, bool]]: segs.append((_GEMMA4_THOUGHT_PREFIX, False)) segs.append((scenario.reasoning, False)) segs.append(("", True)) + if scenario.tool_calls is not None and not scenario.tool_calls: + segs.append(("<|tool_call>", True)) + segs.append(("", True)) if scenario.content is not None: segs.append((scenario.content, False)) if scenario.tool_calls: diff --git a/vllm/parser/engine/parser_engine.py b/vllm/parser/engine/parser_engine.py index 237e274563..dafb26fc48 100644 --- a/vllm/parser/engine/parser_engine.py +++ b/vllm/parser/engine/parser_engine.py @@ -672,7 +672,7 @@ class ParserEngine(Parser): if len(tool_call_deltas) > 1: tool_call_deltas = self._coalesce_tool_call_deltas(tool_call_deltas) - if self._deferred_content and not seen_tool_event: + if self._deferred_content and (not seen_tool_event or not tool_call_deltas): content_parts.insert(0, self._deferred_content) self._deferred_content = "" diff --git a/vllm/parser/gemma4.py b/vllm/parser/gemma4.py index 5dd07e44e3..e9223ee72f 100644 --- a/vllm/parser/gemma4.py +++ b/vllm/parser/gemma4.py @@ -375,6 +375,10 @@ def gemma4_config() -> ParserEngineConfig: ParserState.TOOL_PREAMBLE, (EventType.REASONING_END, EventType.TOOL_CALL_START), ), + (ParserState.TOOL_PREAMBLE, "TOOL_END"): Transition( + ParserState.CONTENT, + (EventType.TOOL_CALL_END,), + ), (ParserState.TOOL_PREAMBLE, "CALL_PREFIX"): Transition( ParserState.TOOL_NAME, (), diff --git a/vllm/parser/qwen3.py b/vllm/parser/qwen3.py index 45e3c7e432..583d3481bd 100644 --- a/vllm/parser/qwen3.py +++ b/vllm/parser/qwen3.py @@ -125,6 +125,10 @@ def qwen3_config(thinking: bool = True) -> ParserEngineConfig: ParserState.TOOL_NAME, (EventType.TOOL_CALL_START,), ), + (ParserState.TOOL_PREAMBLE, "TOOL_END"): Transition( + ParserState.CONTENT, + (EventType.TOOL_CALL_END,), + ), (ParserState.TOOL_PREAMBLE, "FUNC_PREFIX"): Transition( ParserState.TOOL_NAME, (),