[Frontend] Skip structural tags for auto tool_choice without strict mode (#45600)
Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -216,14 +216,32 @@ def test_streaming_emits_incremental_argument_chunks():
|
||||
}
|
||||
|
||||
|
||||
def _with_strict(
|
||||
tools: list[ChatCompletionToolsParam],
|
||||
) -> list[ChatCompletionToolsParam]:
|
||||
return [
|
||||
ChatCompletionToolsParam(
|
||||
type=t.type,
|
||||
function=FunctionDefinition(
|
||||
name=t.function.name,
|
||||
description=t.function.description,
|
||||
parameters=t.function.parameters,
|
||||
strict=True,
|
||||
),
|
||||
)
|
||||
for t in tools
|
||||
]
|
||||
|
||||
|
||||
def test_get_vllm_registry_structural_tag_returns_structural_tag(
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
) -> None:
|
||||
parser = make_parser()
|
||||
strict_tools = _with_strict(sample_tools)
|
||||
req = ChatCompletionRequest(
|
||||
messages=[],
|
||||
model="m",
|
||||
tools=sample_tools,
|
||||
tools=strict_tools,
|
||||
tool_choice="auto",
|
||||
)
|
||||
tag = parser.get_structural_tag(req)
|
||||
|
||||
@@ -14,6 +14,7 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionNamedToolChoiceParam,
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionToolsParam,
|
||||
FunctionDefinition,
|
||||
)
|
||||
from vllm.entrypoints.openai.engine.protocol import (
|
||||
DeltaMessage,
|
||||
@@ -115,6 +116,23 @@ def sample_tools(request):
|
||||
]
|
||||
|
||||
|
||||
def _with_strict(
|
||||
tools: list[ChatCompletionToolsParam],
|
||||
) -> list[ChatCompletionToolsParam]:
|
||||
return [
|
||||
ChatCompletionToolsParam(
|
||||
type=t.type,
|
||||
function=FunctionDefinition(
|
||||
name=t.function.name,
|
||||
description=t.function.description,
|
||||
parameters=t.function.parameters,
|
||||
strict=True,
|
||||
),
|
||||
)
|
||||
for t in tools
|
||||
]
|
||||
|
||||
|
||||
def _as_chat_completion_tools(
|
||||
tools: list[ChatCompletionToolsParam | FunctionTool],
|
||||
) -> list[ChatCompletionToolsParam]:
|
||||
@@ -1323,10 +1341,11 @@ def test_get_vllm_registry_structural_tag_returns_structural_tag(
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
) -> None:
|
||||
request_tools = _as_chat_completion_tools(sample_tools)
|
||||
strict_tools = _with_strict(request_tools)
|
||||
req = ChatCompletionRequest(
|
||||
messages=[],
|
||||
model="m",
|
||||
tools=request_tools,
|
||||
tools=strict_tools,
|
||||
tool_choice="auto",
|
||||
)
|
||||
tag = qwen3_tool_parser.get_structural_tag(req)
|
||||
@@ -1364,10 +1383,11 @@ def test_adjust_request_auto_uses_vllm_registry_structural_tag(
|
||||
tool_parser_cls = Qwen3EngineToolParser
|
||||
|
||||
request_tools = _as_chat_completion_tools(sample_tools)
|
||||
strict_tools = _with_strict(request_tools)
|
||||
req = ChatCompletionRequest(
|
||||
messages=[],
|
||||
model="m",
|
||||
tools=request_tools,
|
||||
tools=strict_tools,
|
||||
tool_choice="auto",
|
||||
include_reasoning=include_reasoning,
|
||||
)
|
||||
|
||||
@@ -51,6 +51,24 @@ def sample_tools() -> list[ChatCompletionToolsParam]:
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_tools_strict() -> list[ChatCompletionToolsParam]:
|
||||
return [
|
||||
ChatCompletionToolsParam(
|
||||
type="function",
|
||||
function={
|
||||
"name": "get_weather",
|
||||
"strict": True,
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
"required": ["city"],
|
||||
},
|
||||
},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_supported_structural_tag_models_include_vllm_builtins():
|
||||
assert SUPPORTED_STRUCTURAL_TAG_MODELS == (
|
||||
XGRAMMAR_BUILTIN_STRUCTURAL_TAG_MODELS | VLLM_BUILTIN_STRUCTURAL_TAG_MODELS
|
||||
@@ -61,11 +79,11 @@ def test_supported_structural_tag_models_include_vllm_builtins():
|
||||
@pytest.mark.parametrize("model", sorted(XGRAMMAR_BUILTIN_STRUCTURAL_TAG_MODELS))
|
||||
def test_get_model_structural_tag_supports_all_xgrammar_builtins(
|
||||
model: str,
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
sample_tools_strict: list[ChatCompletionToolsParam],
|
||||
):
|
||||
tag = get_model_structural_tag(
|
||||
model=model,
|
||||
tools=sample_tools,
|
||||
tools=sample_tools_strict,
|
||||
tool_choice="auto",
|
||||
reasoning=False,
|
||||
)
|
||||
@@ -219,7 +237,7 @@ def test_non_structural_tag_parser_uses_schema_constraints(
|
||||
|
||||
def test_get_structural_tag_disables_reasoning(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
sample_tools_strict: list[ChatCompletionToolsParam],
|
||||
):
|
||||
captured: list[bool] = []
|
||||
|
||||
@@ -235,10 +253,10 @@ def test_get_structural_tag_disables_reasoning(
|
||||
request = ChatCompletionRequest(
|
||||
messages=[],
|
||||
model="m",
|
||||
tools=sample_tools,
|
||||
tools=sample_tools_strict,
|
||||
tool_choice="auto",
|
||||
)
|
||||
parser = Qwen3EngineToolParser(MagicMock(), tools=sample_tools)
|
||||
parser = Qwen3EngineToolParser(MagicMock(), tools=sample_tools_strict)
|
||||
|
||||
parser.get_structural_tag(request)
|
||||
|
||||
@@ -247,7 +265,7 @@ def test_get_structural_tag_disables_reasoning(
|
||||
|
||||
def test_unified_parser_get_structural_tag_disables_reasoning(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
sample_tools_strict: list[ChatCompletionToolsParam],
|
||||
):
|
||||
captured: list[bool] = []
|
||||
|
||||
@@ -266,10 +284,10 @@ def test_unified_parser_get_structural_tag_disables_reasoning(
|
||||
request = ChatCompletionRequest(
|
||||
messages=[],
|
||||
model="m",
|
||||
tools=sample_tools,
|
||||
tools=sample_tools_strict,
|
||||
tool_choice="auto",
|
||||
)
|
||||
parser = TestParser(MagicMock(), tools=sample_tools)
|
||||
parser = TestParser(MagicMock(), tools=sample_tools_strict)
|
||||
parser.reasoning_parser = MagicMock(adjust_request=lambda request: request)
|
||||
|
||||
parser.adjust_request(request)
|
||||
@@ -279,7 +297,7 @@ def test_unified_parser_get_structural_tag_disables_reasoning(
|
||||
|
||||
def test_xgrammar_function_parameters_are_preserved(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
sample_tools_strict: list[ChatCompletionToolsParam],
|
||||
):
|
||||
captured: list[list[dict]] = []
|
||||
|
||||
@@ -294,15 +312,31 @@ def test_xgrammar_function_parameters_are_preserved(
|
||||
|
||||
get_model_structural_tag(
|
||||
model="llama",
|
||||
tools=sample_tools,
|
||||
tools=sample_tools_strict,
|
||||
tool_choice="auto",
|
||||
reasoning=False,
|
||||
)
|
||||
|
||||
assert (
|
||||
captured[0][0]["function"]["parameters"] == sample_tools[0].function.parameters
|
||||
captured[0][0]["function"]["parameters"]
|
||||
== sample_tools_strict[0].function.parameters
|
||||
)
|
||||
assert sample_tools[0].function.parameters is not None
|
||||
assert sample_tools_strict[0].function.parameters is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("model", sorted(XGRAMMAR_BUILTIN_STRUCTURAL_TAG_MODELS))
|
||||
def test_auto_tool_choice_skips_structural_tag_without_strict(
|
||||
model: str,
|
||||
sample_tools: list[ChatCompletionToolsParam],
|
||||
):
|
||||
tag = get_model_structural_tag(
|
||||
model=model,
|
||||
tools=sample_tools,
|
||||
tool_choice="auto",
|
||||
reasoning=False,
|
||||
)
|
||||
|
||||
assert tag is None
|
||||
|
||||
|
||||
def test_get_function_parameters_relaxes_function_strict_false():
|
||||
|
||||
Reference in New Issue
Block a user