Skip to content

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

feat: add merge_prev handling in Block for improved JSON serialization and deserialization

myhloli2b04bce3747d
diff --git a/mineru/backend/utils/para_block_utils.py b/mineru/backend/utils/para_block_utils.py
index 5eef36ca..afe19764 100644
--- a/mineru/backend/utils/para_block_utils.py
+++ b/mineru/backend/utils/para_block_utils.py
@@ -364,6 +364,7 @@ def _has_mergeable_block_bbox_relation(current_block: Block, previous_block: Blo
def _cleanup_block_internal_metadata(block: Block) -> None:
"""递归清理只供 finalize 内部流程使用的临时字段。"""
+ block.merge_prev = False
block._ocr_det_lines = []
block._line_avg_height = 0
for sub_block in block.blocks:
diff --git a/mineru/types.py b/mineru/types.py
index d0665b02..b2267b42 100644
--- a/mineru/types.py
+++ b/mineru/types.py
@@ -535,6 +535,21 @@ class Block(_DocElement):
self._merge_prev = _initvar_default(merge_prev, False)
self._is_numbered_style = _initvar_default(is_numbered_style, False)
+ def to_dict(self, *, skip_defaults: bool = True) -> dict[str, Any]:
+ """仅在 staged middle_json 需要时输出 merge_prev 合并提示。"""
+ result = super().to_dict(skip_defaults=skip_defaults)
+ if self.merge_prev or not skip_defaults:
+ result["merge_prev"] = self.merge_prev
+ return result
+
+ @classmethod
+ def from_dict(cls, d: dict) -> Block:
+ """读取 staged middle_json 中的 merge_prev 合并提示。"""
+ block = _DocElement.from_dict.__func__(cls, d)
+ if "merge_prev" in d:
+ block.merge_prev = bool(d.get("merge_prev", False))
+ return block
+
@property
def angle(self) -> int | None:
"""转换阶段读取的原始旋转角,默认不作为 middle_json block 字段输出。"""