Fix RequestValidationError response format (#7487)
This commit is contained in:
@@ -52,6 +52,7 @@ from sglang.srt.entrypoints.openai.protocol import (
|
|||||||
ChatCompletionRequest,
|
ChatCompletionRequest,
|
||||||
CompletionRequest,
|
CompletionRequest,
|
||||||
EmbeddingRequest,
|
EmbeddingRequest,
|
||||||
|
ErrorResponse,
|
||||||
ModelCard,
|
ModelCard,
|
||||||
ModelList,
|
ModelList,
|
||||||
ScoringRequest,
|
ScoringRequest,
|
||||||
@@ -172,12 +173,23 @@ app.add_middleware(
|
|||||||
@app.exception_handler(RequestValidationError)
|
@app.exception_handler(RequestValidationError)
|
||||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||||
"""Override FastAPI's default 422 validation error with 400"""
|
"""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(
|
return ORJSONResponse(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
content={
|
content=err.model_dump(),
|
||||||
"detail": exc.errors(),
|
|
||||||
"body": exc.body,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user