Skip to content

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

feat: replace BlockType.FORMULA_NUMBER with RAW_FORMULA_NUMBER for consistency in type handling

myhloli972c847e561f
diff --git a/mineru/backend/analysis/pdf/constants.py b/mineru/backend/analysis/pdf/constants.py
index d79712df..9b76729d 100644
--- a/mineru/backend/analysis/pdf/constants.py
+++ b/mineru/backend/analysis/pdf/constants.py
@@ -5,7 +5,7 @@ from __future__ import annotations
import re
-from mineru.types import RAW_ALGORITHM, RAW_CAPTION, RAW_FOOTNOTE, RAW_PHONETIC, BlockType
+from mineru.types import RAW_ALGORITHM, RAW_CAPTION, RAW_FOOTNOTE, RAW_FORMULA_NUMBER, RAW_PHONETIC, BlockType
LAYOUT_BASE_BATCH_SIZE = 1
MFR_BASE_BATCH_SIZE = 16
@@ -85,7 +85,7 @@ VLM_LAYOUT_LABEL_MAP = {
"footer": BlockType.FOOTER,
"footer_image": BlockType.FOOTER,
"footnote": BlockType.PAGE_FOOTNOTE,
- "formula_number": BlockType.FORMULA_NUMBER,
+ "formula_number": RAW_FORMULA_NUMBER,
"header": BlockType.HEADER,
"header_image": BlockType.HEADER,
"image": BlockType.IMAGE,
diff --git a/mineru/backend/analysis/pdf/formulas.py b/mineru/backend/analysis/pdf/formulas.py
index a069ce60..62b68076 100644
--- a/mineru/backend/analysis/pdf/formulas.py
+++ b/mineru/backend/analysis/pdf/formulas.py
@@ -14,7 +14,7 @@ from mineru.backend.local_model_runtime import HybridLocalModelContext
from mineru.utils.bbox_utils import normalize_to_int_bbox
from mineru.utils.ocr_utils import get_rotate_crop_image_for_text_rec
-from ....types import BlockType
+from ....types import RAW_FORMULA_NUMBER, BlockType
from mineru.utils.text_utils import full_to_half
from mineru.utils.text_utils import clean_isolated_formula
@@ -61,7 +61,7 @@ def _is_hybrid_equation_block(block: dict[str, Any]) -> bool:
def _is_hybrid_formula_number_block(block: dict[str, Any]) -> bool:
"""判断 raw Hybrid/VLM block 是否表示公式编号。"""
- return str(block.get("type") or "").lower() == BlockType.FORMULA_NUMBER
+ return str(block.get("type") or "").lower() == RAW_FORMULA_NUMBER
def _normalize_hybrid_formula_bbox(bbox: Any) -> list[float] | None:
@@ -222,7 +222,7 @@ def _apply_medium_formula_number_ocr(
image_h, image_w = np_img.shape[:2]
bgr_image = cv2.cvtColor(np_img, cv2.COLOR_RGB2BGR)
for block_item in block_list:
- if block_item.get("type") != BlockType.FORMULA_NUMBER:
+ if block_item.get("type") != RAW_FORMULA_NUMBER:
continue
formula_number_bbox = normalize_to_int_bbox(
diff --git a/mineru/backend/analysis/pdf/tables.py b/mineru/backend/analysis/pdf/tables.py
index b7c33a95..a41ea8e4 100644
--- a/mineru/backend/analysis/pdf/tables.py
+++ b/mineru/backend/analysis/pdf/tables.py
@@ -14,7 +14,7 @@ from loguru import logger
from mineru.backend.local_model_runtime import HybridLocalModelContext, run_ocr_inference
from mineru.model.model_types import AtomicModelName
-from mineru.types import BBox, BlockType, ContentType
+from mineru.types import RAW_FORMULA_NUMBER, BBox, BlockType, ContentType
from mineru.utils.bbox_utils import (
calculate_overlap_area_in_bbox1_area_ratio,
normalize_to_int_bbox,
@@ -535,7 +535,7 @@ def _remove_low_table_inner_blocks(
inner_block_types = {
BlockType.IMAGE,
BlockType.EQUATION,
- BlockType.FORMULA_NUMBER,
+ RAW_FORMULA_NUMBER,
}
for image_dict, page_model_list in zip(images_list, model_list):
page_size = _normalize_page_size(image_dict["img_pil"])
diff --git a/mineru/types.py b/mineru/types.py
index 1d925e96..5e795ac4 100644
--- a/mineru/types.py
+++ b/mineru/types.py
@@ -17,12 +17,14 @@ RawBlockType: TypeAlias = Literal[
"algorithm",
"caption",
"footnote",
+ "formula_number",
"phonetic",
]
RAW_ALGORITHM: RawBlockType = "algorithm"
RAW_CAPTION: RawBlockType = "caption"
RAW_FOOTNOTE: RawBlockType = "footnote"
+RAW_FORMULA_NUMBER: RawBlockType = "formula_number"
RAW_PHONETIC: RawBlockType = "phonetic"
RAW_ONLY_BLOCK_TYPES = frozenset(
@@ -30,6 +32,7 @@ RAW_ONLY_BLOCK_TYPES = frozenset(
RAW_ALGORITHM,
RAW_CAPTION,
RAW_FOOTNOTE,
+ RAW_FORMULA_NUMBER,
RAW_PHONETIC,
}
)
@@ -102,7 +105,6 @@ class BlockType:
# Added in pp_doclayout_v2
DOC_TITLE = "doc_title"
PARAGRAPH_TITLE = "paragraph_title"
- FORMULA_NUMBER = "formula_number"
class ContentType:
@@ -172,7 +174,6 @@ BlockTypes = Literal[
BlockType.PAGE_FOOTNOTE,
BlockType.DOC_TITLE,
BlockType.PARAGRAPH_TITLE,
- BlockType.FORMULA_NUMBER,
]
BLOCK_TYPES = {
@@ -204,7 +205,6 @@ BLOCK_TYPES = {
BlockType.PAGE_FOOTNOTE,
BlockType.DOC_TITLE,
BlockType.PARAGRAPH_TITLE,
- BlockType.FORMULA_NUMBER,
}
VISUAL_RELATION_IGNORED_TYPES = {
@@ -379,11 +379,6 @@ class PageFootnoteBlock(BlockBase):
content: str
-class FormulaNumberBlock(BlockBase):
- type: Literal[BlockType.FORMULA_NUMBER]
- content: str
-
-
class ImagePayloadBlock(BlockBase):
"""直接携带图片 data URI 的 block 基类。"""
@@ -589,7 +584,6 @@ Block: TypeAlias = Annotated[
FooterBlock,
PageNumberBlock,
PageFootnoteBlock,
- FormulaNumberBlock,
EquationBlock,
ListBlock,
IndexBlock,
diff --git a/tests/unittest/test_middle_json_block_models.py b/tests/unittest/test_middle_json_block_models.py
index 0af7ae49..3a315ab9 100644
--- a/tests/unittest/test_middle_json_block_models.py
+++ b/tests/unittest/test_middle_json_block_models.py
@@ -7,7 +7,8 @@ import pytest
from pydantic import ValidationError
import mineru.types as types_module
-from mineru.types import RAW_ONLY_BLOCK_TYPES
+from mineru.backend.postprocess.pages import model_list_to_pages
+from mineru.types import RAW_FORMULA_NUMBER, RAW_ONLY_BLOCK_TYPES
from mineru.types import (
BLOCK_ADAPTER,
BLOCK_TYPES,
@@ -24,13 +25,12 @@ from mineru.types import (
def _public_block_payloads() -> dict[str, dict[str, object]]:
- """构造 29 个公开 discriminator 的最小合法载荷。"""
+ """构造 28 个公开 discriminator 的最小合法载荷。"""
text_leaf = {"type": "text", "content": "item"}
payloads: dict[str, dict[str, object]] = {
"aside_text": {"type": "aside_text", "content": "aside"},
"doc_title": {"type": "doc_title", "content": "title"},
"footer": {"type": "footer", "content": "footer"},
- "formula_number": {"type": "formula_number", "content": "(1)"},
"header": {"type": "header", "content": "header"},
"index": {"type": "index", "content": [text_leaf]},
"equation": {"type": "equation", "content": "x=1"},
@@ -69,12 +69,12 @@ def _public_block_payloads() -> dict[str, dict[str, object]]:
return payloads
-def test_all_29_public_discriminators_parse_to_concrete_models() -> None:
+def test_all_28_public_discriminators_parse_to_concrete_models() -> None:
"""验证公开类型集合与 discriminated union 完整一致。"""
payloads = _public_block_payloads()
assert set(payloads) == BLOCK_TYPES
- assert len(payloads) == 29
+ assert len(payloads) == 28
assert "equation" not in RAW_ONLY_BLOCK_TYPES
assert all(parse_block(payload).type == block_type for block_type, payload in payloads.items())
assert isinstance(parse_block(payloads["equation"]), EquationBlock)
@@ -114,6 +114,22 @@ def test_equation_schema_uses_only_canonical_discriminator() -> None:
assert legacy_model_name not in schema["$defs"]
+def test_formula_number_is_rejected_by_middle_json_boundary_and_schema() -> None:
+ """验证 formula_number 仅属于 Analyze raw 阶段,不能进入公开 Block 或 Middle JSON。"""
+ schema = BLOCK_ADAPTER.json_schema()
+ mapping = schema["discriminator"]["mapping"]
+
+ assert RAW_FORMULA_NUMBER in RAW_ONLY_BLOCK_TYPES
+ assert RAW_FORMULA_NUMBER not in mapping
+ assert "FormulaNumberBlock" not in schema["$defs"]
+ with pytest.raises(ValidationError):
+ parse_block({"type": RAW_FORMULA_NUMBER, "content": "(1)"})
+ with pytest.raises(ValidationError):
+ model_list_to_pages(
+ [[{"type": RAW_FORMULA_NUMBER, "bbox": [0.7, 0.3, 0.8, 0.4], "content": "(1)"}]]
+ )
+
+
def test_cross_type_fields_and_unknown_fields_are_rejected() -> None:
"""验证 Text/RefText 的专属字段边界与 extra forbid。"""
with pytest.raises(ValidationError):
diff --git a/tests/unittest/test_pdf_analyze_visual_blocks.py b/tests/unittest/test_pdf_analyze_visual_blocks.py
index a5a2a3de..8af7b992 100644
--- a/tests/unittest/test_pdf_analyze_visual_blocks.py
+++ b/tests/unittest/test_pdf_analyze_visual_blocks.py
@@ -12,7 +12,7 @@ from PIL import Image, ImageDraw
from mineru.backend import analyze
from mineru.backend.analysis import office
from mineru.backend.analysis.pdf import constants, formulas, normalization, pipeline, tables, visuals, window
-from mineru.types import RAW_ALGORITHM, RAW_CAPTION, RAW_FOOTNOTE, RAW_PHONETIC
+from mineru.types import RAW_ALGORITHM, RAW_CAPTION, RAW_FOOTNOTE, RAW_FORMULA_NUMBER, RAW_PHONETIC
from mineru.types import BlockType, MiddleJson
@@ -681,7 +681,6 @@ def test_normalize_pdf_model_list_preserves_content_after_unclosed_inline_formul
BlockType.CODE,
RAW_ALGORITHM,
BlockType.EQUATION,
- BlockType.FORMULA_NUMBER,
BlockType.IMAGE,
BlockType.CHART,
"unknown",
@@ -737,7 +736,7 @@ def test_formula_number_optimizer_recognizes_canonical_and_upstream_equation_typ
"content": r"\[x+1\]",
},
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.75, 0.32, 0.85, 0.38],
"content": "(2)",
},
@@ -758,7 +757,7 @@ def test_formula_number_optimizer_merges_leading_number_bbox() -> None:
optimized = formulas.optimize_hybrid_formula_number_blocks(
[
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.1, 0.32, 0.2, 0.38],
"content": "(3)",
},
@@ -789,7 +788,7 @@ def test_formula_number_optimizer_merges_empty_content_by_geometry() -> None:
"content": "",
},
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.75, 0.32, 0.85, 0.38],
"content": "",
},
@@ -827,7 +826,7 @@ def test_formula_number_optimizer_preserves_equation_bbox_when_union_is_invalid(
if equation_bbox is not None:
equation["bbox"] = equation_bbox
number = {
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"content": "(4)",
}
if number_bbox is not None:
@@ -845,7 +844,7 @@ def test_formula_number_optimizer_prefers_trailing_number() -> None:
optimized = formulas.optimize_hybrid_formula_number_blocks(
[
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.1, 0.32, 0.2, 0.38],
"content": "(1)",
},
@@ -855,7 +854,7 @@ def test_formula_number_optimizer_prefers_trailing_number() -> None:
"content": "x=2",
},
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.75, 0.32, 0.85, 0.38],
"content": "(2)",
},
@@ -881,7 +880,7 @@ def test_formula_number_optimizer_downgrades_unmatched_number() -> None:
optimized = formulas.optimize_hybrid_formula_number_blocks(
[
{
- "type": BlockType.FORMULA_NUMBER,
+ "type": RAW_FORMULA_NUMBER,
"bbox": [0.75, 0.32, 0.85, 0.38],
"content": "(5)",
}
@@ -902,7 +901,7 @@ def test_formula_number_optimizer_does_not_accept_legacy_interline_equation() ->
optimized = formulas.optimize_hybrid_formula_number_blocks(
[
{"type": "interline_equation", "content": "x+1"},
- {"type": BlockType.FORMULA_NUMBER, "content": "(2)"},
+ {"type": RAW_FORMULA_NUMBER, "content": "(2)"},
]
)