[Frontend] Support CustomToolCall in the non-Harmony Responses API (#57322)
Signed-off-by: Shijin Zhang <75300765+Dovis01@users.noreply.github.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -8,6 +8,7 @@ from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.parser.gemma4 import (
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALL_START,
|
||||
@@ -117,6 +118,21 @@ def mock_request():
|
||||
return request
|
||||
|
||||
|
||||
def test_named_custom_tool_choice_skips_structured_outputs(mock_tokenizer):
|
||||
# Responses custom tools ride the function shim, so a named custom choice
|
||||
# must route like a named function choice, not into guided JSON.
|
||||
tools = [{"type": "custom", "name": "emit_command"}]
|
||||
request = ResponsesRequest(
|
||||
input="hi",
|
||||
tools=tools,
|
||||
tool_choice={"type": "custom", "name": "emit_command"},
|
||||
)
|
||||
Gemma4EngineToolParser(mock_tokenizer, tools=tools).adjust_request(request)
|
||||
|
||||
assert request.skip_special_tokens is False
|
||||
assert request.structured_outputs is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Unit tests for _parse_gemma4_args (shared parser logic)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -8,6 +8,7 @@ import pytest
|
||||
|
||||
from vllm.entrypoints.generate.base.protocol import DeltaMessage
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.parser.parser_manager import ParserManager
|
||||
from vllm.tool_parsers import ToolParserManager
|
||||
from vllm.tool_parsers.k2_horizon_tool_parser import K2HorizonToolParser
|
||||
@@ -316,3 +317,14 @@ def test_whitespace_only_surrounding_content_is_not_preserved(tokenizer):
|
||||
def test_tool_parser_registered():
|
||||
assert ToolParserManager.get_tool_parser("k2_horizon") is K2HorizonToolParser
|
||||
assert K2HorizonToolParser.supports_required_and_named is False
|
||||
|
||||
|
||||
def test_named_tool_resolves_custom_choice():
|
||||
# Custom tools ride the function shim; a ToolChoiceCustom must resolve to
|
||||
# its name like ToolChoiceFunction does.
|
||||
request = ResponsesRequest(
|
||||
input="hi",
|
||||
tools=[{"type": "custom", "name": "emit_command"}],
|
||||
tool_choice={"type": "custom", "name": "emit_command"},
|
||||
)
|
||||
assert K2HorizonToolParser._named_tool(request) == "emit_command"
|
||||
|
||||
@@ -8,6 +8,7 @@ import pytest
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionRequest,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
|
||||
from vllm.exceptions import VLLMValidationError
|
||||
from vllm.sampling_params import StructuredOutputsParams
|
||||
|
||||
@@ -60,3 +61,26 @@ def test_named_choice_allowed_with_structural_tag():
|
||||
def test_named_choice_rejected_without_structural_tag():
|
||||
with pytest.raises(VLLMValidationError):
|
||||
_parser().adjust_request(_request(with_tag=False))
|
||||
|
||||
|
||||
def _custom_request(with_tag: bool) -> ResponsesRequest:
|
||||
req = ResponsesRequest(
|
||||
input="hi",
|
||||
tools=[{"type": "custom", "name": "emit_command"}],
|
||||
tool_choice={"type": "custom", "name": "emit_command"},
|
||||
)
|
||||
if with_tag:
|
||||
req.structured_outputs = StructuredOutputsParams(
|
||||
structural_tag='{"type": "structural_tag", "format": {}}'
|
||||
)
|
||||
return req
|
||||
|
||||
|
||||
def test_named_custom_choice_allowed_with_structural_tag():
|
||||
req = _parser().adjust_request(_custom_request(with_tag=True))
|
||||
assert req.skip_special_tokens is False
|
||||
|
||||
|
||||
def test_named_custom_choice_rejected_without_structural_tag():
|
||||
with pytest.raises(VLLMValidationError):
|
||||
_parser().adjust_request(_custom_request(with_tag=False))
|
||||
|
||||
@@ -78,12 +78,15 @@ def _build_chat_request(*, tool_choice: str | dict[str, Any]) -> ChatCompletionR
|
||||
|
||||
|
||||
def _build_responses_request(
|
||||
*, tool_choice: str | dict[str, Any], include: list[str] | None = None
|
||||
*,
|
||||
tool_choice: str | dict[str, Any],
|
||||
include: list[str] | None = None,
|
||||
tools: list[dict[str, Any]] | None = None,
|
||||
) -> ResponsesRequest:
|
||||
return ResponsesRequest(
|
||||
model="poolside-test",
|
||||
input=[{"role": "user", "content": "write the file"}],
|
||||
tools=[_responses_write_file_tool()],
|
||||
tools=tools if tools is not None else [_responses_write_file_tool()],
|
||||
tool_choice=tool_choice,
|
||||
stream=True,
|
||||
max_output_tokens=200,
|
||||
@@ -145,6 +148,19 @@ def test_named_skips_structured_outputs_responses() -> None:
|
||||
assert request.skip_special_tokens is False
|
||||
|
||||
|
||||
def test_named_custom_tool_choice_skips_structured_outputs_responses() -> None:
|
||||
# A named custom choice parses to ToolChoiceCustom; custom tools ride the
|
||||
# function shim, so it must route like ToolChoiceFunction.
|
||||
request = _build_responses_request(
|
||||
tools=[{"type": "custom", "name": "emit_command"}],
|
||||
tool_choice={"type": "custom", "name": "emit_command"},
|
||||
)
|
||||
PoolsideV1ToolParser(_StubTokenizer()).adjust_request(request)
|
||||
|
||||
assert request.text is None
|
||||
assert request.skip_special_tokens is False
|
||||
|
||||
|
||||
def test_auto_still_keeps_special_tokens() -> None:
|
||||
request = _build_chat_request(tool_choice="auto")
|
||||
_make_parser(request).adjust_request(request)
|
||||
|
||||
@@ -7,7 +7,7 @@ from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from openai.types.responses import ToolChoiceFunction
|
||||
from openai.types.responses import CustomTool, ToolChoiceFunction
|
||||
from openai.types.responses.tool_choice_allowed import ToolChoiceAllowed
|
||||
from xgrammar import Grammar, StructuralTag
|
||||
from xgrammar import get_model_structural_tag as get_xgrammar_builtin_structural_tag
|
||||
@@ -787,6 +787,19 @@ def test_get_model_structural_tag_supports_named_tool_choice(
|
||||
assert isinstance(tag, StructuralTag)
|
||||
|
||||
|
||||
def test_glm_4_7_forced_custom_tool_uses_function_shim():
|
||||
tools = [CustomTool(type="custom", name="emit_command", description="Emit.")]
|
||||
tag = get_model_structural_tag(
|
||||
model="glm_4_7",
|
||||
tools=tools,
|
||||
tool_choice="required",
|
||||
reasoning=False,
|
||||
)
|
||||
|
||||
assert isinstance(tag, StructuralTag)
|
||||
assert "emit_command" in tag.model_dump_json()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("parser_cls", "model"),
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user