diff --git a/backend/open_webui/routers/ollama.py b/backend/open_webui/routers/ollama.py index ea46a1cca..346d49607 100644 --- a/backend/open_webui/routers/ollama.py +++ b/backend/open_webui/routers/ollama.py @@ -147,8 +147,23 @@ async def send_post_request( }, ssl=AIOHTTP_CLIENT_SESSION_SSL, ) - r.raise_for_status() + if r.ok is False: + try: + res = await r.json() + if "error" in res: + log.error(f"Error from server: {res['error']}") + raise HTTPException(status_code=r.status, detail=res["error"]) + except HTTPException as e: + raise e # Re-raise HTTPException to be handled by FastAPI + except Exception as e: + log.error(f"Failed to parse error response: {e}") + raise HTTPException( + status_code=r.status, + detail=f"Open WebUI: Server Connection Error", + ) + + r.raise_for_status() # Raises an error for bad responses (4xx, 5xx) if stream: response_headers = dict(r.headers) @@ -168,20 +183,14 @@ async def send_post_request( await cleanup_response(r, session) return res + except HTTPException as e: + raise e # Re-raise HTTPException to be handled by FastAPI except Exception as e: - detail = None - - if r is not None: - try: - res = await r.json() - if "error" in res: - detail = f"Ollama: {res.get('error', 'Unknown error')}" - except Exception: - detail = f"Ollama: {e}" + detail = f"Ollama: {e}" raise HTTPException( status_code=r.status if r else 500, - detail=detail if detail else "Open WebUI: Server Connection Error", + detail=detail if e else "Open WebUI: Server Connection Error", )