diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index e6c561a8ca..c03e689ff2 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -400,9 +400,6 @@ def get_citation_source_from_tool_result( Returns a list of sources (usually one, but query_knowledge_files/query_chat_files may return multiple). """ - _EXPECTS_LIST = {'search_web', 'query_knowledge_files', 'query_chat_files'} - _EXPECTS_DICT = {'view_knowledge_file', 'view_file'} - try: try: tool_result = JSONCodec.loads(tool_result) @@ -411,41 +408,10 @@ 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 in ('view_knowledge_file', 'view_file'): + if not isinstance(tool_result, dict): + return [] - if tool_name == 'search_web': - # Parse JSON array: [{"title": "...", "link": "...", "snippet": "..."}] - results = tool_result - documents = [] - metadata = [] - - for result in results: - title = result.get('title', '') - link = result.get('link', '') - snippet = result.get('snippet', '') - - documents.append(f'{title}\n{snippet}') - metadata.append( - { - 'source': link, - 'name': title, - 'url': link, - } - ) - - return [ - { - 'source': {'name': 'search_web', 'id': 'search_web'}, - 'document': documents, - 'metadata': metadata, - } - ] - - elif tool_name in ('view_knowledge_file', 'view_file'): file_data = tool_result filename = file_data.get('filename', 'Unknown File') file_id = file_data.get('id', '') @@ -490,6 +456,9 @@ def get_citation_source_from_tool_result( ] elif tool_name in ('query_knowledge_files', 'query_chat_files'): + if not isinstance(tool_result, list): + return [] + chunks = tool_result # Group chunks by source for better citation display @@ -5772,7 +5741,6 @@ async def streaming_chat_response_handler(response, ctx): citations_enabled and tool_function_name in [ - 'search_web', 'fetch_url', 'view_file', 'view_knowledge_file',