From b64fd988f02b8a4295193892b24b00bdf635a4dc Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 1 Jun 2026 09:30:15 -0700 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 28 ++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 52d90c082d..63d4d0a347 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -18,7 +18,7 @@ from uuid import uuid4 from aiocache import cached from fastapi import HTTPException, Request -from fastapi.responses import HTMLResponse +from fastapi.responses import HTMLResponse, JSONResponse from open_webui.config import ( CACHE_DIR, CODE_INTERPRETER_BLOCKED_MODULES, @@ -1332,7 +1332,8 @@ async def chat_completion_tools_handler( tool_function_name = tool_call.get('name', None) if tool_function_name not in tools: - return body, {} + log.warning(f'Tool "{tool_function_name}" not found') + return tool_function_params = tool_call.get('parameters', {}) @@ -1520,6 +1521,18 @@ async def chat_web_search_handler(request: Request, form_data: dict, extra_param user, ) + # generate_queries returns a JSONResponse on error (e.g. model not + # found, chat completion failure). Extract the error detail and + # re-raise so the outer except block falls back to using the raw + # user message as the search query. + if isinstance(res, JSONResponse): + try: + error_body = json.loads(res.body) + detail = error_body.get('detail', 'Query generation failed') + except Exception: + detail = 'Query generation failed' + raise Exception(detail) + response = res['choices'][0]['message']['content'] try: @@ -1851,6 +1864,15 @@ async def chat_image_generation_handler(request: Request, form_data: dict, extra user, ) + # Handle JSONResponse from error paths + if isinstance(res, JSONResponse): + try: + error_body = json.loads(res.body) + detail = error_body.get('detail', 'Image prompt generation failed') + except Exception: + detail = 'Image prompt generation failed' + raise Exception(detail) + response = res['choices'][0]['message']['content'] try: @@ -4577,6 +4599,8 @@ async def streaming_chat_response_handler(response, ctx): except Exception as e: tool_result = str(e) + else: + tool_result = f'Error: Tool "{tool_function_name}" not found.' tool_result, tool_result_files, tool_result_embeds = await process_tool_result( request,