diff --git a/tests/entrypoints/test_utils.py b/tests/entrypoints/test_utils.py new file mode 100644 index 000000000..dc1101840 --- /dev/null +++ b/tests/entrypoints/test_utils.py @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project +from vllm.entrypoints.utils import sanitize_message + + +def test_sanitize_message(): + assert ( + sanitize_message("<_io.BytesIO object at 0x7a95e299e750>") + == "<_io.BytesIO object>" + ) diff --git a/vllm/entrypoints/openai/api_server.py b/vllm/entrypoints/openai/api_server.py index b489eca5f..6cf90cea9 100644 --- a/vllm/entrypoints/openai/api_server.py +++ b/vllm/entrypoints/openai/api_server.py @@ -88,8 +88,10 @@ from vllm.entrypoints.utils import ( log_non_default_args, process_chat_template, process_lora_modules, + sanitize_message, with_cancellation, ) +from vllm.exceptions import VLLMValidationError from vllm.logger import init_logger from vllm.reasoning import ReasoningParserManager from vllm.tasks import POOLING_TASKS @@ -902,7 +904,7 @@ def build_app(args: Namespace) -> FastAPI: async def http_exception_handler(_: Request, exc: HTTPException): err = ErrorResponse( error=ErrorInfo( - message=exc.detail, + message=sanitize_message(exc.detail), type=HTTPStatus(exc.status_code).phrase, code=exc.status_code, ) @@ -911,8 +913,6 @@ def build_app(args: Namespace) -> FastAPI: @app.exception_handler(RequestValidationError) async def validation_exception_handler(_: Request, exc: RequestValidationError): - from vllm.exceptions import VLLMValidationError - param = None for error in exc.errors(): if "ctx" in error and "error" in error["ctx"]: @@ -931,7 +931,7 @@ def build_app(args: Namespace) -> FastAPI: err = ErrorResponse( error=ErrorInfo( - message=message, + message=sanitize_message(message), type=HTTPStatus.BAD_REQUEST.phrase, code=HTTPStatus.BAD_REQUEST, param=param, diff --git a/vllm/entrypoints/utils.py b/vllm/entrypoints/utils.py index f4a633c69..6f3e7c55f 100644 --- a/vllm/entrypoints/utils.py +++ b/vllm/entrypoints/utils.py @@ -9,6 +9,7 @@ from argparse import Namespace from pathlib import Path from typing import Any +import regex as re from fastapi import Request from fastapi.responses import JSONResponse, StreamingResponse from starlette.background import BackgroundTask, BackgroundTasks @@ -317,3 +318,8 @@ async def process_chat_template( model_config.model, ) return resolved_chat_template + + +def sanitize_message(message: str) -> str: + # Avoid leaking memory address from object reprs + return re.sub(r" at 0x[0-9a-f]+>", ">", message)