diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 76431db97f..a02e4327a4 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -2585,11 +2585,11 @@ class ChatTable: if not file_ids: return None - chat_message_file_ids = { - item.id for item in await self.get_chat_files_by_chat_id_and_message_id(chat_id, message_id, db=db) - } + async with get_async_db_context(db) as session: + result = await session.execute(select(ChatFile.file_id).filter_by(chat_id=chat_id)) + chat_file_ids = set(result.scalars().all()) # Remove duplicates and existing file_ids - file_ids = list({file_id for file_id in file_ids if file_id and file_id not in chat_message_file_ids}) + file_ids = list({file_id for file_id in file_ids if file_id and file_id not in chat_file_ids}) if not file_ids: return None