From 0c6849e37859fcb764b9c707636714552a910ac2 Mon Sep 17 00:00:00 2001 From: "minke.yu" Date: Sat, 14 Nov 2026 14:09:36 +0800 Subject: [PATCH] chat: flatten namespace tools to ns__function for Chat Completions --- .../sglang/srt/entrypoints/openai/protocol.py | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/entrypoints/openai/protocol.py b/python/sglang/srt/entrypoints/openai/protocol.py index 7749699675..3012160393 100644 --- a/python/sglang/srt/entrypoints/openai/protocol.py +++ b/python/sglang/srt/entrypoints/openai/protocol.py @@ -834,6 +834,26 @@ class Tool(BaseModel): return self +class ChatCompletionNamespaceTool(BaseModel): + """A Responses-style namespace grouping sent to the Chat Completions API. + + Chat Completions has no namespace field in its tool-call shape, so the + members are flattened to ``__`` before rendering; the + model calls that flat name and the response reports it back unchanged. + """ + + type: Literal["namespace"] + name: str + description: Optional[str] = None + tools: List[Tool] + + @model_validator(mode="after") + def validate_members(self) -> "ChatCompletionNamespaceTool": + if not self.tools: + raise ValueError("namespace tools must list at least one function tool") + return self + + # Tool is defined after the message params that reference it, so the forward # reference has to be resolved explicitly. ChatCompletionMessageGenericParam.model_rebuild() @@ -921,7 +941,9 @@ class ChatCompletionRequest(PDRoutingFields): temperature: Optional[float] = None top_p: Optional[float] = None user: Optional[str] = None - tools: Optional[List[Tool]] = Field(default=None, examples=[None]) + tools: Optional[List[Union[Tool, ChatCompletionNamespaceTool]]] = Field( + default=None, examples=[None] + ) # Set by /v1/responses when the request has namespace tools: the chat template # renders these, while ``tools`` (flattened to ``.``) still # drives tool-call parsing. @@ -2123,6 +2145,39 @@ class ResponsesRequest(PDRoutingFields): return choice return "auto" + @model_validator(mode="after") + def flatten_namespace_tools(self) -> "ChatCompletionRequest": + """Render namespace groups as flat ``ns__function`` chat tools.""" + if not self.tools: + return self + flattened: List[Tool] = [] + for tool in self.tools: + if isinstance(tool, ChatCompletionNamespaceTool): + for member in tool.tools: + name = f"{tool.name}__{member.function.name}" + description = member.function.description + if tool.description: + description = ( + tool.description + chr(10) + description + if description + else tool.description + ) + flattened.append( + Tool( + type="function", + function=Function( + name=name, + description=description, + parameters=member.function.parameters, + strict=member.function.strict, + ), + ) + ) + else: + flattened.append(tool) + self.tools = flattened + return self + def to_sampling_params( self, default_max_tokens: Optional[int] = None,