Skip to content

Commit c9bd9f0

Browse files
committed
pick opc-request-id middleware and handle_pydantic_validation_error
1 parent 3015d56 commit c9bd9f0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

vllm/entrypoints/openai/api_server.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,19 @@ def build_app(args: Namespace) -> FastAPI:
831831
allow_headers=args.allowed_headers,
832832
)
833833

834+
def _handle_pydantic_validation_error(exc: RequestValidationError):
835+
"""Temp util function to handle pydantic validation errors."""
836+
errors = exc.errors()
837+
filtered_errors = []
838+
for error in errors:
839+
if "msg" in error:
840+
filtered_errors.append(error["msg"])
841+
842+
return filtered_errors or exc
843+
834844
@app.exception_handler(RequestValidationError)
835845
async def validation_exception_handler(_, exc):
846+
exc = _handle_pydantic_validation_error(exc)
836847
err = ErrorResponse(message=str(exc),
837848
type="BadRequestError",
838849
code=HTTPStatus.BAD_REQUEST)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
3+
from fastapi import Request
4+
from vllm.logger import init_logger
5+
6+
logger = init_logger(__name__)
7+
8+
9+
async def log_opc_header(request: Request, call_next):
10+
# Log at the start and end of a POST request
11+
if request.method == "POST":
12+
opc_request_id = request.headers.get("opc-request-id", "unknown")
13+
logger.info(f"POST Request Start - opc-request-id: {opc_request_id}")
14+
15+
try:
16+
response = await call_next(request)
17+
logger.info(
18+
f"POST Request End - opc-request-id: {opc_request_id}, "
19+
f"status_code: {response.status_code}")
20+
return response
21+
except Exception as e:
22+
logger.error(
23+
f"Exception during POST request with "
24+
f"opc-request-id: {opc_request_id}, error: {e}")
25+
raise
26+
27+
# For non-POST requests, just pass through
28+
return await call_next(request)

0 commit comments

Comments
 (0)