From 9af3d535769599e45428e5e4b52cc6107cd14d5e Mon Sep 17 00:00:00 2001 From: Wenyi Xu Date: Wed, 10 Dec 2025 16:13:05 +0800 Subject: [PATCH 1/4] Add dynamic tool call parser choices to RouterArgs --- .../bindings/python/sglang_router/router_args.py | 7 ++++++- sgl-model-gateway/bindings/python/src/lib.rs | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/sgl-model-gateway/bindings/python/sglang_router/router_args.py b/sgl-model-gateway/bindings/python/sglang_router/router_args.py index 2eefb513a515..3600b75ca531 100644 --- a/sgl-model-gateway/bindings/python/sglang_router/router_args.py +++ b/sgl-model-gateway/bindings/python/sglang_router/router_args.py @@ -4,6 +4,8 @@ import os from typing import Dict, List, Optional +from sglang_router.sglang_router_rs import get_available_tool_call_parsers + logger = logging.getLogger(__name__) @@ -520,11 +522,14 @@ def add_cli_args( default=None, help="Specify the parser for reasoning models (e.g., deepseek-r1, qwen3)", ) + # Get parser choices dynamically from the Rust implementation + tool_call_parser_choices = get_available_tool_call_parsers() parser.add_argument( f"--{prefix}tool-call-parser", type=str, default=None, - help="Specify the parser for handling tool-call interactions", + choices=tool_call_parser_choices, + help=f"Specify the parser for tool-call interactions (e.g., json, qwen)", ) # MCP server configuration parser.add_argument( diff --git a/sgl-model-gateway/bindings/python/src/lib.rs b/sgl-model-gateway/bindings/python/src/lib.rs index 67082a618031..4128b0cc73a1 100644 --- a/sgl-model-gateway/bindings/python/src/lib.rs +++ b/sgl-model-gateway/bindings/python/src/lib.rs @@ -730,6 +730,13 @@ fn get_verbose_version_string() -> String { version::get_verbose_version_string() } +/// Get the list of available tool call parsers from the Rust factory. +#[pyfunction] +fn get_available_tool_call_parsers() -> Vec { + let factory = tool_parser::ParserFactory::new(); + factory.list_parsers() +} + #[pymodule] fn sglang_router_rs(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; @@ -740,5 +747,6 @@ fn sglang_router_rs(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_function(wrap_pyfunction!(get_version_string, m)?)?; m.add_function(wrap_pyfunction!(get_verbose_version_string, m)?)?; + m.add_function(wrap_pyfunction!(get_available_tool_call_parsers, m)?)?; Ok(()) } From 438549fef0ce21a564aef8cb3d90aeff6d904697 Mon Sep 17 00:00:00 2001 From: Wenyi Xu Date: Wed, 10 Dec 2025 16:34:38 +0800 Subject: [PATCH 2/4] Remove redundant comment about dynamic tool call parser choices in RouterArgs --- sgl-model-gateway/bindings/python/sglang_router/router_args.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sgl-model-gateway/bindings/python/sglang_router/router_args.py b/sgl-model-gateway/bindings/python/sglang_router/router_args.py index 3600b75ca531..01d391d83a20 100644 --- a/sgl-model-gateway/bindings/python/sglang_router/router_args.py +++ b/sgl-model-gateway/bindings/python/sglang_router/router_args.py @@ -522,7 +522,6 @@ def add_cli_args( default=None, help="Specify the parser for reasoning models (e.g., deepseek-r1, qwen3)", ) - # Get parser choices dynamically from the Rust implementation tool_call_parser_choices = get_available_tool_call_parsers() parser.add_argument( f"--{prefix}tool-call-parser", From 8b70a36e678d729209220cfae24b50a7fe4c6425 Mon Sep 17 00:00:00 2001 From: Wenyi Xu Date: Wed, 10 Dec 2025 16:57:29 +0800 Subject: [PATCH 3/4] Optimize tool call parser retrieval with OnceCell for caching --- sgl-model-gateway/bindings/python/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sgl-model-gateway/bindings/python/src/lib.rs b/sgl-model-gateway/bindings/python/src/lib.rs index 4128b0cc73a1..dbe29ebf4391 100644 --- a/sgl-model-gateway/bindings/python/src/lib.rs +++ b/sgl-model-gateway/bindings/python/src/lib.rs @@ -1,5 +1,6 @@ use pyo3::prelude::*; use sgl_model_gateway::*; +use once_cell::sync::OnceCell; use std::collections::HashMap; // Define the enums with PyO3 bindings @@ -733,8 +734,13 @@ fn get_verbose_version_string() -> String { /// Get the list of available tool call parsers from the Rust factory. #[pyfunction] fn get_available_tool_call_parsers() -> Vec { - let factory = tool_parser::ParserFactory::new(); - factory.list_parsers() + static PARSERS: OnceCell> = OnceCell::new(); + PARSERS + .get_or_init(|| { + let factory = tool_parser::ParserFactory::new(); + factory.list_parsers() + }) + .clone() } #[pymodule] From e5109cf302b4c34e7424ef260efe3a31f328c5be Mon Sep 17 00:00:00 2001 From: Wenyi Xu Date: Wed, 10 Dec 2025 17:05:40 +0800 Subject: [PATCH 4/4] Add once_cell dependency for improved caching support --- sgl-model-gateway/bindings/python/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/sgl-model-gateway/bindings/python/Cargo.toml b/sgl-model-gateway/bindings/python/Cargo.toml index 3f88cd6958df..f44f74a9a755 100644 --- a/sgl-model-gateway/bindings/python/Cargo.toml +++ b/sgl-model-gateway/bindings/python/Cargo.toml @@ -10,6 +10,7 @@ crate-type = ["cdylib"] [dependencies] pyo3 = { version = "0.27.1", features = ["extension-module", "abi3-py38"] } tokio = { version = "1.42.0", features = ["full"] } +once_cell = "1.19" [dependencies.sgl-model-gateway] path = "../.."