[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:
|
def _build_chat_request(
|
||||||
return ChatCompletionRequest.model_validate(
|
*,
|
||||||
{
|
tool_choice: str | dict[str, Any],
|
||||||
"model": "gemma4-test",
|
chat_template_kwargs: dict[str, Any] | None = None,
|
||||||
"messages": [{"role": "user", "content": "What is the weather in Hanoi?"}],
|
) -> ChatCompletionRequest:
|
||||||
"tools": [
|
data: dict[str, Any] = {
|
||||||
{
|
"model": "gemma4-test",
|
||||||
"type": "function",
|
"messages": [{"role": "user", "content": "What is the weather in Hanoi?"}],
|
||||||
"function": {
|
"tools": [
|
||||||
"name": "get_weather",
|
{
|
||||||
"description": "Get current weather for a city",
|
"type": "function",
|
||||||
"parameters": {
|
"function": {
|
||||||
"type": "object",
|
"name": "get_weather",
|
||||||
"properties": {"city": {"type": "string"}},
|
"description": "Get current weather for a city",
|
||||||
"required": ["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:
|
class _StubTokenizer:
|
||||||
@@ -212,3 +217,34 @@ def test_gemma4_named_skips_structured_outputs_responses() -> None:
|
|||||||
|
|
||||||
assert request.text is None
|
assert request.text is None
|
||||||
assert request.skip_special_tokens is False
|
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,
|
self,
|
||||||
request: ChatCompletionRequest | ResponsesRequest,
|
request: ChatCompletionRequest | ResponsesRequest,
|
||||||
) -> 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,
|
``skip_special_tokens`` must stay ``False`` when there is something to
|
||||||
keeping the default prevents tool-call delimiter tokens
|
preserve: reasoning channel tokens (thinking enabled) or tool-call
|
||||||
from leaking into content (e.g. with ``tool_choice="none"``).
|
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 {}
|
chat_template_kwargs = getattr(request, "chat_template_kwargs", None) or {}
|
||||||
if not chat_template_kwargs.get("enable_thinking", True):
|
enable_thinking = chat_template_kwargs.get("enable_thinking", True)
|
||||||
return request
|
has_tools = bool(getattr(request, "tools", None))
|
||||||
return super().adjust_request(request)
|
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:
|
def _reset(self, initial_state=None) -> None:
|
||||||
super()._reset(initial_state=initial_state)
|
super()._reset(initial_state=initial_state)
|
||||||
|
|||||||
Reference in New Issue
Block a user