~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix: deduplicate chunk IDs before FAISS indexing (#2182)
LearningCircuit0ee82418b338
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.pyindex b3077b0837..20d636e673 100644--- a/src/local_deep_research/research_library/services/library_rag_service.py+++ b/src/local_deep_research/research_library/services/library_rag_service.py@@ -214,6 +214,25 @@ class LibraryRAGService:cache_dir.mkdir(parents=True, exist_ok=True)return cache_dir / f"{index_hash}.faiss"+ @staticmethod+ def _deduplicate_chunks(+ chunks: List[LangchainDocument],+ chunk_ids: List[str],+ existing_ids: Optional[set] = None,+ ) -> tuple:+ """Deduplicate chunks by ID within a batch, optionally excluding existing IDs."""+ seen_ids: set = set()+ new_chunks: List[LangchainDocument] = []+ new_ids: List[str] = []+ for chunk, chunk_id in zip(chunks, chunk_ids):+ if chunk_id not in seen_ids and (+ existing_ids is None or chunk_id not in existing_ids+ ):+ new_chunks.append(chunk)+ new_ids.append(chunk_id)+ seen_ids.add(chunk_id)+ return new_chunks, new_ids+def _get_or_create_rag_index(self, collection_id: str) -> RAGIndex:"""Get or create RAGIndex record for the current configuration."""with get_user_db_session(self.username, self.db_password) as session:@@ -634,16 +653,11 @@ class LibraryRAGService:if hasattr(self.faiss_index, "docstore")else set())- new_chunks = []- new_ids = []- for chunk, chunk_id in zip(chunks, embedding_ids):- if chunk_id not in existing_ids:- new_chunks.append(chunk)- new_ids.append(chunk_id)else:- # force_reindex: add all chunks- new_chunks = chunks- new_ids = embedding_ids+ existing_ids = None+ new_chunks, new_ids = self._deduplicate_chunks(+ chunks, embedding_ids, existing_ids+ )# Add embeddings to FAISS indexif new_chunks:@@ -1303,16 +1317,11 @@ class LibraryRAGService:if hasattr(self.faiss_index, "docstore")else set())- new_chunks = []- new_ids = []- for chunk, chunk_id in zip(chunks, embedding_ids):- if chunk_id not in existing_ids:- new_chunks.append(chunk)- new_ids.append(chunk_id)else:- # force_reindex: add all chunks- new_chunks = chunks- new_ids = embedding_ids+ existing_ids = None+ new_chunks, new_ids = self._deduplicate_chunks(+ chunks, embedding_ids, existing_ids+ )# Add embeddings to FAISS indexif new_chunks:diff --git a/tests/research_library/services/test_library_rag_service.py b/tests/research_library/services/test_library_rag_service.pyindex 804fa2daf9..994cf30612 100644--- a/tests/research_library/services/test_library_rag_service.py+++ b/tests/research_library/services/test_library_rag_service.py@@ -1096,3 +1096,144 @@ class TestRemoveCollectionFromIndex:# Method should have docstring or be properly documentedmethod = LibraryRAGService.remove_collection_from_indexassert method is not None+++class TestDeduplicateChunks:+ """Tests for _deduplicate_chunks static method.++ This covers the bug fix where duplicate chunk IDs from+ _store_chunks_to_db() caused FAISS add_documents() to reject+ the batch with 'Duplicate ids found in the ids list'.+ """++ def test_no_duplicates_passes_all_through(self):+ """All chunks pass through when there are no duplicates."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="chunk 1"),+ LangchainDocument(page_content="chunk 2"),+ LangchainDocument(page_content="chunk 3"),+ ]+ ids = ["id-1", "id-2", "id-3"]++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids+ )++ assert len(result_chunks) == 3+ assert result_ids == ["id-1", "id-2", "id-3"]++ def test_batch_duplicates_are_removed(self):+ """Duplicate IDs within a batch keep only the first occurrence."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="header text"),+ LangchainDocument(page_content="unique content"),+ LangchainDocument(+ page_content="header text"+ ), # same hash -> same ID+ ]+ ids = ["id-dup", "id-unique", "id-dup"]++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids+ )++ assert len(result_chunks) == 2+ assert result_ids == ["id-dup", "id-unique"]+ assert result_chunks[0].page_content == "header text"+ assert result_chunks[1].page_content == "unique content"++ def test_existing_ids_are_excluded(self):+ """Chunks with IDs already in the FAISS index are filtered out."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="already indexed"),+ LangchainDocument(page_content="new content"),+ ]+ ids = ["id-old", "id-new"]+ existing = {"id-old"}++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids, existing_ids=existing+ )++ assert len(result_chunks) == 1+ assert result_ids == ["id-new"]++ def test_batch_duplicates_and_existing_ids_combined(self):+ """Both batch duplicates and existing IDs are filtered."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="old"),+ LangchainDocument(page_content="new A"),+ LangchainDocument(page_content="new A copy"), # dup of new A+ LangchainDocument(page_content="new B"),+ ]+ ids = ["id-old", "id-a", "id-a", "id-b"]+ existing = {"id-old"}++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids, existing_ids=existing+ )++ assert len(result_chunks) == 2+ assert result_ids == ["id-a", "id-b"]++ def test_all_duplicates_returns_empty(self):+ """Returns empty lists when all chunks are duplicates."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="same"),+ LangchainDocument(page_content="same"),+ ]+ ids = ["id-x", "id-x"]+ existing = {"id-x"}++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids, existing_ids=existing+ )++ assert len(result_chunks) == 0+ assert len(result_ids) == 0++ def test_none_existing_ids_skips_existing_check(self):+ """When existing_ids is None (force_reindex), only batch dedup applies."""+ from langchain_core.documents import Document as LangchainDocument+ from local_deep_research.research_library.services.library_rag_service import (+ LibraryRAGService,+ )++ chunks = [+ LangchainDocument(page_content="A"),+ LangchainDocument(page_content="B"),+ LangchainDocument(page_content="A copy"),+ ]+ ids = ["id-1", "id-2", "id-1"]++ result_chunks, result_ids = LibraryRAGService._deduplicate_chunks(+ chunks, ids, existing_ids=None+ )++ assert len(result_chunks) == 2+ assert result_ids == ["id-1", "id-2"]