Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ async def check_semantic_cache(request: Request) -> Optional[JSONResponse]:
return None

# Get the request body
body = await request.json()
try:
body = await request.body()
body = json.loads(body) if body else {}
except (json.JSONDecodeError, UnicodeDecodeError):
logger.warning("Failed to parse request body in semantic cache check, skipping")
return None
logger.info("Checking semantic cache for potential cache hit")

# Check if semantic cache is initialized
Expand Down
26 changes: 22 additions & 4 deletions src/vllm_router/services/request_service/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,16 @@ async def process_request(
request_json = json.loads(body)
is_streaming = request_json.get("stream", False)
model_name = request_json.get("model", "unknown")
except (JSONDecodeError, UnicodeDecodeError, ValueError):
except (json.JSONDecodeError, UnicodeDecodeError, ValueError) as e:
# If we can't parse the body as JSON, assume it's not streaming
raise HTTPException(status=400, detail="Request body is not JSON parsable.")
logger.warning(
f"Failed to parse request body in process_request: {e}. "
f"Body length: {len(body)} bytes"
)
raise HTTPException(
status_code=400,
detail=f"Request body is not JSON parsable: {e}",
)
Comment on lines +292 to +295

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

fastapi.HTTPException does not accept a status parameter; it expects status_code. Using status=400 will raise a TypeError at runtime. Change it to status_code=400.

Suggested change
raise HTTPException(
status=400,
detail=f"Request body is not JSON parsable: {e}",
)
raise HTTPException(
status_code=400,
detail=f"Request body is not JSON parsable: {e}",
)


# Add streaming info to span after parsing
if span is not None:
Expand Down Expand Up @@ -418,10 +425,21 @@ async def route_general_request(
request_body = await request.body()
try:
request_json = json.loads(request_body) if request_body else {}
except (json.JSONDecodeError, UnicodeDecodeError, RecursionError):
except (json.JSONDecodeError, UnicodeDecodeError, RecursionError) as e:
content_length = len(request_body)
expected_length = request.headers.get("content-length")
logger.warning(
f"Failed to parse request body as JSON for request {request_id}: "
f"{e}. Body length: {content_length} bytes, "
f"Content-Length header: {expected_length}"
)
return JSONResponse(
status_code=400,
content={"error": "Invalid request: request body must be valid JSON."},
content={
"error": "Invalid request: request body must be valid JSON.",
"detail": str(e),
"body_length": content_length,
},
headers={"X-Request-Id": request_id},
)

Expand Down
Loading