| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | """Non-object JSON bodies must fail validation cleanly (4xx), not AttributeError (500). |
| 5 | |
| 6 | mode=before validators that call data.get(...) without an isinstance(data, dict) |
| 7 | guard raise AttributeError for string/list/scalar bodies and surface as HTTP 500. |
| 8 | """ |
| 9 | |
| 10 | import pytest |
| 11 | from pydantic import ValidationError |
| 12 | |
| 13 | from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest |
| 14 | from vllm.exceptions import VLLMValidationError |
| 15 | |
| 16 | |
| 17 | @pytest.mark.parametrize( |
| 18 | "payload", |
| 19 | [ |
| 20 | "this is not valid json{{{", |
| 21 | ["not", "an", "object"], |
| 22 | 42, |
| 23 | None, |
| 24 | ], |
| 25 | ) |
| 26 | def test_chat_completion_request_rejects_non_object_body(payload): |
| 27 | with pytest.raises(ValidationError): |
| 28 | ChatCompletionRequest.model_validate(payload) |
| 29 | |
| 30 | |
| 31 | def test_chat_completion_request_cache_salt_still_validated_on_dict(): |
| 32 | with pytest.raises(VLLMValidationError, match="cache_salt"): |
| 33 | ChatCompletionRequest.model_validate( |
| 34 | { |
| 35 | "model": "qwen", |
| 36 | "messages": [{"role": "user", "content": "hello"}], |
| 37 | "cache_salt": "", |
| 38 | } |
| 39 | ) |