| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Regression test: calling ``/tokenize`` with multimodal data followed by |
| 4 | ``/v1/chat/completions`` with the same data must not cause an error. |
| 5 | |
| 6 | Ensures that the ``/tokenize`` endpoint does not pollute internal caches |
| 7 | (e.g. multimodal feature caches) and that a subsequent |
| 8 | ``/v1/chat/completions`` request with the same multimodal payload |
| 9 | completes successfully. |
| 10 | """ |
| 11 | |
| 12 | import json |
| 13 | |
| 14 | import openai |
| 15 | import pytest |
| 16 | import pytest_asyncio |
| 17 | import requests |
| 18 | |
| 19 | from tests.utils import RemoteOpenAIServer |
| 20 | |
| 21 | MODEL_NAME = "Qwen/Qwen2.5-VL-3B-Instruct" |
| 22 | |
| 23 | |
| 24 | @pytest.fixture(scope="module") |
| 25 | def server(): |
| 26 | args = [ |
| 27 | "--dtype", |
| 28 | "bfloat16", |
| 29 | "--max-model-len", |
| 30 | "4096", |
| 31 | "--max-num-seqs", |
| 32 | "5", |
| 33 | "--enforce-eager", |
| 34 | "--limit-mm-per-prompt", |
| 35 | json.dumps({"image": 1}), |
| 36 | ] |
| 37 | with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: |
| 38 | yield remote_server |
| 39 | |
| 40 | |
| 41 | @pytest_asyncio.fixture |
| 42 | async def client(server): |
| 43 | async with server.get_async_client() as async_client: |
| 44 | yield async_client |
| 45 | |
| 46 | |
| 47 | @pytest.mark.asyncio |
| 48 | async def test_tokenize_then_chat_completion_with_image( |
| 49 | client: openai.AsyncOpenAI, |
| 50 | server: RemoteOpenAIServer, |
| 51 | local_asset_server, |
| 52 | ): |
| 53 | """Tokenize a multimodal message, then send the same message to chat |
| 54 | completions. The chat completion must succeed (not 500).""" |
| 55 | image_url = local_asset_server.url_for("stop_sign.jpg") |
| 56 | messages = [ |
| 57 | { |
| 58 | "role": "user", |
| 59 | "content": [ |
| 60 | {"type": "image_url", "image_url": {"url": image_url}}, |
| 61 | {"type": "text", "text": "Describe this image briefly."}, |
| 62 | ], |
| 63 | } |
| 64 | ] |
| 65 | |
| 66 | tok_resp = requests.post( |
| 67 | server.url_for("tokenize"), |
| 68 | json={"model": MODEL_NAME, "messages": messages}, |
| 69 | ) |
| 70 | tok_resp.raise_for_status() |
| 71 | tok_data = tok_resp.json() |
| 72 | assert tok_data["count"] > 0, "Tokenization must return tokens" |
| 73 | |
| 74 | chat_completion = await client.chat.completions.create( |
| 75 | model=MODEL_NAME, |
| 76 | messages=messages, |
| 77 | max_tokens=10, |
| 78 | temperature=0.0, |
| 79 | ) |
| 80 | |
| 81 | assert chat_completion.choices[0].message.content, ( |
| 82 | "Chat completion must produce non-empty content after tokenize" |
| 83 | ) |