[Bugfix][Gemma4] Fix offline parser truncation, adjust_request token leak, and chat template sync (#45553)
Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com> Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com>
This commit is contained in:
co-authored by
Luciano Martins
parent
b8bd773fe4
commit
6607a80dab
+20
-3
@@ -423,6 +423,8 @@ class Gemma4Parser(ParserEngine):
|
||||
tools: list[Tool] | None = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {}
|
||||
self._thinking_enabled = chat_kwargs.get("enable_thinking", True)
|
||||
super().__init__(
|
||||
tokenizer,
|
||||
tools,
|
||||
@@ -437,6 +439,21 @@ class Gemma4Parser(ParserEngine):
|
||||
self._prefix_stripped: bool = False
|
||||
self._is_first_feed: bool = True
|
||||
|
||||
def adjust_request(
|
||||
self,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> ChatCompletionRequest | ResponsesRequest:
|
||||
"""Skip ``skip_special_tokens=False`` when thinking is disabled.
|
||||
|
||||
When there are no reasoning channel tokens to preserve,
|
||||
keeping the default prevents tool-call delimiter tokens
|
||||
from leaking into content (e.g. with ``tool_choice="none"``).
|
||||
"""
|
||||
chat_template_kwargs = getattr(request, "chat_template_kwargs", None) or {}
|
||||
if not chat_template_kwargs.get("enable_thinking", True):
|
||||
return request
|
||||
return super().adjust_request(request)
|
||||
|
||||
def _reset(self, initial_state=None) -> None:
|
||||
super()._reset(initial_state=initial_state)
|
||||
self._reasoning_text = ""
|
||||
@@ -494,12 +511,12 @@ class Gemma4Parser(ParserEngine):
|
||||
if tool_call_id is not None and tid == tool_call_id:
|
||||
return True
|
||||
if new_turn_id is not None and tid == new_turn_id:
|
||||
return False
|
||||
return not self._thinking_enabled
|
||||
if tool_response_id is not None and tid == tool_response_id:
|
||||
return False
|
||||
return not self._thinking_enabled
|
||||
if end_id is not None and tid == end_id:
|
||||
return True
|
||||
return self._reasoning_ended
|
||||
return True
|
||||
|
||||
def _events_to_delta(
|
||||
self,
|
||||
|
||||
@@ -35,8 +35,6 @@ Ported from ``transformers.models.gemma4.utils_gemma4`` so that vLLM users
|
||||
do not need a transformers dependency for output parsing.
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
import regex as re
|
||||
|
||||
# Tool call delimiter tokens as they appear in decoded text.
|
||||
@@ -52,42 +50,23 @@ _ESCAPE_TOKEN = '<|"|>'
|
||||
def _parse_tool_arguments(args_str: str) -> dict[str, str]:
|
||||
"""Parse tool call arguments from the Gemma4 compact format.
|
||||
|
||||
Handles the ``key:<|"|>value<|"|>`` format used by Gemma4, with fallback
|
||||
to heuristic key-value extraction. Also tolerates the slightly different
|
||||
``key: "value"`` format (space + plain quotes) that some chat templates
|
||||
produce.
|
||||
Delegates to the native ``<|"|>``-aware parser from
|
||||
``vllm.parser.gemma4``, which handles internal quotes, nested
|
||||
objects, arrays, and all Gemma4 value types correctly.
|
||||
|
||||
Args:
|
||||
args_str: Raw argument string from inside ``call:name{...}``.
|
||||
|
||||
Returns:
|
||||
Dictionary of argument name → value.
|
||||
Dictionary of argument name → string value.
|
||||
"""
|
||||
if not args_str or not args_str.strip():
|
||||
return {}
|
||||
|
||||
# Replace Gemma4 escape tokens with standard quotes.
|
||||
cleaned = args_str.replace(_ESCAPE_TOKEN, '"')
|
||||
from vllm.parser.gemma4 import _parse_gemma4_args
|
||||
|
||||
# Try JSON parsing first (handles nested values, arrays, etc.).
|
||||
try:
|
||||
parsed = json.loads("{" + cleaned + "}")
|
||||
# Ensure all values are strings for consistency.
|
||||
return {k: str(v) if not isinstance(v, str) else v for k, v in parsed.items()}
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
pass
|
||||
|
||||
# Fallback: extract key:"value" pairs (allow optional space after colon).
|
||||
arguments = {}
|
||||
for key, value in re.findall(r'(\w+):\s*"([^"]*)"', cleaned):
|
||||
arguments[key] = value
|
||||
|
||||
if not arguments:
|
||||
# Last resort: extract key:value pairs (unquoted).
|
||||
for key, value in re.findall(r"(\w+):\s*([^,}]+)", args_str):
|
||||
arguments[key] = value.strip().strip('"').replace(_ESCAPE_TOKEN, "")
|
||||
|
||||
return arguments
|
||||
parsed = _parse_gemma4_args(args_str)
|
||||
return {k: str(v) if not isinstance(v, str) else v for k, v in parsed.items()}
|
||||
|
||||
|
||||
def parse_tool_calls(text: str, *, strict: bool = False) -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user