RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: keep OCR text when no image2text model is configured (#18012)
Dhruv Diwakirti86c25068fa37
diff --git a/rag/app/picture.py b/rag/app/picture.pyindex 4c6b3cc21c..762d52a719 100644--- a/rag/app/picture.py+++ b/rag/app/picture.py@@ -91,6 +91,7 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):bxs = _get_ocr()(np.array(img))txt = "\n".join([t[0] for _, t in bxs if t[0]])+ txt = txt.strip()callback(0.4, "Finish OCR: (%s ...)" % txt[:12])if (eng and len(txt.split()) > 32) or len(txt) > 32:tokenize(doc, txt, eng, language=lang)@@ -110,6 +111,11 @@ def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):tokenize(doc, txt, eng, language=lang)return attach_media_context([doc], 0, image_ctx)except Exception as e:+ if txt:+ logging.warning(f"CV LLM unavailable, indexing OCR text instead: {e}")+ callback(msg=f"[WARN] CV LLM unavailable ({e}), indexing OCR text only.")+ tokenize(doc, txt, eng, language=lang)+ return attach_media_context([doc], 0, image_ctx)callback(prog=-1, msg=str(e))return []diff --git a/test/unit_test/rag/app/test_picture_video.py b/test/unit_test/rag/app/test_picture_video.pyindex 58d6ce5e9f..5923c05ffc 100644--- a/test/unit_test/rag/app/test_picture_video.py+++ b/test/unit_test/rag/app/test_picture_video.py@@ -15,14 +15,22 @@#import importlib.util+import ioimport sysfrom pathlib import Pathfrom types import ModuleType, SimpleNamespacefrom unittest.mock import patch+import pytest+from PIL import Image-def _load_picture_module(tokenized_texts):- """Load the picture parser with lightweight fakes for external services."""++def _load_picture_module(tokenized_texts, ocr_text="", vision_error=""):+ """Load the picture parser with lightweight fakes for external services.++ ``ocr_text`` is what the local OCR returns for an image, and ``vision_error``+ makes the vision-model lookup fail with that message.+ """class FakeLLMBundle:def __init__(self, *args, **kwargs):@@ -38,8 +46,15 @@ def _load_picture_module(tokenized_texts):llm_service = ModuleType("api.db.services.llm_service")llm_service.LLMBundle = FakeLLMBundle+ def resolve_default_model(*_args, **_kwargs):+ if vision_error:+ # get_tenant_default_model_by_type raises a bare Exception when no+ # default model is set, not LookupError.+ raise Exception(vision_error) # noqa: TRY002+ return {}+tenant_model_service = ModuleType("api.db.joint_services.tenant_model_service")- tenant_model_service.get_tenant_default_model_by_type = lambda *args, **kwargs: {}+ tenant_model_service.get_tenant_default_model_by_type = resolve_default_modeltenant_model_service.get_first_provider_model_name = lambda *args, **kwargs: Nonetenant_model_service.get_composite_model_name_by_id = lambda model_id: model_idtenant_model_service.resolve_model_config = lambda *args, **kwargs: {}@@ -54,8 +69,13 @@ def _load_picture_module(tokenized_texts):string_utils = ModuleType("common.string_utils")string_utils.clean_markdown_block = lambda value: value+ def fake_ocr(_image):+ """Mimic deepdoc's OCR return shape: (box, (text, confidence)) pairs."""++ return [(None, (ocr_text, 0.99))] if ocr_text else []+vision = ModuleType("deepdoc.vision")- vision.OCR = lambda: object()+ vision.OCR = lambda: fake_ocrnlp = ModuleType("rag.nlp")nlp.attach_media_context = lambda docs, *_args: docs@@ -107,3 +127,97 @@ def test_video_description_is_tokenized_once():assert len(chunks) == 1assert chunks[0]["doc_type_kwd"] == "video"assert tokenized_texts == ["A concise video description."]+++def _jpeg_bytes():+ """Encode a small blank image the parser can open."""++ buffer = io.BytesIO()+ Image.new("RGB", (64, 64), "white").save(buffer, format="JPEG")+ return buffer.getvalue()+++def test_short_ocr_text_is_kept_when_vision_model_is_missing():+ """OCR text under the CV LLM threshold is indexed when no image2text model+ is configured, instead of being dropped and leaving the image with no chunk.++ Guards https://github.com/infiniflow/ragflow/issues/17941.+ """++ tokenized_texts = []+ picture = _load_picture_module(+ tokenized_texts,+ ocr_text="工流宛转绕芳甸",+ vision_error="No default image2text model is set.",+ )++ callback_calls = []+ chunks = picture.chunk(+ "scan.jpg",+ _jpeg_bytes(),+ "tenant",+ "English",+ callback=lambda *args, **kwargs: callback_calls.append((args, kwargs)),+ )++ assert len(chunks) == 1, "OCR text was discarded, leaving the image with no chunk"+ assert chunks[0]["doc_type_kwd"] == "image"+ assert tokenized_texts == ["工流宛转绕芳甸"]++[email protected]("ocr_text", ["", " \n"])+def test_parser_reports_failure_when_ocr_finds_no_text(ocr_text):+ """With no usable OCR text and no vision model there is nothing to index, so+ chunk() returns no chunks and reports the failure through the callback."""++ tokenized_texts = []+ picture = _load_picture_module(+ tokenized_texts,+ ocr_text=ocr_text,+ vision_error="No default image2text model is set.",+ )++ callback_calls = []+ chunks = picture.chunk(+ "blank.jpg",+ _jpeg_bytes(),+ "tenant",+ "English",+ callback=lambda *args, **kwargs: callback_calls.append((args, kwargs)),+ )++ assert chunks == []+ assert not tokenized_texts+ assert [kwargs.get("msg") for _args, kwargs in callback_calls if kwargs.get("prog") == -1] == ["No default image2text model is set."]+++def test_ocr_only_fallback_is_reported_as_degraded():+ """The OCR-only fallback keeps the task successful but must not report a clean+ parse: the vision failure is surfaced as a [WARN] message instead of prog=-1,+ so a misconfigured model stays visible to the operator."""++ tokenized_texts = []+ picture = _load_picture_module(+ tokenized_texts,+ ocr_text="invoice total 42",+ vision_error="No default image2text model is set.",+ )++ callback_calls = []+ chunks = picture.chunk(+ "scan.jpg",+ _jpeg_bytes(),+ "tenant",+ "English",+ callback=lambda *args, **kwargs: callback_calls.append((args, kwargs)),+ )++ assert len(chunks) == 1+ assert [kwargs.get("prog") for _args, kwargs in callback_calls if kwargs.get("prog") == -1] == []++ warnings = [kwargs["msg"] for _args, kwargs in callback_calls if "msg" in kwargs and kwargs["msg"].startswith("[WARN]")]+ assert len(warnings) == 1, "the vision failure was not reported as a degraded parse"+ assert "No default image2text model is set." in warnings[0]++ progressed = [args[0] for args, _kwargs in callback_calls if args]+ assert 0.8 not in progressed, "the fallback still reports a clean 0.8 success"