Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: backfill span content from html for legacy pdf_info

jinzhenj78330d992504
diff --git a/mineru/parser/api_client.py b/mineru/parser/api_client.py
index 3209d564..71088ca9 100644
--- a/mineru/parser/api_client.py
+++ b/mineru/parser/api_client.py
@@ -973,6 +973,45 @@ def _pages_from_middle_json(mid_json: dict[str, Any] | None) -> list[PageInfo]:
return _parse_result_from_middle_json(mid_json).pages
+def _normalize_legacy_pdf_info_spans(pdf_info: Any) -> None:
+ """旧版 middle_json 的 span 可能用 html 字段携带 table/chart 等内容而不设 content。
+
+ Span.from_dict 只读取 dataclass 定义的字段,会丢弃 html,导致 content 为空。
+ 在反序列化前把非空 html 回填到 content。
+ """
+ if not isinstance(pdf_info, list):
+ return
+ for page in pdf_info:
+ if not isinstance(page, dict):
+ continue
+ for key in ("preproc_blocks", "para_blocks", "discarded_blocks"):
+ blocks = page.get(key)
+ if isinstance(blocks, list):
+ _normalize_legacy_blocks(blocks)
+
+
+def _normalize_legacy_blocks(blocks: list[Any]) -> None:
+ for block in blocks:
+ if not isinstance(block, dict):
+ continue
+ lines = block.get("lines")
+ if isinstance(lines, list):
+ for line in lines:
+ if not isinstance(line, dict):
+ continue
+ spans = line.get("spans")
+ if isinstance(spans, list):
+ for span in spans:
+ if not isinstance(span, dict):
+ continue
+ html = span.get("html")
+ if isinstance(html, str) and html:
+ span["content"] = html
+ children = block.get("blocks")
+ if isinstance(children, list):
+ _normalize_legacy_blocks(children)
+
+
def _parse_result_from_middle_json(mid_json: dict[str, Any]) -> ParseResult:
if isinstance(mid_json, dict):
pages = mid_json.get("pages")
@@ -982,6 +1021,7 @@ def _parse_result_from_middle_json(mid_json: dict[str, Any]) -> ParseResult:
pdf_info = mid_json.get("pdf_info")
if isinstance(pdf_info, list):
# The official API may use the older pdf_info field instead of ParseResult pages.
+ _normalize_legacy_pdf_info_spans(pdf_info)
compat_payload = dict(mid_json)
compat_payload["pages"] = pdf_info
return ParseResult.from_dict(compat_payload)
diff --git a/mineru/version.py b/mineru/version.py
index 12081b2d..d9abcc92 100644
--- a/mineru/version.py
+++ b/mineru/version.py
@@ -1 +1 @@
-__version__ = "4.0.0a4"
+__version__ = "4.0.0a5"
diff --git a/tests/unittest/test_parser_api_contract.py b/tests/unittest/test_parser_api_contract.py
index 116ba0b2..4a244437 100644
--- a/tests/unittest/test_parser_api_contract.py
+++ b/tests/unittest/test_parser_api_contract.py
@@ -1466,6 +1466,128 @@ def test_api_client_accepts_legacy_pdf_info_middle_json() -> None:
assert pages[0].page_size == (100, 200)
+def _legacy_span_with_html(html: str, *, content: str | None = None, span_type: str = "table") -> dict[str, Any]:
+ span: dict[str, Any] = {"type": span_type, "bbox": [0, 0, 10, 10], "html": html}
+ if content is not None:
+ span["content"] = content
+ return span
+
+
+def _legacy_page_with_spans(spans: list[dict[str, Any]], *, block_key: str = "para_blocks") -> dict[str, Any]:
+ return {
+ "page_idx": 0,
+ "page_size": [100, 200],
+ block_key: [
+ {
+ "index": 0,
+ "type": "table",
+ "bbox": [0, 0, 100, 100],
+ "lines": [{"bbox": [0, 0, 100, 100], "spans": spans}],
+ }
+ ],
+ }
+
+
+def test_legacy_pdf_info_fills_content_from_html_when_content_missing() -> None:
+ pages = _pages_from_middle_json(
+ {"pdf_info": [_legacy_page_with_spans([_legacy_span_with_html("<table></table>")])]}
+ )
+
+ spans = list(pages[0].para_blocks[0].lines[0].spans)
+ assert len(spans) == 1
+ assert spans[0].content == "<table></table>"
+
+
+def test_legacy_pdf_info_overrides_content_with_non_empty_html() -> None:
+ pages = _pages_from_middle_json(
+ {"pdf_info": [_legacy_page_with_spans([_legacy_span_with_html("<table>new</table>", content="old")])]}
+ )
+
+ spans = list(pages[0].para_blocks[0].lines[0].spans)
+ assert spans[0].content == "<table>new</table>"
+
+
+def test_legacy_pdf_info_leaves_content_untouched_when_html_missing_or_empty() -> None:
+ span_no_html: dict[str, Any] = {"type": "text", "bbox": [0, 0, 10, 10], "content": "keep"}
+ span_empty_html: dict[str, Any] = {"type": "text", "bbox": [0, 0, 10, 10], "content": "keep", "html": ""}
+
+ pages = _pages_from_middle_json(
+ {"pdf_info": [_legacy_page_with_spans([span_no_html, span_empty_html])]}
+ )
+
+ spans = list(pages[0].para_blocks[0].lines[0].spans)
+ assert [s.content for s in spans] == ["keep", "keep"]
+
+
+def test_legacy_pdf_info_normalizes_spans_in_nested_blocks_and_all_block_lists() -> None:
+ nested_span = _legacy_span_with_html("<table>nested</table>")
+ preproc_span = _legacy_span_with_html("<table>preproc</table>")
+ discarded_span = _legacy_span_with_html("<table>discarded</table>")
+
+ page: dict[str, Any] = {
+ "page_idx": 0,
+ "page_size": [100, 200],
+ "preproc_blocks": [
+ {
+ "index": 0,
+ "type": "table",
+ "bbox": [0, 0, 100, 100],
+ "lines": [{"bbox": [0, 0, 100, 100], "spans": [preproc_span]}],
+ }
+ ],
+ "para_blocks": [
+ {
+ "index": 0,
+ "type": "image",
+ "bbox": [0, 0, 100, 100],
+ "blocks": [
+ {
+ "index": 0,
+ "type": "image_body",
+ "bbox": [0, 0, 50, 50],
+ "lines": [{"bbox": [0, 0, 50, 50], "spans": [nested_span]}],
+ }
+ ],
+ }
+ ],
+ "discarded_blocks": [
+ {
+ "index": 0,
+ "type": "discarded",
+ "bbox": [0, 0, 100, 100],
+ "lines": [{"bbox": [0, 0, 100, 100], "spans": [discarded_span]}],
+ }
+ ],
+ }
+
+ pages = _pages_from_middle_json({"pdf_info": [page]})
+
+ assert list(pages[0].preproc_blocks[0].lines[0].spans)[0].content == "<table>preproc</table>"
+ nested = list(pages[0].para_blocks[0].blocks[0].lines[0].spans)[0]
+ assert nested.content == "<table>nested</table>"
+ assert list(pages[0].discarded_blocks[0].lines[0].spans)[0].content == "<table>discarded</table>"
+
+
+def test_new_format_pages_does_not_treat_html_as_content() -> None:
+ # 新版 pages 格式不走 compat 分支,span 的 html 字段应被 Span.from_dict 正常丢弃,不回填 content。
+ page = {
+ "page_idx": 0,
+ "page_size": [100, 200],
+ "para_blocks": [
+ {
+ "index": 0,
+ "type": "table",
+ "bbox": [0, 0, 100, 100],
+ "lines": [{"bbox": [0, 0, 100, 100], "spans": [_legacy_span_with_html("<table>html</table>")]}],
+ }
+ ],
+ }
+
+ result = ParseResult.from_dict({"pages": [page]})
+ spans = list(result.pages[0].para_blocks[0].lines[0].spans)
+ assert spans[0].content == ""
+
+
@pytest.mark.parametrize("payload", [[{"page_idx": 0}], {"pdf_info": {"preproc_blocks": []}}])
def test_api_client_rejects_legacy_middle_json_shapes(payload: object) -> None:
with pytest.raises(Exception, match="pages"):