[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
|
||||
|
||||
Reference in New Issue
Block a user