Files
vLLM/tests/entrypoints/openai/responses/test_protocol.py
T

75 lines
2.2 KiB
Python

# 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:
dict_value = {"a": 1, "b": "2"}
assert serialize_message(dict_value) == dict_value
msg_value = {
"role": "assistant",
"name": None,
"content": [{"type": "text", "text": "Test 1"}],
"channel": "analysis",
}
msg = Message.from_dict(msg_value)
assert serialize_message(msg) == msg_value
def test_serialize_messages() -> None:
assert serialize_messages(None) is None
assert serialize_messages([]) is None
dict_value = {"a": 3, "b": "4"}
msg_value = {
"role": "assistant",
"name": None,
"content": [{"type": "text", "text": "Test 2"}],
"channel": "analysis",
}
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)