From 79ed13fc2c5d945f86ed2d5f511c79fcfb76023c Mon Sep 17 00:00:00 2001 From: tou <57480529+toulzx@users.noreply.github.com> Date: Tue, 20 Oct 2026 09:36:06 +0800 Subject: [PATCH] [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 Co-authored-by: Claude Opus 5.5 --- .../srt/entrypoints/openai/serving_chat.py | 7 ++- .../entrypoints/openai/test_serving_chat.py | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/entrypoints/openai/serving_chat.py b/python/sglang/srt/entrypoints/openai/serving_chat.py index ec56b6f709..9ecca0c71e 100644 --- a/python/sglang/srt/entrypoints/openai/serving_chat.py +++ b/python/sglang/srt/entrypoints/openai/serving_chat.py @@ -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, diff --git a/test/registered/unit/entrypoints/openai/test_serving_chat.py b/test/registered/unit/entrypoints/openai/test_serving_chat.py index d28d6663c0..ccbf0f3cfb 100644 --- a/test/registered/unit/entrypoints/openai/test_serving_chat.py +++ b/test/registered/unit/entrypoints/openai/test_serving_chat.py @@ -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"{name}") + 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