From d04163b3fa73e62428ec49f27a43d3e42629c62c Mon Sep 17 00:00:00 2001 From: Chang Su Date: Mon, 23 Jun 2025 20:35:11 -0700 Subject: [PATCH] Fix RequestValidationError response format (#7487) --- python/sglang/srt/entrypoints/http_server.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/entrypoints/http_server.py b/python/sglang/srt/entrypoints/http_server.py index b3e646147..daa8999b7 100644 --- a/python/sglang/srt/entrypoints/http_server.py +++ b/python/sglang/srt/entrypoints/http_server.py @@ -52,6 +52,7 @@ from sglang.srt.entrypoints.openai.protocol import ( ChatCompletionRequest, CompletionRequest, EmbeddingRequest, + ErrorResponse, ModelCard, ModelList, ScoringRequest, @@ -172,12 +173,23 @@ app.add_middleware( @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): """Override FastAPI's default 422 validation error with 400""" + exc_str = str(exc) + errors_str = str(exc.errors()) + + if errors_str and errors_str != exc_str: + message = f"{exc_str} {errors_str}" + else: + message = exc_str + + err = ErrorResponse( + message=message, + type=HTTPStatus.BAD_REQUEST.phrase, + code=HTTPStatus.BAD_REQUEST.value, + ) + return ORJSONResponse( status_code=400, - content={ - "detail": exc.errors(), - "body": exc.body, - }, + content=err.model_dump(), )