[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:
Luciano Martins
2026-06-16 04:31:53 +00:00
committed by GitHub
co-authored by Luciano Martins
parent b8bd773fe4
commit 6607a80dab
5 changed files with 94 additions and 74 deletions
+20 -3
View File
@@ -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,