From 943dc7af993be0fa484faca07b068b414bb2de17 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Fri, 30 Oct 2026 08:21:28 -0700 Subject: [PATCH] Chore: Import paperless_ai.indexing lazily in document tasks --- src/documents/tasks.py | 11 ++++++++--- src/documents/tests/test_tasks.py | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 778ee9673..8f0e85f16 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -76,9 +76,6 @@ from paperless.logging import consume_task_id from paperless.parsers import ParserContext from paperless.parsers.registry import get_parser_registry from paperless_ai.exceptions import LLMTimeoutError -from paperless_ai.indexing import llm_index_add_or_update_document -from paperless_ai.indexing import llm_index_remove_document -from paperless_ai.indexing import update_llm_index if settings.AUDIT_LOG_ENABLED: from auditlog.models import LogEntry @@ -339,6 +336,8 @@ def bulk_update_documents(document_ids) -> None: ai_config = AIConfig() if ai_config.llm_index_enabled: + from paperless_ai.indexing import update_llm_index + update_llm_index( rebuild=False, document_ids=document_ids, @@ -499,6 +498,8 @@ def update_document_content_maybe_archive_file( ai_config = AIConfig() if ai_config.llm_index_enabled: + from paperless_ai.indexing import llm_index_add_or_update_document + llm_index_add_or_update_document(document) clear_document_caches(document.pk) @@ -800,11 +801,15 @@ def apply_ai_suggestions(self, action_id: int, document_id: int) -> None: @shared_task def update_document_in_llm_index(document) -> None: + from paperless_ai.indexing import llm_index_add_or_update_document + llm_index_add_or_update_document(document) @shared_task def remove_document_from_llm_index(document) -> None: + from paperless_ai.indexing import llm_index_remove_document + llm_index_remove_document(document) diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index a5def0944..2e2b541a9 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -322,7 +322,7 @@ class TestUpdateContent(DirectoriesMixin, TestCase): ) @override_settings(AI_ENABLED=True, LLM_EMBEDDING_BACKEND="huggingface") - @mock.patch("documents.tasks.llm_index_add_or_update_document") + @mock.patch("paperless_ai.indexing.llm_index_add_or_update_document") @mock.patch("documents.search.get_backend") def test_update_content_version_updates_llm_index( self, @@ -450,7 +450,7 @@ class TestAIIndex(DirectoriesMixin, TestCase): checksum="wow", ) with mock.patch( - "documents.tasks.llm_index_add_or_update_document", + "paperless_ai.indexing.llm_index_add_or_update_document", ) as llm_index_add_or_update_document: tasks.update_document_in_llm_index(doc) llm_index_add_or_update_document.assert_called_once_with(doc) @@ -470,7 +470,7 @@ class TestAIIndex(DirectoriesMixin, TestCase): checksum="wow", ) with mock.patch( - "documents.tasks.llm_index_remove_document", + "paperless_ai.indexing.llm_index_remove_document", ) as llm_index_remove_document: tasks.remove_document_from_llm_index(doc) llm_index_remove_document.assert_called_once_with(doc) @@ -495,7 +495,7 @@ class TestAIIndex(DirectoriesMixin, TestCase): "documents.tasks.update_document_in_llm_index", ) as update_document_in_llm_index, mock.patch( - "documents.tasks.update_llm_index", + "paperless_ai.indexing.update_llm_index", ) as update_llm_index, ): doc_ids = [doc.pk for doc in docs]