From 84aef37808040b05b86aa61b8308ebf7506e7199 Mon Sep 17 00:00:00 2001 From: Raghav Ravishankar <113712354+alyosha-swamy@users.noreply.github.com> Date: Tue, 20 Jan 2026 08:03:17 +0530 Subject: [PATCH] fix function calling for Trinity (#17364) --- .../srt/function_call/function_call_parser.py | 2 + .../srt/function_call/trinity_detector.py | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 python/sglang/srt/function_call/trinity_detector.py diff --git a/python/sglang/srt/function_call/function_call_parser.py b/python/sglang/srt/function_call/function_call_parser.py index 8df0a2401..df03dae71 100644 --- a/python/sglang/srt/function_call/function_call_parser.py +++ b/python/sglang/srt/function_call/function_call_parser.py @@ -27,6 +27,7 @@ from sglang.srt.function_call.pythonic_detector import PythonicDetector from sglang.srt.function_call.qwen3_coder_detector import Qwen3CoderDetector from sglang.srt.function_call.qwen25_detector import Qwen25Detector from sglang.srt.function_call.step3_detector import Step3Detector +from sglang.srt.function_call.trinity_detector import TrinityDetector from sglang.srt.function_call.utils import get_json_schema_constraint logger = logging.getLogger(__name__) @@ -59,6 +60,7 @@ class FunctionCallParser: "qwen3_coder": Qwen3CoderDetector, "step3": Step3Detector, "minimax-m2": MinimaxM2Detector, + "trinity": TrinityDetector, "interns1": InternlmDetector, } diff --git a/python/sglang/srt/function_call/trinity_detector.py b/python/sglang/srt/function_call/trinity_detector.py new file mode 100644 index 000000000..e6288d3b3 --- /dev/null +++ b/python/sglang/srt/function_call/trinity_detector.py @@ -0,0 +1,43 @@ +import logging +from typing import List + +from sglang.srt.entrypoints.openai.protocol import Tool +from sglang.srt.function_call.core_types import StreamingParseResult +from sglang.srt.function_call.qwen25_detector import Qwen25Detector + +logger = logging.getLogger(__name__) + + +class TrinityDetector(Qwen25Detector): + """ + Detector for Trinity models using Qwen-style function call format. + + This detector extends Qwen25Detector to handle tool calls that may appear + inside sections by stripping the think tags before parsing. + + Reference: https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct?chat_template=default + """ + + def _strip_think_tags(self, text: str) -> str: + """Remove and tags, keeping the content inside.""" + return text.replace("", "").replace("", "") + + def has_tool_call(self, text: str) -> bool: + """Check if the text contains a tool call.""" + return super().has_tool_call(self._strip_think_tags(text)) + + def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult: + """ + One-time parsing: Detects and parses tool calls in the provided text. + """ + return super().detect_and_parse(self._strip_think_tags(text), tools) + + def parse_streaming_increment( + self, new_text: str, tools: List[Tool] + ) -> StreamingParseResult: + """ + Streaming incremental parsing for tool calls. + """ + return super().parse_streaming_increment( + self._strip_think_tags(new_text), tools + )