Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions python/sglang/srt/function_call/function_call_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Type, Union

from sglang.srt.function_call.base_format_detector import BaseFormatDetector
Expand All @@ -14,6 +15,8 @@
ToolChoice,
)

logger = logging.getLogger(__name__)


class FunctionCallParser:
"""
Expand Down Expand Up @@ -165,11 +168,35 @@ def get_ebnf(
) -> Optional[str]:
"""
Get the EBNF grammar for the specified tool choice.

Args:
tool_choice: The tool choice specification

Returns:
EBNF grammar string, or None if no valid tools found

Note:
If a specific function is requested but not found in available tools,
logs a warning and falls back to using all available tools for backward compatibility.
"""
filtered_tools = []
if isinstance(tool_choice, ToolChoice):
fn_name = tool_choice.function.name
filtered_tools = [t for t in self.tools if t.function.name == fn_name]

# Check if the requested function exists in available tools
if not filtered_tools:
available_functions = [t.function.name for t in self.tools]
logger.warning(
f"Function '{fn_name}' not found in available tools. "
f"Available functions: {available_functions}. "
f"Skipping tool choice."
)

# TODO: Return a 400 error instead of warning when adapter supports proper error handling
# For now, fall back to return None
return None
else:
filtered_tools = self.tools

return self.detector.build_ebnf(filtered_tools)
1 change: 1 addition & 0 deletions test/srt/run_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestFile:
TestFile("test_skip_tokenizer_init.py", 117),
TestFile("test_srt_engine.py", 261),
TestFile("test_srt_endpoint.py", 130),
TestFile("test_tool_choice.py", 120),
TestFile("test_torch_compile.py", 76),
TestFile("test_torch_compile_moe.py", 172),
TestFile("test_torch_native_attention_backend.py", 123),
Expand Down
Loading
Loading