From 7cdff6b1e25692a27cc1c2a1896cb64f3b6071aa Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 7 Mar 2026 17:24:17 -0600 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index b5cf97d713..1f2718ae7f 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -173,6 +173,9 @@ def get_citation_source_from_tool_result( Returns a list of sources (usually one, but query_knowledge_files may return multiple). """ + _EXPECTS_LIST = {"search_web", "query_knowledge_files"} + _EXPECTS_DICT = {"view_knowledge_file"} + try: try: tool_result = json.loads(tool_result) @@ -181,6 +184,12 @@ def get_citation_source_from_tool_result( if isinstance(tool_result, dict) and "error" in tool_result: return [] + # Validate tool_result type based on what the branch expects + if tool_name in _EXPECTS_LIST and not isinstance(tool_result, list): + return [] + elif tool_name in _EXPECTS_DICT and not isinstance(tool_result, dict): + return [] + if tool_name == "search_web": # Parse JSON array: [{"title": "...", "link": "...", "snippet": "..."}] results = tool_result