Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix handling of no LF image processing (#2318)

Mike Fortman1ab7f1120a35
diff --git a/src/models/processors.py b/src/models/processors.py
index 2287dd813..3359cf747 100644
--- a/src/models/processors.py
+++ b/src/models/processors.py
@@ -7,7 +7,6 @@ from typing import TYPE_CHECKING, Any, Literal
from config.settings import clients, get_embedding_model, get_index_name, get_openrag_config
from session_manager import AnonymousUser
from utils.document_processing import (
- IMAGE_PLACEHOLDER,
extract_relevant,
process_text_file,
resplit_chunks_character_windows,
@@ -532,18 +531,6 @@ class TaskProcessor:
# since chunk_texts_for_embeddings also drops empty texts.
slim_doc["chunks"] = [c for c in slim_doc["chunks"] if c.get("text") and c["text"].strip()]
- # A placeholder preserves picture provenance for otherwise empty documents,
- # but it is not searchable content and must not turn ingestion into a false
- # success or consume an embedding request.
- if slim_doc["chunks"] and all(
- chunk["text"].strip() == IMAGE_PLACEHOLDER for chunk in slim_doc["chunks"]
- ):
- logger.error(
- "No searchable content extracted — document contains only image placeholders",
- file_hash=file_hash,
- )
- return {"status": "error", "error": "No text content could be extracted from document"}
-
litellm_embedding_model = (
await self.models_service.get_litellm_model_name(embedding_model)
if self.models_service is not None
diff --git a/src/utils/document_processing.py b/src/utils/document_processing.py
index 955ed2b46..a0a9c3996 100644
--- a/src/utils/document_processing.py
+++ b/src/utils/document_processing.py
@@ -208,6 +208,12 @@ def resplit_chunks_character_windows(
if not isinstance(text, str):
out.append(dict(ch) if isinstance(ch, dict) else ch)
continue
+ # This marker is a semantic signal that Docling detected an image even
+ # though it extracted no text. Keep it intact so image-only documents
+ # remain ingestible and display the same placeholder as the Langflow path.
+ if text.strip() == IMAGE_PLACEHOLDER:
+ out.append(dict(ch))
+ continue
if len(text) <= chunk_size:
out.append(dict(ch))
continue
diff --git a/tests/unit/test_document_processing_resplit.py b/tests/unit/test_document_processing_resplit.py
index 919fa5a3e..999e6b80f 100644
--- a/tests/unit/test_document_processing_resplit.py
+++ b/tests/unit/test_document_processing_resplit.py
@@ -1,6 +1,6 @@
"""Tests for resplit_chunks_character_windows."""
-from src.utils.document_processing import resplit_chunks_character_windows
+from src.utils.document_processing import IMAGE_PLACEHOLDER, resplit_chunks_character_windows
def test_invalid_returns_original():
@@ -25,6 +25,14 @@ def test_short_text_unchanged_count():
assert out[0]["page"] == 2
+def test_image_placeholder_is_not_split():
+ chunks = [{"page": 1, "type": "picture", "text": IMAGE_PLACEHOLDER}]
+
+ out = resplit_chunks_character_windows(chunks, 1, 0)
+
+ assert out == chunks
+
+
def test_long_text_windows_with_overlap():
# 22 chars, size 10, overlap 4 -> stride 6; third window reaches end
text = "abcdefghijklmnopqrstuv"
diff --git a/tests/unit/test_processors_image_only.py b/tests/unit/test_processors_image_only.py
index e971beacd..72d437b59 100644
--- a/tests/unit/test_processors_image_only.py
+++ b/tests/unit/test_processors_image_only.py
@@ -7,8 +7,20 @@ from models.processors import TaskProcessor
@pytest.mark.asyncio
-async def test_placeholder_only_document_returns_no_text_error_before_embedding(monkeypatch):
- embedding_create = AsyncMock(side_effect=AssertionError("placeholder must not be embedded"))
+ ("pictures", "chunk_size", "expected_result"),
+ [
+ ([{"prov": [{"page_no": 1}], "annotations": []}], None, "indexed"),
+ ([{"prov": [{"page_no": 1}], "annotations": []}], 1, "indexed"),
+ ([], None, "error"),
+ ],
+)
+async def test_image_only_placeholder_succeeds_but_empty_document_fails(
+ monkeypatch, pictures, chunk_size, expected_result
+):
+ embedding_create = AsyncMock(
+ return_value=SimpleNamespace(data=[{"embedding": [0.1, 0.2, 0.3]}])
+ )
monkeypatch.setattr(
"models.processors.clients",
SimpleNamespace(
@@ -23,21 +35,30 @@ async def test_placeholder_only_document_returns_no_text_error_before_embedding(
lambda: SimpleNamespace(
knowledge=SimpleNamespace(
embedding_model="text-embedding-3-small",
- chunk_size=None,
+ chunk_size=chunk_size,
chunk_overlap=None,
)
),
)
monkeypatch.setattr(
"services.document_service.chunk_texts_for_embeddings",
- lambda texts, max_tokens: [texts],
+ lambda texts, max_tokens: [texts] if texts else [],
)
user_client = SimpleNamespace()
+ indexed: dict = {}
+
+ class FakeDocumentIndexWriter:
+ async def index_chunks(self, context, chunks, *, final=False):
+ indexed["context"] = context
+ indexed["chunks"] = chunks
+ indexed["final"] = final
+
document_service = SimpleNamespace(
session_manager=SimpleNamespace(
get_user_opensearch_client=lambda user_id, jwt_token: user_client
- )
+ ),
+ document_index_writer=FakeDocumentIndexWriter(),
)
models_service = SimpleNamespace(
get_litellm_model_name=AsyncMock(return_value="text-embedding-3-small")
@@ -52,7 +73,7 @@ async def test_placeholder_only_document_returns_no_text_error_before_embedding(
},
"texts": [],
"tables": [],
- "pictures": [{"prov": [{"page_no": 1}], "annotations": []}],
+ "pictures": pictures,
}
)
)
@@ -68,8 +89,20 @@ async def test_placeholder_only_document_returns_no_text_error_before_embedding(
picture_descriptions=False,
)
- assert result == {
- "status": "error",
- "error": "No text content could be extracted from document",
- }
- embedding_create.assert_not_awaited()
+ if expected_result == "error":
+ assert result == {
+ "status": "error",
+ "error": "No text content could be extracted from document",
+ }
+ embedding_create.assert_not_awaited()
+ assert indexed == {}
+ return
+
+ assert result == {"status": "indexed", "id": "image-hash"}
+ embedding_create.assert_awaited_once_with(
+ model="text-embedding-3-small", input=["<!-- image -->"]
+ )
+ assert indexed["final"] is True
+ assert len(indexed["chunks"]) == 1
+ assert indexed["chunks"][0].text == "<!-- image -->"
+ assert indexed["chunks"][0].page == 1