diff --git a/backend/open_webui/retrieval/utils.py b/backend/open_webui/retrieval/utils.py index b1aec78656..14a64fed60 100644 --- a/backend/open_webui/retrieval/utils.py +++ b/backend/open_webui/retrieval/utils.py @@ -566,6 +566,13 @@ async def query_collection( log.exception(f'Error when querying the collection: {e}') return None, e + # Sanitize: filter out None/empty queries to prevent embedding crashes + # (e.g. when get_last_user_message returns None) + queries = [q for q in queries if q] + if not queries: + log.warning('query_collection: all queries were None or empty, returning empty results') + return {'distances': [[]], 'documents': [[]], 'metadatas': [[]]} + # Generate all query embeddings (in one call) query_embeddings = await embedding_function(queries, prefix=RAG_EMBEDDING_QUERY_PREFIX) log.debug(f'query_collection: processing {len(queries)} queries across {len(collection_names)} collections') diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 25d6a2cecb..33d6bfa91e 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -238,9 +238,13 @@ async def fetch_url( content, _ = await asyncio.to_thread(get_content_from_url, __request__, url) # Truncate if configured (WEB_FETCH_MAX_CONTENT_LENGTH) - max_length = getattr(__request__.app.state.config, 'WEB_FETCH_MAX_CONTENT_LENGTH', None) - if max_length and max_length > 0 and len(content) > max_length: - content = content[:max_length] + '\n\n[Content truncated...]' + # Guard: content may be None if the web loader silently failed + if content is not None: + max_length = getattr(__request__.app.state.config, 'WEB_FETCH_MAX_CONTENT_LENGTH', None) + if max_length and max_length > 0 and len(content) > max_length: + content = content[:max_length] + '\n\n[Content truncated...]' + else: + content = '' return content except Exception as e: diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 3fd4d011df..ab2ca104f4 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1546,11 +1546,11 @@ async def chat_web_search_handler(request: Request, form_data: dict, extra_param except Exception as e: log.exception(e) - queries = [user_message] + queries = [user_message or ''] # Check if generated queries are empty if len(queries) == 1 and queries[0].strip() == '': - queries = [user_message] + queries = [user_message or ''] # Check if queries are not found if len(queries) == 0: @@ -1991,7 +1991,7 @@ async def chat_completion_files_handler( ) if len(queries) == 0: - queries = [get_last_user_message(body['messages'])] + queries = [get_last_user_message(body['messages']) or ''] try: # Directly await async get_sources_from_items (no thread needed - fully async now)