[Bugfix][Gemma4] Fix parsing when thinking is disabled (#45832)
Signed-off-by: Federico Iezzi <fiezzi@google.com>
This commit is contained in:
@@ -68,28 +68,33 @@ def _build_responses_request(*, tool_choice: str | dict[str, Any]) -> ResponsesR
|
||||
)
|
||||
|
||||
|
||||
def _build_chat_request(*, tool_choice: str | dict[str, Any]) -> ChatCompletionRequest:
|
||||
return ChatCompletionRequest.model_validate(
|
||||
{
|
||||
"model": "gemma4-test",
|
||||
"messages": [{"role": "user", "content": "What is the weather in Hanoi?"}],
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get current weather for a city",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"],
|
||||
},
|
||||
def _build_chat_request(
|
||||
*,
|
||||
tool_choice: str | dict[str, Any],
|
||||
chat_template_kwargs: dict[str, Any] | None = None,
|
||||
) -> ChatCompletionRequest:
|
||||
data: dict[str, Any] = {
|
||||
"model": "gemma4-test",
|
||||
"messages": [{"role": "user", "content": "What is the weather in Hanoi?"}],
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get current weather for a city",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"],
|
||||
},
|
||||
}
|
||||
],
|
||||
"tool_choice": tool_choice,
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
],
|
||||
"tool_choice": tool_choice,
|
||||
}
|
||||
if chat_template_kwargs is not None:
|
||||
data["chat_template_kwargs"] = chat_template_kwargs
|
||||
return ChatCompletionRequest.model_validate(data)
|
||||
|
||||
|
||||
class _StubTokenizer:
|
||||
@@ -212,3 +217,34 @@ def test_gemma4_named_skips_structured_outputs_responses() -> None:
|
||||
|
||||
assert request.text is None
|
||||
assert request.skip_special_tokens is False
|
||||
|
||||
|
||||
def test_gemma4_keeps_special_tokens_with_tools_thinking_disabled() -> None:
|
||||
"""tools active + thinking disabled: ``skip_special_tokens`` must stay
|
||||
False so ``<|tool_call>`` delimiters reach the extractor. The merged
|
||||
enable_thinking early-return stripped them, breaking tool calling when
|
||||
thinking is off.
|
||||
"""
|
||||
parser = Gemma4ToolParser(_StubTokenizer())
|
||||
request = _build_chat_request(
|
||||
tool_choice="auto", chat_template_kwargs={"enable_thinking": False}
|
||||
)
|
||||
|
||||
parser.adjust_request(request)
|
||||
|
||||
assert request.skip_special_tokens is False
|
||||
|
||||
|
||||
def test_gemma4_strips_special_tokens_when_nothing_to_preserve() -> None:
|
||||
"""No active tools + thinking disabled: keep the default
|
||||
(``skip_special_tokens=True``) so stray delimiters do not leak into
|
||||
content.
|
||||
"""
|
||||
parser = Gemma4ToolParser(_StubTokenizer())
|
||||
request = _build_chat_request(
|
||||
tool_choice="none", chat_template_kwargs={"enable_thinking": False}
|
||||
)
|
||||
|
||||
parser.adjust_request(request)
|
||||
|
||||
assert request.skip_special_tokens is True
|
||||
|
||||
+13
-7
@@ -443,16 +443,22 @@ class Gemma4Parser(ParserEngine):
|
||||
self,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> ChatCompletionRequest | ResponsesRequest:
|
||||
"""Skip ``skip_special_tokens=False`` when thinking is disabled.
|
||||
"""Keep special tokens when thinking or tool calls need them.
|
||||
|
||||
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"``).
|
||||
``skip_special_tokens`` must stay ``False`` when there is something to
|
||||
preserve: reasoning channel tokens (thinking enabled) or tool-call
|
||||
delimiters (tools active). Otherwise keep the default so stray
|
||||
delimiters do not leak into content (e.g. ``tool_choice="none"`` with
|
||||
thinking disabled).
|
||||
"""
|
||||
request = super().adjust_request(request)
|
||||
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)
|
||||
enable_thinking = chat_template_kwargs.get("enable_thinking", True)
|
||||
has_tools = bool(getattr(request, "tools", None))
|
||||
tools_active = has_tools and request.tool_choice != "none"
|
||||
if not enable_thinking and not tools_active:
|
||||
request.skip_special_tokens = True
|
||||
return request
|
||||
|
||||
def _reset(self, initial_state=None) -> None:
|
||||
super()._reset(initial_state=initial_state)
|
||||
|
||||
Reference in New Issue
Block a user