[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:
@@ -1,13 +1,17 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import pytest
|
||||
from openai_harmony import (
|
||||
Message,
|
||||
)
|
||||
from pydantic import ValidationError
|
||||
|
||||
from vllm.entrypoints.openai.responses.protocol import (
|
||||
ResponsesRequest,
|
||||
serialize_message,
|
||||
serialize_messages,
|
||||
)
|
||||
from vllm.exceptions import VLLMValidationError
|
||||
|
||||
|
||||
def test_serialize_message() -> None:
|
||||
@@ -37,3 +41,34 @@ def test_serialize_messages() -> None:
|
||||
}
|
||||
msg = Message.from_dict(msg_value)
|
||||
assert serialize_messages([msg, dict_value]) == [msg_value, dict_value]
|
||||
|
||||
|
||||
def test_custom_tool_grammar_format_rejected() -> None:
|
||||
grammar = {"type": "grammar", "syntax": "lark", "definition": 'start: "pwd"'}
|
||||
with pytest.raises(VLLMValidationError) as exc_info:
|
||||
ResponsesRequest(
|
||||
input="hi", tools=[{"type": "custom", "name": "emit", "format": grammar}]
|
||||
)
|
||||
assert exc_info.value.parameter == "tools"
|
||||
|
||||
request = ResponsesRequest(
|
||||
input="hi",
|
||||
tools=[{"type": "custom", "name": "emit", "format": {"type": "text"}}],
|
||||
)
|
||||
assert request.tools is not None and request.tools[0].type == "custom"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"tools",
|
||||
[
|
||||
"abc",
|
||||
{"type": "custom"},
|
||||
[None],
|
||||
[{"type": "custom", "name": "x", "format": "text"}],
|
||||
],
|
||||
)
|
||||
def test_malformed_tools_rejected_by_field_validation(tools) -> None:
|
||||
"""Malformed tools bodies fail pydantic field validation (4xx), not an
|
||||
AttributeError inside the custom-format validator (500)."""
|
||||
with pytest.raises(ValidationError):
|
||||
ResponsesRequest(input="hi", tools=tools)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from openai.types.responses import CustomTool, FunctionTool, ResponseCustomToolCall
|
||||
from openai.types.responses.response_function_tool_call import ResponseFunctionToolCall
|
||||
from openai.types.responses.response_function_tool_call_output_item import (
|
||||
ResponseFunctionToolCallOutputItem,
|
||||
@@ -16,10 +17,14 @@ from openai.types.responses.response_reasoning_item import (
|
||||
Summary,
|
||||
)
|
||||
|
||||
from vllm.entrypoints.generate.base.protocol import FunctionCall
|
||||
from vllm.entrypoints.openai.responses.utils import (
|
||||
_construct_message_from_response_item,
|
||||
build_response_output_items,
|
||||
construct_chat_messages_with_tool_call,
|
||||
construct_input_messages,
|
||||
decode_custom_tool_input,
|
||||
decode_custom_tool_input_prefix,
|
||||
should_continue_final_message,
|
||||
)
|
||||
from vllm.exceptions import VLLMValidationError
|
||||
@@ -248,6 +253,89 @@ class TestResponsesUtils:
|
||||
assert formatted_item["content"] == "dongyi"
|
||||
|
||||
|
||||
class TestCustomTools:
|
||||
def test_custom_tool_call_output_item(self):
|
||||
tools = [
|
||||
CustomTool(type="custom", name="emit_command", format={"type": "text"}),
|
||||
FunctionTool(
|
||||
type="function", name="get_weather", parameters={}, strict=None
|
||||
),
|
||||
]
|
||||
custom, function = build_response_output_items(
|
||||
reasoning=None,
|
||||
content=None,
|
||||
tool_calls=[
|
||||
FunctionCall(
|
||||
id="call_1", name="emit_command", arguments='{"input": "pwd"}'
|
||||
),
|
||||
FunctionCall(
|
||||
id="call_2", name="get_weather", arguments='{"city": "Paris"}'
|
||||
),
|
||||
],
|
||||
tools=tools,
|
||||
)
|
||||
assert isinstance(custom, ResponseCustomToolCall)
|
||||
assert (custom.call_id, custom.input) == ("call_1", "pwd")
|
||||
assert custom.id.startswith("ctc_")
|
||||
assert isinstance(function, ResponseFunctionToolCall)
|
||||
assert function.arguments == '{"city": "Paris"}'
|
||||
|
||||
def test_custom_tool_history_replays_through_function_shim(self):
|
||||
messages = construct_chat_messages_with_tool_call(
|
||||
[
|
||||
make_reasoning_item(content_text="Run pwd."),
|
||||
ResponseCustomToolCall(
|
||||
type="custom_tool_call",
|
||||
id="ctc_1",
|
||||
call_id="call_exec",
|
||||
name="emit_command",
|
||||
input='echo "hi"',
|
||||
),
|
||||
{
|
||||
"type": "custom_tool_call_output",
|
||||
"call_id": "call_exec",
|
||||
"output": "hi",
|
||||
},
|
||||
{"role": "user", "content": "Continue"},
|
||||
]
|
||||
)
|
||||
assert messages == [
|
||||
{
|
||||
"role": "assistant",
|
||||
"reasoning": "Run pwd.",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_exec",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "emit_command",
|
||||
"arguments": '{"input": "echo \\"hi\\""}',
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "content": "hi", "tool_call_id": "call_exec"},
|
||||
{"role": "user", "content": "Continue"},
|
||||
]
|
||||
|
||||
def test_decode_custom_tool_input(self):
|
||||
assert decode_custom_tool_input('{"input": "pwd"}') == "pwd"
|
||||
assert decode_custom_tool_input('{"cmd": "pwd"}') == '{"cmd": "pwd"}'
|
||||
assert decode_custom_tool_input('{"file": "a.py"}') == '{"file": "a.py"}'
|
||||
assert decode_custom_tool_input('{"input": 7}') == '{"input": 7}'
|
||||
assert decode_custom_tool_input("pwd") == "pwd"
|
||||
|
||||
def test_decode_custom_tool_input_prefix(self):
|
||||
assert decode_custom_tool_input_prefix('{"inp') == ""
|
||||
assert decode_custom_tool_input_prefix('{"input": "ls \\"dir') == 'ls "dir'
|
||||
assert decode_custom_tool_input_prefix('{"input": "a\\') == "a"
|
||||
assert decode_custom_tool_input_prefix('{"input": "\\ud83d') == ""
|
||||
assert (
|
||||
decode_custom_tool_input_prefix('{"input": "\\ud83d\\ude00!"}')
|
||||
== "\U0001f600!"
|
||||
)
|
||||
|
||||
|
||||
class TestReasoningItemContentPriority:
|
||||
"""Tests that content is prioritized over summary for reasoning items."""
|
||||
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import pytest
|
||||
from openai.types.responses import ResponseFunctionWebSearch, ResponseStreamEvent
|
||||
from openai.types.responses import (
|
||||
CustomTool,
|
||||
ResponseCustomToolCall,
|
||||
ResponseFunctionWebSearch,
|
||||
ResponseStreamEvent,
|
||||
)
|
||||
from openai_harmony import Message, Role
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
@@ -20,6 +25,7 @@ from vllm.entrypoints.openai.responses.streaming_events import (
|
||||
emit_reasoning_done_events,
|
||||
split_delta,
|
||||
)
|
||||
from vllm.entrypoints.openai.responses.utils import decode_custom_tool_input
|
||||
|
||||
|
||||
def test_browser_find_uses_responses_action_type():
|
||||
@@ -190,3 +196,111 @@ class TestProcessorCompoundDeltas:
|
||||
types = [e.type for e in events]
|
||||
assert "response.reasoning_text.delta" in types
|
||||
assert "response.output_text.delta" in types
|
||||
|
||||
|
||||
class TestSimpleToolCallShapes:
|
||||
def test_arguments_done_emitted_without_deltas(self):
|
||||
processor = SimpleStreamingEventProcessor()
|
||||
events = _run_through_processor(
|
||||
processor, DeltaMessage(tool_calls=[_make_tool_call(0, name="noop")])
|
||||
)
|
||||
events += processor.close_current()
|
||||
assert [e.type for e in events] == [
|
||||
"response.output_item.added",
|
||||
"response.function_call_arguments.done",
|
||||
"response.output_item.done",
|
||||
]
|
||||
|
||||
|
||||
class TestCustomToolStreaming:
|
||||
def test_custom_tool_input_events(self):
|
||||
processor = SimpleStreamingEventProcessor(
|
||||
tools=[CustomTool(type="custom", name="emit_command")]
|
||||
)
|
||||
events = _run_through_processor(
|
||||
processor,
|
||||
DeltaMessage(
|
||||
tool_calls=[
|
||||
_make_tool_call(0, name="emit_command", arguments='{"input": "ls ')
|
||||
]
|
||||
),
|
||||
)
|
||||
events += _run_through_processor(
|
||||
processor,
|
||||
DeltaMessage(tool_calls=[_make_tool_call(0, arguments='-la \\"x\\""}')]),
|
||||
)
|
||||
events += processor.close_current()
|
||||
|
||||
assert [e.type for e in events] == [
|
||||
"response.output_item.added",
|
||||
"response.custom_tool_call_input.delta",
|
||||
"response.custom_tool_call_input.delta",
|
||||
"response.custom_tool_call_input.done",
|
||||
"response.output_item.done",
|
||||
]
|
||||
done = events[-1].item
|
||||
assert isinstance(done, ResponseCustomToolCall)
|
||||
assert done.input == 'ls -la "x"'
|
||||
assert "".join(e.delta for e in events[1:3]) == done.input
|
||||
assert events[3].input == done.input
|
||||
assert events[0].item.type == "custom_tool_call"
|
||||
assert events[0].item.id == done.id
|
||||
assert done.id.startswith("ctc_")
|
||||
assert events[0].item.call_id == done.call_id
|
||||
|
||||
def test_unrelated_keys_are_not_streamed_as_payload(self):
|
||||
"""Review repro: chunks of an object whose input key arrives last must
|
||||
not leak fragments of other keys into the payload."""
|
||||
processor = SimpleStreamingEventProcessor(
|
||||
tools=[CustomTool(type="custom", name="emit_command")]
|
||||
)
|
||||
events = _run_through_processor(
|
||||
processor,
|
||||
DeltaMessage(
|
||||
tool_calls=[
|
||||
_make_tool_call(0, name="emit_command", arguments='{"a": "xy"')
|
||||
]
|
||||
),
|
||||
)
|
||||
events += _run_through_processor(
|
||||
processor,
|
||||
DeltaMessage(
|
||||
tool_calls=[_make_tool_call(0, arguments=', "input": "hello"}')]
|
||||
),
|
||||
)
|
||||
events += processor.close_current()
|
||||
|
||||
deltas = [
|
||||
e for e in events if e.type == "response.custom_tool_call_input.delta"
|
||||
]
|
||||
done_input = next(
|
||||
e for e in events if e.type == "response.custom_tool_call_input.done"
|
||||
).input
|
||||
item = events[-1].item
|
||||
assert "".join(e.delta for e in deltas) == "hello" # "xy" never leaks
|
||||
assert done_input == "hello"
|
||||
assert item.input == "hello"
|
||||
assert decode_custom_tool_input('{"a": "xy", "input": "hello"}') == "hello"
|
||||
|
||||
def test_done_reported_authoritative_payload_on_mismatch(self):
|
||||
"""If the final decode is not an extension of what was streamed, the
|
||||
done event and item carry the authoritative value and a correcting
|
||||
delta is emitted rather than dropping the divergence."""
|
||||
processor = SimpleStreamingEventProcessor(
|
||||
tools=[CustomTool(type="custom", name="emit_command")]
|
||||
)
|
||||
processor.state.current_state = _StateType.TOOL_CALL
|
||||
processor.state.tool_call_is_custom = True
|
||||
processor.state.tool_call_payload = "xy" # stale fragment already streamed
|
||||
processor.state.accumulated_text = '{"a": "xy", "input": "hello"}'
|
||||
|
||||
events = processor.close_current()
|
||||
delta = next(
|
||||
e for e in events if e.type == "response.custom_tool_call_input.delta"
|
||||
)
|
||||
done_input = next(
|
||||
e for e in events if e.type == "response.custom_tool_call_input.done"
|
||||
).input
|
||||
assert delta.delta == "hello" # full authoritative resend, not a dropped suffix
|
||||
assert done_input == "hello"
|
||||
assert events[-1].item.input == "hello"
|
||||
|
||||
Reference in New Issue
Block a user