From 74a25bd51d15eec71530926f457b1c3e5d1f3d4c Mon Sep 17 00:00:00 2001 From: adarshkumar23 <131923092+adarshkumar23@users.noreply.github.com> Date: Mon, 30 Mar 2026 16:36:13 +0000 Subject: [PATCH] fix: add input validation for empty chat messages Empty or whitespace-only messages sent to /chat would be forwarded to the LLM, wasting compute and returning meaningless responses. Add Pydantic validator to strip whitespace and reject empty messages with a clear 422 error before they reach the inference pipeline. Signed-off-by: Adarsh Kumar Signed-off-by: adarshkumar23 <131923092+adarshkumar23@users.noreply.github.com> --- server-https/app.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server-https/app.py b/server-https/app.py index 694af8a..83630e6 100644 --- a/server-https/app.py +++ b/server-https/app.py @@ -4,7 +4,7 @@ from fastapi import FastAPI, HTTPException from fastapi.responses import StreamingResponse from fastapi.middleware.cors import CORSMiddleware -from pydantic import BaseModel +from pydantic import BaseModel, validator import uvicorn from typing import Dict, Any, List, Optional, AsyncGenerator from sentence_transformers import SentenceTransformer @@ -111,6 +111,12 @@ class ChatRequest(BaseModel): message: str stream: Optional[bool] = True + @validator("message") + def message_must_not_be_empty(cls, v): + if not v or not v.strip(): + raise ValueError("message must not be empty or whitespace-only") + return v.strip() + def milvus_search(query: str, top_k: int = 5) -> Dict[str, Any]: """Execute a semantic search in Milvus and return structured JSON serializable results.""" try: