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: surface MinerU API error bodies and re-raise parser failures (#17308)
天海蒼灆a1208cd2733d
diff --git a/deepdoc/parser/mineru_parser.py b/deepdoc/parser/mineru_parser.pyindex 17c54447ba..b77212eef3 100644--- a/deepdoc/parser/mineru_parser.py+++ b/deepdoc/parser/mineru_parser.py@@ -297,12 +297,11 @@ class MinerUParser(RAGFlowPdfParser):data = {"output_dir": "./output",- "lang_list": options.lang,- "backend": options.backend,- "parse_method": options.method,+ "lang_list": options.lang.value if isinstance(options.lang, MinerULanguage) else options.lang,+ "backend": options.backend.value if isinstance(options.backend, MinerUBackend) else options.backend,+ "parse_method": options.method.value if isinstance(options.method, MinerUParseMethod) else options.method,"formula_enable": options.formula_enable,"table_enable": options.table_enable,- "server_url": None,"return_md": True,"return_middle_json": True,"return_model_output": True,@@ -324,7 +323,7 @@ class MinerUParser(RAGFlowPdfParser):headers = {"Accept": "application/json"}try:- self.logger.info(f"[MinerU] invoke api: {self.mineru_api}/file_parse backend={options.backend} server_url={data.get('server_url')}")+ self.logger.info(f"[MinerU] invoke api: {self.mineru_api}/file_parse backend={data['backend']} server_url={data.get('server_url')}")if callback:callback(0.20, f"[MinerU] invoke api: {self.mineru_api}/file_parse")with open(pdf_file_path, "rb") as pdf_file:@@ -337,7 +336,9 @@ class MinerUParser(RAGFlowPdfParser):timeout=1800,stream=True,) as response:- response.raise_for_status()+ if not response.ok:+ body = (response.text or "")[:2000]+ raise RuntimeError(f"[MinerU] api failed status={response.status_code} body={body}")content_type = response.headers.get("Content-Type", "")if not content_type.startswith("application/zip"):raise RuntimeError(f"[MinerU] not zip returned from api: {content_type}")@@ -354,7 +355,14 @@ class MinerUParser(RAGFlowPdfParser):self.logger.info("[MinerU] Api completed successfully.")return Path(output_path)except requests.RequestException as e:- raise RuntimeError(f"[MinerU] api failed with exception {e}")+ detail = ""+ resp = getattr(e, "response", None)+ if resp is not None:+ try:+ detail = f" body={(resp.text or '')[:2000]}"+ except Exception:+ pass+ raise RuntimeError(f"[MinerU] api failed with exception {e}{detail}") from edef __images__(self, fnm, zoomin: int = 1, page_from=0, page_to=MAXIMUM_PAGE_NUMBER, callback=None):self.page_from = page_fromdiff --git a/rag/app/naive.py b/rag/app/naive.pyindex 3f4c8498ab..612279d7a2 100644--- a/rag/app/naive.py+++ b/rag/app/naive.py@@ -413,6 +413,9 @@ def by_paddleocr(return sections, tables, pdf_parserexcept Exception as e:logging.error(f"Failed to parse pdf via LLMBundle PaddleOCR ({paddleocr_llm_name}): {e}")+ if callback:+ callback(-1, f"Failed to parse pdf via PaddleOCR ({paddleocr_llm_name}): {e}")+ raisereturn None, None, Nonediff --git a/test/unit_test/deepdoc/parser/test_mineru_parser.py b/test/unit_test/deepdoc/parser/test_mineru_parser.pyindex cc5125033c..5a2d9dddd9 100644--- a/test/unit_test/deepdoc/parser/test_mineru_parser.py+++ b/test/unit_test/deepdoc/parser/test_mineru_parser.py@@ -577,17 +577,22 @@ def test_transfer_to_sections_routes_app_media_separately(monkeypatch, parse_metclass _FakeZipResponse:"""Stand-in for the streaming response returned by requests.post.- Provides the minimum surface that _run_mineru_api touches: status code,- headers (Content-Type), and a `.raw` stream that copyfileobj can drain.+ Provides the minimum surface that _run_mineru_api touches: status_code,+ ok, text, headers (Content-Type), and a `.raw` stream that copyfileobj+ can drain."""- def __init__(self, body: bytes = b"zip-bytes"):+ def __init__(self, body: bytes = b"zip-bytes", *, status_code: int = 200, text: str = ""):self._body = body+ self.status_code = status_code+ self.ok = 200 <= status_code < 400+ self.text = textself.headers = {"Content-Type": "application/zip"}self.raw = BytesIO(body)def raise_for_status(self):- return None+ if not self.ok:+ raise RuntimeError(f"HTTP {self.status_code}")class _FakePostContext:@@ -602,7 +607,15 @@ class _FakePostContext:return False-def _capture_run_mineru_api(monkeypatch, module, *, pdf_path: Path, extracted_dir: Path):+def _capture_run_mineru_api(+ monkeypatch,+ module,+ *,+ pdf_path: Path,+ extracted_dir: Path,+ status_code: int = 200,+ text: str = "",+):"""Stub everything around requests.post so _run_mineru_api runs end-to-endagainst an in-memory response. Returns the captured kwargs dict."""@@ -612,7 +625,7 @@ def _capture_run_mineru_api(monkeypatch, module, *, pdf_path: Path, extracted_dicaptured["url"] = urlcaptured["data"] = datacaptured["files"] = files- return _FakePostContext(_FakeZipResponse(), captured)+ return _FakePostContext(_FakeZipResponse(status_code=status_code, text=text), captured)monkeypatch.setattr(module.requests, "post", fake_post)monkeypatch.setattr(module.os.path, "exists", lambda _p: True)@@ -720,6 +733,61 @@ def test_end_page_minus_one_normalizes_for_mineru_api(monkeypatch, tmp_path):assert captured["data"]["end_page_id"] == 12+def test_run_mineru_api_raises_with_status_and_body_on_non_ok(monkeypatch, tmp_path):+ module = _load_mineru_parser(monkeypatch)+ parser = module.MinerUParser(mineru_api="http://mineru.local")+ parser.mineru_server_url = ""++ pdf_path = tmp_path / "sample.pdf"+ pdf_path.write_bytes(b"%PDF-1.4 fake")+ extracted_dir = tmp_path / "out"+ extracted_dir.mkdir()++ _capture_run_mineru_api(+ monkeypatch,+ module,+ pdf_path=pdf_path,+ extracted_dir=extracted_dir,+ status_code=502,+ text='{"error":"upstream timeout"}',+ )+ options = module.MinerUParseOptions()++ with pytest.raises(RuntimeError, match=r"status=502.*upstream timeout") as exc_info:+ parser._run_mineru_api(pdf_path, extracted_dir, options, callback=None)++ assert "body=" in str(exc_info.value)+++ ("options_server_url", "parser_server_url", "expected"),+ [+ (None, "", None),+ ("http://options.server", "", "http://options.server"),+ (None, "http://parser.server", "http://parser.server"),+ ("http://options.server", "http://parser.server", "http://options.server"),+ ],+)+def test_run_mineru_api_server_url_only_when_set(monkeypatch, tmp_path, options_server_url, parser_server_url, expected):+ module = _load_mineru_parser(monkeypatch)+ parser = module.MinerUParser(mineru_api="http://mineru.local", mineru_server_url=parser_server_url)++ pdf_path = tmp_path / "sample.pdf"+ pdf_path.write_bytes(b"%PDF-1.4 fake")+ extracted_dir = tmp_path / "out"+ extracted_dir.mkdir()++ captured = _capture_run_mineru_api(monkeypatch, module, pdf_path=pdf_path, extracted_dir=extracted_dir)+ options = module.MinerUParseOptions(server_url=options_server_url)++ parser._run_mineru_api(pdf_path, extracted_dir, options, callback=None)++ if expected is None:+ assert "server_url" not in captured["data"]+ else:+ assert captured["data"]["server_url"] == expected++class _FakePageImage:def __init__(self, width: int, height: int):self.size = (width, height)diff --git a/test/unit_test/rag/test_naive_dispatch.py b/test/unit_test/rag/test_naive_dispatch.pyindex b56ce74608..2dd6a2cc05 100644--- a/test/unit_test/rag/test_naive_dispatch.py+++ b/test/unit_test/rag/test_naive_dispatch.py@@ -36,6 +36,7 @@ import importlib.utilimport sysimport typesfrom pathlib import Path+from unittest.mock import Mockimport pytest@@ -420,3 +421,37 @@ def test_merge_excel_items_passthrough_when_budget_disabled(naive_module):items = [("a", (0, 2, 2, 1, 1)), ("b", (0, 3, 3, 1, 1))]assert naive_module._merge_excel_items(items, chunk_token_num=0) == itemsassert naive_module._merge_excel_items([], chunk_token_num=128) == []+++def test_by_paddleocr_rethrows_parse_failure_and_notifies_callback(naive_module, monkeypatch):+ callback = Mock()+ parse_error = RuntimeError("paddle boom")+ fake_parser = Mock()+ fake_parser.parse_pdf.side_effect = parse_error+ fake_bundle = Mock(mdl=fake_parser)++ monkeypatch.setattr(naive_module, "resolve_model_config", Mock(return_value={"id": "cfg"}))+ monkeypatch.setattr(naive_module, "LLMBundle", Mock(return_value=fake_bundle))++ with pytest.raises(RuntimeError, match="paddle boom"):+ naive_module.by_paddleocr(+ "doc.pdf",+ tenant_id="t1",+ paddleocr_llm_name="paddle@provider",+ callback=callback,+ )++ callback.assert_called_once()+ prog, msg = callback.call_args.args+ assert prog == -1+ assert "Failed to parse pdf via PaddleOCR" in msg+ assert "paddle boom" in msg+++def test_by_paddleocr_not_found_path_unchanged(naive_module):+ callback = Mock()++ sections, tables, parser = naive_module.by_paddleocr("doc.pdf", callback=callback)++ assert (sections, tables, parser) == (None, None, None)+ callback.assert_called_once_with(-1, "PaddleOCR not found.")