[Fix] glm47: Disable tool grammar for tool_choice="none" (#41233)

Co-authored-by: Po-Han Huang (NVIDIA) <53919306+nvpohanh@users.noreply.github.com>
Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com>
Co-authored-by: Po-Han Huang <pohanh@nvidia.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tou
2026-10-20 09:36:06 +08:00
committed by GitHub
co-authored by Po-Han Huang Xinyuan Tong Po-Han Huang Claude Opus 5.5
parent 1897575ce1
commit 79ed13fc2c
2 changed files with 67 additions and 3 deletions
@@ -1378,18 +1378,19 @@ class OpenAIServingChat(OpenAIServingBase):
tool_call_constraint = None
effective_tools = self._effective_tools(request)
# Only tool-bearing requests get the full-assistant EBNF: its terminal
# state finishes a request even under ignore_eos.
# A grammar reaching its terminal state ends generation regardless of
# `ignore_eos`, so only install it when the request allows tool calls.
glm_constraint = (
self.tool_call_parser == "glm47"
and bool(effective_tools)
and request.tool_choice != "none"
and not any(tool.function.strict for tool in effective_tools)
)
if glm_constraint:
enable_thinking = (request.chat_template_kwargs or {}).get(
"enable_thinking"
)
parser = FunctionCallParser(request.tools or [], self.tool_call_parser)
parser = FunctionCallParser(effective_tools, self.tool_call_parser)
tool_call_constraint = parser.get_structure_constraint(
request.tool_choice,
parallel_tool_calls=request.parallel_tool_calls,
@@ -1346,6 +1346,69 @@ class ServingChatTestCase(CustomTestCase):
second_tools, [tool.function.model_dump() for tool in req.tools]
)
def test_glm47_constraint_includes_message_tools(self):
"""Constrain every active message tool, but none when tool use is disabled.
A terminal GLM47 grammar on ``tool_choice="none"`` ends generation early,
while omitting message-scoped tools rejects otherwise valid tool calls.
"""
import xgrammar as xgr
self.template_manager.chat_template_name = None
self.template_manager.jinja_template_content_format = "string"
self.chat.tool_call_parser = "glm47"
self.tm.tokenizer.apply_chat_template.return_value = [1, 2, 3]
compiler = xgr.GrammarCompiler(
xgr.TokenizerInfo(
[bytes([i]) for i in range(256)], vocab_type=xgr.VocabType.RAW
),
max_threads=1,
)
message_tool = {
"type": "function",
"function": {"name": "message_tool", "parameters": {"type": "object"}},
}
top_level_tool = {
"type": "function",
"function": {"name": "top_level_tool", "parameters": {"type": "object"}},
}
named = ToolChoice(function=ToolChoiceFuncName(name="message_tool"))
for role in ("system", "developer"):
for tools in (None, [], [top_level_tool]):
for choice in ("auto", "required", named, "none"):
with self.subTest(role=role, tools=tools, choice=choice):
request = ChatCompletionRequest(
model="x",
messages=[
{"role": role, "content": "", "tools": [message_tool]},
{"role": "user", "content": "Use message_tool."},
],
tools=tools,
tool_choice=choice,
chat_template_kwargs={"enable_thinking": False},
)
result = self.chat._process_messages(
request, is_multimodal=False
)
if choice == "none":
self.assertIsNone(result.tool_call_constraint)
continue
self.assertIsNotNone(result.tool_call_constraint)
kind, ebnf = result.tool_call_constraint
self.assertEqual(kind, "full_assistant_ebnf")
grammar = compiler.compile_grammar(xgr.Grammar.from_ebnf(ebnf))
for name, allowed in (
("message_tool", True),
("top_level_tool", bool(tools) and choice != named),
("unknown_tool", False),
):
matcher = xgr.GrammarMatcher(grammar)
accepted = (
matcher.accept_string(f"<tool_call>{name}</tool_call>")
and matcher.is_completed()
)
self.assertEqual(accepted, allowed, name)
def test_xgrammar_tag_omits_reasoning_when_parser_owns_it(self):
"""ReasonerGrammarBackend owns the thinking prefix when a parser is set."""
self.template_manager.chat_template_name = None