Fix: use pydantic pattern; handle httpx RequestError in gateway and qdrant

This commit is contained in:
2026-03-21 10:07:20 +01:00
parent e6da9924ff
commit ecf8c9a401
3 changed files with 25 additions and 7 deletions

View File

@@ -39,7 +39,7 @@ class AnalyzeRequest(BaseModel):
class EmbedRequest(BaseModel):
url: Optional[str] = None
backend: Optional[str] = Field(default="openclip", regex="^(openclip|hf)$")
backend: Optional[str] = Field(default="openclip", pattern="^(openclip|hf)$")
model: Optional[str] = None
pretrained: Optional[str] = None

View File

@@ -59,7 +59,10 @@ async def _get_health(client: httpx.AsyncClient, base: str) -> Dict[str, Any]:
async def _post_json(client: httpx.AsyncClient, url: str, payload: Dict[str, Any]) -> Dict[str, Any]:
r = await client.post(url, json=payload)
try:
r = await client.post(url, json=payload)
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Upstream request failed {url}: {str(e)}")
if r.status_code >= 400:
raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:1000]}")
try:
@@ -71,7 +74,10 @@ async def _post_json(client: httpx.AsyncClient, url: str, payload: Dict[str, Any
async def _post_file(client: httpx.AsyncClient, url: str, data: bytes, fields: Dict[str, Any]) -> Dict[str, Any]:
files = {"file": ("image", data, "application/octet-stream")}
r = await client.post(url, data={k: str(v) for k, v in fields.items()}, files=files)
try:
r = await client.post(url, data={k: str(v) for k, v in fields.items()}, files=files)
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Upstream request failed {url}: {str(e)}")
if r.status_code >= 400:
raise HTTPException(status_code=502, detail=f"Upstream error {url}: {r.status_code} {r.text[:1000]}")
try:

View File

@@ -109,20 +109,32 @@ def _col(name: Optional[str]) -> str:
async def _embed_url(url: str) -> List[float]:
"""Call the CLIP service to get an image embedding."""
async with httpx.AsyncClient(timeout=30) as http:
r = await http.post(f"{CLIP_URL}/embed", json={"url": url})
try:
r = await http.post(f"{CLIP_URL}/embed", json={"url": url})
except httpx.RequestError as e:
raise HTTPException(502, f"CLIP request failed: {str(e)}")
if r.status_code >= 400:
raise HTTPException(502, f"CLIP /embed error: {r.status_code} {r.text[:200]}")
return r.json()["vector"]
try:
return r.json()["vector"]
except Exception:
raise HTTPException(502, f"CLIP /embed returned non-JSON: {r.status_code} {r.text[:200]}")
async def _embed_bytes(data: bytes) -> List[float]:
"""Call the CLIP service to embed uploaded file bytes."""
async with httpx.AsyncClient(timeout=30) as http:
files = {"file": ("image", data, "application/octet-stream")}
r = await http.post(f"{CLIP_URL}/embed/file", files=files)
try:
r = await http.post(f"{CLIP_URL}/embed/file", files=files)
except httpx.RequestError as e:
raise HTTPException(502, f"CLIP request failed: {str(e)}")
if r.status_code >= 400:
raise HTTPException(502, f"CLIP /embed/file error: {r.status_code} {r.text[:200]}")
return r.json()["vector"]
try:
return r.json()["vector"]
except Exception:
raise HTTPException(502, f"CLIP /embed/file returned non-JSON: {r.status_code} {r.text[:200]}")
def _build_filter(metadata: Dict[str, Any]) -> Optional[Filter]: