From 0a1f9966efc6d5dbfeeef9a4caf522a8e530271e Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 6 Jul 2025 14:20:38 +0400 Subject: [PATCH] refac: audio error handling --- backend/open_webui/routers/audio.py | 34 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/backend/open_webui/routers/audio.py b/backend/open_webui/routers/audio.py index 211f2ae859..cac9c9e5a2 100644 --- a/backend/open_webui/routers/audio.py +++ b/backend/open_webui/routers/audio.py @@ -328,6 +328,7 @@ async def speech(request: Request, user=Depends(get_verified_user)): log.exception(e) raise HTTPException(status_code=400, detail="Invalid JSON payload") + r = None if request.app.state.config.TTS_ENGINE == "openai": payload["model"] = request.app.state.config.TTS_MODEL @@ -336,7 +337,7 @@ async def speech(request: Request, user=Depends(get_verified_user)): async with aiohttp.ClientSession( timeout=timeout, trust_env=True ) as session: - async with session.post( + r = await session.post( url=f"{request.app.state.config.TTS_OPENAI_API_BASE_URL}/audio/speech", json=payload, headers={ @@ -354,14 +355,15 @@ async def speech(request: Request, user=Depends(get_verified_user)): ), }, ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - r.raise_for_status() + ) - async with aiofiles.open(file_path, "wb") as f: - await f.write(await r.read()) + r.raise_for_status() - async with aiofiles.open(file_body_path, "w") as f: - await f.write(json.dumps(payload)) + async with aiofiles.open(file_path, "wb") as f: + await f.write(await r.read()) + + async with aiofiles.open(file_body_path, "w") as f: + await f.write(json.dumps(payload)) return FileResponse(file_path) @@ -369,18 +371,18 @@ async def speech(request: Request, user=Depends(get_verified_user)): log.exception(e) detail = None - try: - if r.status != 200: - res = await r.json() + status_code = 500 + detail = f"Open WebUI: Server Connection Error" - if "error" in res: - detail = f"External: {res['error'].get('message', '')}" - except Exception: - detail = f"External: {e}" + if r is not None: + status_code = r.status + res = await r.json() + if "error" in res: + detail = f"External: {res['error'].get('message', '')}" raise HTTPException( - status_code=getattr(r, "status", 500) if r else 500, - detail=detail if detail else "Open WebUI: Server Connection Error", + status_code=status_code, + detail=detail, ) elif request.app.state.config.TTS_ENGINE == "elevenlabs":