~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(library): batch convert_all_research to bound memory on large history (#4560) (#4585)
LearningCircuitf19aacadc21b
diff --git a/changelog.d/+batch-research-history-indexer.bugfix.md b/changelog.d/+batch-research-history-indexer.bugfix.mdnew file mode 100644index 0000000000..e764bca65f--- /dev/null+++ b/changelog.d/+batch-research-history-indexer.bugfix.md@@ -0,0 +1 @@+Converting research history into the searchable library now pages through reports in bounded batches instead of loading every report body into memory at once, preventing `MemoryError` on large histories.diff --git a/src/local_deep_research/research_library/search/services/research_history_indexer.py b/src/local_deep_research/research_library/search/services/research_history_indexer.pyindex 3dc8be7f15..ad3469cb41 100644--- a/src/local_deep_research/research_library/search/services/research_history_indexer.py+++ b/src/local_deep_research/research_library/search/services/research_history_indexer.py@@ -39,6 +39,12 @@ class ResearchHistoryIndexer:SOURCE_TYPE_REPORT = "research_report"COLLECTION_TYPE = "research_history"+ # convert_all_research pages through candidates this many rows at a time.+ # report_content is a large Text column, so loading every completed+ # report body at once can exhaust memory on a big history (#4560). This+ # caps how many bodies are resident at any moment.+ CONVERT_BATCH_SIZE = 50+def __init__(self, username: str, db_password: Optional[str] = None):"""Initialize the indexer for a user.@@ -210,50 +216,70 @@ class ResearchHistoryIndexer:.count())- # Fetch candidates — optionally excluding already-converted entries- query = (- session.query(ResearchHistory)+ # Fetch candidate IDs only — optionally excluding already-converted+ # entries. We must NOT materialize every full ResearchHistory row+ # here: report_content is a large Text column, and loading every+ # completed report body at once can exhaust memory on a big history+ # (#4560). IDs are tiny, so the full candidate list is cheap; we+ # then load the full rows one bounded batch at a time below.+ id_query = (+ session.query(ResearchHistory.id).filter(ResearchHistory.status == ResearchStatus.COMPLETED).filter(ResearchHistory.report_content.isnot(None)).filter(ResearchHistory.report_content != "").order_by(ResearchHistory.created_at.desc()))if not force:- query = query.filter(+ id_query = id_query.filter(ResearchHistory.id.notin_(already_converted_subquery.select()))- research_entries = query.all()+ research_ids = [row.id for row in id_query.all()]converted = 0- skipped = total_eligible - len(research_entries) if not force else 0+ skipped = total_eligible - len(research_ids) if not force else 0failed = 0- for research in research_entries:- try:- # Create (or reuse) report Document- report_doc = self._create_document_from_report(- research,- collection_id,- session,- report_type_id=report_type.id,- )- if report_doc is None:- # SourceType missing inside helper (already warned)- failed += 1- continue-- # Commit each entry individually so a rollback on failure- # only loses the failing entry, not the whole batch.- session.commit()- converted += 1+ for start in range(0, len(research_ids), self.CONVERT_BATCH_SIZE):+ batch_ids = research_ids[+ start : start + self.CONVERT_BATCH_SIZE+ ]+ # Load one batch of full rows (report bodies) at a time so peak+ # memory stays bounded regardless of total history size.+ batch = (+ session.query(ResearchHistory)+ .filter(ResearchHistory.id.in_(batch_ids))+ .order_by(ResearchHistory.created_at.desc())+ .all()+ )- except Exception:- logger.exception(f"Error converting research {research.id}")- session.rollback()- failed += 1+ for research in batch:+ try:+ # Create (or reuse) report Document+ report_doc = self._create_document_from_report(+ research,+ collection_id,+ session,+ report_type_id=report_type.id,+ )+ if report_doc is None:+ # SourceType missing inside helper (already warned)+ failed += 1+ continue++ # Commit each entry individually so a rollback on+ # failure only loses the failing entry, not the batch.+ session.commit()+ converted += 1++ except Exception:+ logger.exception(+ f"Error converting research {research.id}"+ )+ session.rollback()+ failed += 1logger.info(f"convert_all_research complete — converted={converted}, "diff --git a/tests/research_library/search/services/test_research_history_indexer.py b/tests/research_library/search/services/test_research_history_indexer.pyindex b8096e9081..2a0cf701f5 100644--- a/tests/research_library/search/services/test_research_history_indexer.py+++ b/tests/research_library/search/services/test_research_history_indexer.py@@ -2,6 +2,7 @@Tests for ResearchHistoryIndexer service."""+import mathimport uuidfrom contextlib import contextmanagerfrom unittest.mock import patch@@ -339,6 +340,83 @@ class TestConvertAllResearch:docs = mock_session_ctx.query(Document).all()assert len(docs) == 2+ def test_batches_large_history_without_loading_all_bodies(+ self,+ indexer,+ mock_session_ctx,+ research_source_types,+ research_collection,+ ):+ """convert_all_research must enumerate candidates by ID and page+ through full rows in CONVERT_BATCH_SIZE chunks, so it never loads+ every report body at once (#4560). Asserts both multi-batch+ correctness (5 entries, batch size 2 -> 2+2+1 all convert) and that+ the candidate fetch projects ResearchHistory.id rather than the full+ entity (a revert to query(ResearchHistory) would never do this)."""+ session = mock_session_ctx # fixture yields the real library_session+ for i in range(5):+ session.add(+ ResearchHistory(+ id=str(uuid.uuid4()),+ query=f"Batch query {i}",+ mode="detailed_report",+ status="completed",+ created_at=f"2025-04-0{i + 1}T10:00:00",+ report_content=f"# Batch Report {i}\n\nBody {i}.",+ title=f"Batch {i}",+ )+ )+ session.commit()++ real_query = session.query+ query_calls = []++ def spy_query(*args, **kwargs):+ query_calls.append(args)+ return real_query(*args, **kwargs)++ with (+ patch.object(+ indexer,+ "get_or_create_collection",+ return_value=research_collection.id,+ ),+ patch.object(ResearchHistoryIndexer, "CONVERT_BATCH_SIZE", 2),+ patch.object(session, "query", side_effect=spy_query),+ ):+ result = indexer.convert_all_research(force=False)++ assert result["converted"] == 5+ assert result["skipped"] == 0+ assert result["failed"] == 0+ assert len(session.query(Document).all()) == 5++ # (1) The candidate enumeration must project the id column, not the+ # full ResearchHistory entity (identity check — column __eq__ builds a+ # clause, so `in`/`==` are unsafe). Exactly one such fetch.+ id_fetches = [+ a for a in query_calls if len(a) == 1 and a[0] is ResearchHistory.id+ ]+ assert len(id_fetches) == 1, (+ "candidate fetch must project ResearchHistory.id exactly once"+ )++ # (2) Full report rows must be loaded in bounded batches, not all at+ # once. convert_all_research issues one full-entity query(ResearchHistory)+ # for the eligibility count plus one per batch. With 5 entries and+ # CONVERT_BATCH_SIZE=2 that is 1 + ceil(5/2) = 4. A regression that+ # collapses the loop into a single query(ResearchHistory).filter(+ # id.in_(all_ids)).all() would issue only 1 + 1 = 2 and fail here.+ expected_full_entity_queries = 1 + math.ceil(5 / 2)+ full_entity_queries = [+ a for a in query_calls if len(a) == 1 and a[0] is ResearchHistory+ ]+ assert len(full_entity_queries) == expected_full_entity_queries, (+ f"expected {expected_full_entity_queries} full-entity "+ f"ResearchHistory queries (1 count + 3 batches), "+ f"got {len(full_entity_queries)}"+ )+def test_skips_already_converted_when_force_false(self,indexer,