Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: implement deepcopy for model_list_to_pages to preserve original input integrity
myhlolief4c15228dc6
diff --git a/mineru/backend/model_list_to_midlle_json.py b/mineru/backend/model_list_to_midlle_json.pyindex afc5413f..af3638a5 100644--- a/mineru/backend/model_list_to_midlle_json.py+++ b/mineru/backend/model_list_to_midlle_json.py@@ -1,5 +1,7 @@# Copyright (c) Opendatalab. All rights reserved.+from copy import deepcopyfrom typing import Any+from mineru.backend.magic_model import MagicModel@@ -24,8 +26,9 @@ def blocks_to_page_info(page_model_list: list[dict[str, Any]],):"""将blocks转换为页面信息"""+ # Middle JSON 转换允许重写块结构,但不能污染调用方保留的原始 model_list。magic_model = MagicModel(- page_model_list,+ deepcopy(page_model_list),)page_blocks = magic_model.blocksdiff --git a/tests/unittest/test_magic_model.py b/tests/unittest/test_magic_model.pynew file mode 100644index 00000000..300dafda--- /dev/null+++ b/tests/unittest/test_magic_model.py@@ -0,0 +1,108 @@+from __future__ import annotations++from mineru.backend.magic_model import MagicModel, fix_pdf_list_blocks+from mineru.types import BlockType+++def test_fix_pdf_list_blocks_supports_unit_bbox_without_rewrite() -> None:+ """验证归一化列表框可与像素文本框计算包含关系且不回写 bbox。"""+ list_bbox = [0.0, 0.0, 1.0, 1.0]+ text_bbox = [100, 100, 200, 200]+ list_block = {+ "type": BlockType.LIST,+ "bbox": list_bbox,+ "content": "",+ }+ text_block = {+ "type": BlockType.TEXT,+ "bbox": text_bbox,+ "content": "list item",+ }++ list_blocks, text_blocks, ref_text_blocks = fix_pdf_list_blocks(+ [list_block],+ [text_block],+ [],+ )++ assert list_blocks == [list_block]+ assert text_blocks == []+ assert ref_text_blocks == []+ assert list_block["content"] == [text_block]+ assert list_block["sub_type"] == BlockType.TEXT+ assert list_bbox == [0.0, 0.0, 1.0, 1.0]+ assert text_bbox == [100, 100, 200, 200]+++def test_magic_model_groups_bbox_dict_visual_blocks() -> None:+ """验证共享 MagicModel 可将带 bbox 的 dict 视觉块完成分组。"""+ magic_model = MagicModel(+ [+ {+ "type": BlockType.IMAGE_CAPTION,+ "bbox": [0.0, 0.0, 80.0, 10.0],+ "content": "Figure 1",+ },+ {+ "type": BlockType.IMAGE,+ "bbox": [0.0, 15.0, 80.0, 60.0],+ "_image_base64": "data:image/jpeg;base64,image",+ },+ ]+ )++ assert len(magic_model.image_blocks) == 1+ image_block = magic_model.image_blocks[0]+ assert isinstance(image_block, dict)+ assert [block["type"] for block in image_block["blocks"]] == [+ BlockType.IMAGE_CAPTION,+ BlockType.IMAGE_BODY,+ ]+++def test_magic_model_groups_no_bbox_office_caption_by_prefix() -> None:+ """验证共享 MagicModel 使用 Office 前缀规则分组无 bbox 视觉块。"""+ magic_model = MagicModel(+ [+ {+ "type": BlockType.IMAGE,+ "_image_base64": "data:image/jpeg;base64,image",+ },+ {+ "type": BlockType.TEXT,+ "content": "Figure 1",+ },+ ]+ )++ assert magic_model.text_blocks == []+ assert len(magic_model.image_blocks) == 1+ image_block = magic_model.image_blocks[0]+ assert [block["type"] for block in image_block["blocks"]] == [+ BlockType.IMAGE_BODY,+ BlockType.IMAGE_CAPTION,+ ]+++def test_magic_model_groups_no_bbox_chart_and_code_captions() -> None:+ """验证无 bbox 的 chart/code caption 映射及 code subtype 保留。"""+ magic_model = MagicModel(+ [+ {"type": BlockType.CHART_CAPTION, "content": "Chart 1"},+ {"type": BlockType.CHART, "content": "<div>chart</div>"},+ {"type": BlockType.CODE_CAPTION, "content": "Algorithm 1"},+ {"type": BlockType.CODE, "content": "print('ok')"},+ ]+ )++ assert [block["type"] for block in magic_model.chart_blocks[0]["blocks"]] == [+ BlockType.CHART_CAPTION,+ BlockType.CHART_BODY,+ ]+ code_block = magic_model.code_blocks[0]+ assert code_block["sub_type"] == "code"+ assert code_block["guess_lang"]+ assert [block["type"] for block in code_block["blocks"]] == [+ BlockType.CODE_CAPTION,+ BlockType.CODE_BODY,+ ]diff --git a/tests/unittest/test_model_list_to_midlle_json.py b/tests/unittest/test_model_list_to_midlle_json.pynew file mode 100644index 00000000..cb84bd46--- /dev/null+++ b/tests/unittest/test_model_list_to_midlle_json.py@@ -0,0 +1,159 @@+from copy import deepcopy++from mineru.backend.model_list_to_midlle_json import model_list_to_pages+from mineru.types import BlockType+++def test_model_list_to_pages_preserves_pdf_input() -> None:+ """验证 PDF 块完成规范化和分组后不会污染原始 model_list。"""+ model_list = [+ [+ {+ "type": BlockType.IMAGE_CAPTION,+ "bbox": [0, 0, 100, 10],+ "content": "Figure 1",+ },+ {+ "type": BlockType.IMAGE,+ "bbox": [0, 15, 100, 80],+ "image_base64": "data:image/jpeg;base64,image",+ },+ {+ "type": BlockType.EQUATION,+ "bbox": [0, 85, 100, 100],+ "content": r"\[x+1\]",+ },+ {+ "type": BlockType.LIST,+ "bbox": [0, 105, 100, 150],+ "content": "",+ },+ {+ "type": BlockType.TEXT,+ "bbox": [10, 110, 90, 125],+ "content": "list item",+ },+ {+ "type": BlockType.TABLE_CAPTION,+ "bbox": [0, 155, 100, 165],+ "content": "Table 1",+ },+ {+ "type": BlockType.TABLE,+ "bbox": [0, 170, 100, 240],+ "content": "<table></table>",+ },+ ]+ ]+ original_model_list = deepcopy(model_list)++ pages = model_list_to_pages(model_list, page_index_map=[7])++ assert model_list == original_model_list+ assert pages[0]["page_idx"] == 7+ blocks_by_type = {block["type"]: block for block in pages[0]["blocks"]}++ image_block = blocks_by_type[BlockType.IMAGE]+ assert [block["type"] for block in image_block["content"]] == [+ BlockType.IMAGE_CAPTION,+ BlockType.IMAGE_BODY,+ ]++ equation_block = blocks_by_type[BlockType.INTERLINE_EQUATION]+ assert equation_block["content"] == "x+1"+ assert equation_block is not model_list[0][2]++ list_block = blocks_by_type[BlockType.LIST]+ assert list_block["content"] == [+ {+ "type": BlockType.TEXT,+ "bbox": [10, 110, 90, 125],+ "content": "list item",+ "index": 4,+ }+ ]+ assert list_block["sub_type"] == BlockType.TEXT++ table_block = blocks_by_type[BlockType.TABLE]+ assert [block["type"] for block in table_block["content"]] == [+ BlockType.TABLE_CAPTION,+ BlockType.TABLE_BODY,+ ]++ equation_block["content"] = "changed"+ assert model_list == original_model_list+++def test_model_list_to_pages_preserves_office_nested_input() -> None:+ """验证 Office 嵌套列表和目录规范化不会污染原始 model_list。"""+ model_list = [+ [+ {+ "type": BlockType.IMAGE,+ "_image_base64": "data:image/jpeg;base64,image",+ },+ {+ "type": BlockType.TEXT,+ "content": "Figure 1",+ },+ {+ "type": BlockType.LIST,+ "attribute": "ordered",+ "ilevel": 0,+ "start": 1,+ "content": [+ {"type": BlockType.TEXT, "content": "first"},+ {+ "type": BlockType.LIST,+ "attribute": "unordered",+ "ilevel": 1,+ "content": [+ {"type": BlockType.TEXT, "content": "child"},+ ],+ },+ ],+ },+ {+ "type": BlockType.INDEX,+ "ilevel": 0,+ "content": [+ {+ "type": BlockType.INDEX,+ "ilevel": 1,+ "content": [+ {"type": BlockType.TEXT, "content": "section"},+ ],+ },+ ],+ },+ ]+ ]+ original_model_list = deepcopy(model_list)++ pages = model_list_to_pages(model_list)++ assert model_list == original_model_list+ assert pages[0]["page_idx"] == 0+ blocks_by_type = {block["type"]: block for block in pages[0]["blocks"]}++ image_block = blocks_by_type[BlockType.IMAGE]+ assert [block["type"] for block in image_block["content"]] == [+ BlockType.IMAGE_BODY,+ BlockType.IMAGE_CAPTION,+ ]++ list_block = blocks_by_type[BlockType.LIST]+ assert list_block["content"][0]["content"] == "1. first"+ nested_list = list_block["content"][1]+ assert nested_list["content"][0]["content"] == "- child"+ for metadata_key in ("attribute", "ilevel", "start"):+ assert metadata_key not in list_block+ assert metadata_key not in nested_list+ assert list_block is not model_list[0][2]++ index_block = blocks_by_type[BlockType.INDEX]+ assert "ilevel" not in index_block+ assert "ilevel" not in index_block["content"][0]++ list_block["content"][0]["content"] = "changed"+ assert model_list == original_model_list