[Feature] New structural tag support (#10691)

This commit is contained in:
DarkSharpness
2025-10-20 18:25:58 +08:00
committed by GitHub
parent 296f689242
commit 276e7b3e4e
7 changed files with 326 additions and 22 deletions

View File

@@ -2,9 +2,10 @@ import logging
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Type, Union
from sglang.srt.entrypoints.openai.protocol import (
StructuralTagResponseFormat,
LegacyStructuralTagResponseFormat,
StructuresResponseFormat,
Tool,
ToolCallConstraint,
ToolChoice,
)
from sglang.srt.function_call.base_format_detector import BaseFormatDetector
@@ -51,7 +52,6 @@ class FunctionCallParser:
}
def __init__(self, tools: List[Tool], tool_call_parser: str):
detector: Type[BaseFormatDetector] = None
detector_class = self.ToolCallParserEnum.get(tool_call_parser)
if detector_class:
detector = detector_class()
@@ -123,7 +123,7 @@ class FunctionCallParser:
return final_normal_text, final_calls
def get_structure_tag(self) -> StructuralTagResponseFormat:
def get_structure_tag(self) -> LegacyStructuralTagResponseFormat:
"""
Generate a structural tag response format for all available tools.
@@ -151,7 +151,9 @@ class FunctionCallParser:
)
tool_trigger_set.add(info.trigger)
return StructuralTagResponseFormat(
# TODO(dark): move this into new structural tag format
# This requires all grammar backend support the new format
return LegacyStructuralTagResponseFormat(
type="structural_tag",
structures=tool_structures,
triggers=list(tool_trigger_set),
@@ -159,7 +161,7 @@ class FunctionCallParser:
def get_structure_constraint(
self, tool_choice: Union[ToolChoice, Literal["auto", "required"]]
) -> Optional[Tuple[str, Any]]:
) -> Optional[ToolCallConstraint]:
"""
Returns the appropriate structure constraint for tool calls based on the tool_choice.
The constraint is used to guide the model's output format.
@@ -178,8 +180,8 @@ class FunctionCallParser:
and tool_choice == "auto"
and any(tool.function.strict for tool in self.tools)
):
strict_tag = self.get_structure_tag()
return ("structural_tag", strict_tag)
tag = self.get_structure_tag()
return ("structural_tag", tag)
elif tool_choice == "required" or isinstance(tool_choice, ToolChoice):
json_schema = get_json_schema_constraint(self.tools, tool_choice)
return ("json_schema", json_schema)