Skip to content

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

feat: replace Literal file suffix types with FileSuffix type alias for consistency

myhloli919e9faac91d
diff --git a/mineru/backend/analyze.py b/mineru/backend/analyze.py
index da2714c1..787452aa 100644
--- a/mineru/backend/analyze.py
+++ b/mineru/backend/analyze.py
@@ -4,7 +4,7 @@
from __future__ import annotations
import asyncio
-from typing import Literal, cast
+from typing import cast
from loguru import logger
@@ -13,10 +13,9 @@ from mineru.backend.analysis.office import analyze_office
from mineru.backend.analysis.pdf.pipeline import analyze_pdf
from mineru.backend.postprocess.document import model_json_to_middle_json
from mineru.config import config
-from mineru.types import MiddleJson, ModelJson
+from mineru.types import FILE_SUFFIXES, FileSuffix, MiddleJson, ModelJson
from mineru.version import __version__ as mineru_version
-_SUPPORTED_FILE_SUFFIXES = {"pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"}
_SUPPORTED_ANALYZE_EFFORTS = {"flash", "medium", "high", "xhigh"}
@@ -35,10 +34,10 @@ def doc_analyze(
parse_mode: ParseMode = "auto",
image_analysis: bool = True,
page_index_map: list[int] | None = None,
- file_suffix: Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"] = "pdf",
+ file_suffix: FileSuffix = "pdf",
) -> tuple[MiddleJson, ModelJson]:
"""生产严格 ModelJson,并在统一边界构造严格 MiddleJson。"""
- if file_suffix not in _SUPPORTED_FILE_SUFFIXES:
+ if file_suffix not in FILE_SUFFIXES:
raise ValueError(f"Unsupported file suffix: {file_suffix!r}")
if effort not in _SUPPORTED_ANALYZE_EFFORTS:
raise ValueError(f"Unsupported analyze effort: {effort}")
@@ -75,7 +74,7 @@ async def aio_doc_analyze(
parse_mode: ParseMode = "auto",
image_analysis: bool = True,
page_index_map: list[int] | None = None,
- file_suffix: Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"] = "pdf",
+ file_suffix: FileSuffix = "pdf",
) -> tuple[MiddleJson, ModelJson]:
"""在线程中执行统一文档分析,避免阻塞调用方事件循环。"""
return await asyncio.to_thread(
diff --git a/mineru/parser/base.py b/mineru/parser/base.py
index 46d93e16..a095a62c 100644
--- a/mineru/parser/base.py
+++ b/mineru/parser/base.py
@@ -12,13 +12,13 @@ from ..render import render_markdown, render_structured_content
from ..render.contracts import RenderMode
from ..render.image import ImageRenderer
from ..render.writer import DataWriter
-from ..types import MiddleJson, PageInfo
+from ..types import FileSuffix, MiddleJson, PageInfo
from ..utils.image_payload import ImagePayloadCache
from ..utils.pdf_document import PDFDocument
MIDDLE_JSON_SCHEMA_VERSION: str = "2.0"
_LEGACY_SCHEMA_VERSION: str = "1.0"
-_LEGACY_DEFAULT_FILE_SUFFIX: Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"] = "pdf"
+_LEGACY_DEFAULT_FILE_SUFFIX: FileSuffix = "pdf"
_LEGACY_DEFAULT_EFFORT: Literal["flash", "medium", "high", "xhigh"] = "medium"
_LEGACY_DEFAULT_PARSE_MODE: Literal["txt", "ocr"] = "txt"
_PDF_RETAINED_PAGE_INDICES_KEY = "_pdf_retained_page_indices"
diff --git a/mineru/parser/mineru_parser.py b/mineru/parser/mineru_parser.py
index c03eb125..cc1778c4 100644
--- a/mineru/parser/mineru_parser.py
+++ b/mineru/parser/mineru_parser.py
@@ -7,12 +7,12 @@ import logging
import time
from dataclasses import dataclass
from pathlib import Path
-from typing import Any, Literal
+from typing import Any, Literal, cast
from ..backend.analyze import aio_doc_analyze, doc_analyze
from ..errors import InvalidRequestError
from ..filetypes import IMAGE_EXTENSIONS
-from ..types import MiddleJson, ModelJson, PageInfo, Tier
+from ..types import FILE_SUFFIXES, FileSuffix, MiddleJson, ModelJson, PageInfo, Tier
from ..utils.backend_options import effort_for_tier
from .base import DocumentParser, ParseResult
@@ -20,17 +20,13 @@ logger = logging.getLogger(__name__)
_Effort = Literal["flash", "medium", "high", "xhigh"]
_ParseMode = Literal["auto", "txt", "ocr"]
-_FileSuffix = Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"]
-_SUPPORTED_SUFFIXES: frozenset[str] = frozenset({"pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"})
-
-
@dataclass
class _PreparedInput:
"""记录文档输入准备结果,避免跨文档复用 parser 实例状态。"""
file_name: str
file_bytes: bytes
- file_suffix: _FileSuffix
+ file_suffix: FileSuffix
retained_page_indices: list[int] | None = None
broken_page_indices: list[int] | None = None
@@ -147,12 +143,12 @@ class MinerUParser(DocumentParser):
suffix,
page_range,
)
- if suffix not in _SUPPORTED_SUFFIXES:
+ if suffix not in FILE_SUFFIXES:
raise ValueError(f"Unsupported file type: {suffix or path.suffix or 'unknown'}")
return _PreparedInput(
file_name=file_name,
file_bytes=file_bytes,
- file_suffix=suffix, # type: ignore[arg-type]
+ file_suffix=cast(FileSuffix, suffix),
retained_page_indices=retained_page_indices,
broken_page_indices=broken_page_indices,
)
diff --git a/mineru/types.py b/mineru/types.py
index 20a593a7..2fef9466 100644
--- a/mineru/types.py
+++ b/mineru/types.py
@@ -9,7 +9,7 @@ from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from tempfile import NamedTemporaryFile
-from typing import Annotated, Any, ClassVar, Literal, TypeAlias, Union
+from typing import Annotated, Any, ClassVar, Literal, TypeAlias, Union, cast, get_args
from pydantic import (
BaseModel,
@@ -45,6 +45,9 @@ RAW_ONLY_BLOCK_TYPES = frozenset(
}
)
+FileSuffix: TypeAlias = Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"]
+FILE_SUFFIXES: frozenset[FileSuffix] = frozenset(cast(tuple[FileSuffix, ...], get_args(FileSuffix)))
+
Tier = Literal[
"flash",
"basic",
@@ -742,7 +745,7 @@ class ModelJson(_StrictMiddleModel):
pages: list[list[dict[str, Any]]]
page_index_map: list[int]
- file_suffix: Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"]
+ file_suffix: FileSuffix
effort: Literal["flash", "medium", "high", "xhigh"]
parse_mode: Literal["txt", "ocr"]
mineru_version: str = Field(min_length=1)
@@ -808,7 +811,7 @@ class MiddleJson(_StrictMiddleModel):
pages: list[PageInfo]
is_full_document: bool
- file_suffix: Literal["pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx"]
+ file_suffix: FileSuffix
effort: Literal["flash", "medium", "high", "xhigh"]
parse_mode: Literal["txt", "ocr"]
mineru_version: str = Field(min_length=1)
diff --git a/tests/unittest/test_pdf_analyze_visual_blocks.py b/tests/unittest/test_pdf_analyze_visual_blocks.py
index dddefeea..02ccf684 100644
--- a/tests/unittest/test_pdf_analyze_visual_blocks.py
+++ b/tests/unittest/test_pdf_analyze_visual_blocks.py
@@ -17,7 +17,7 @@ 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_FORMULA_NUMBER, RAW_PHONETIC
-from mineru.types import BlockType, MiddleJson, ModelJson
+from mineru.types import FILE_SUFFIXES, BlockType, FileSuffix, MiddleJson, ModelJson
from mineru.version import __version__ as mineru_version
@@ -1197,10 +1197,15 @@ def test_aio_doc_analyze_matches_sync_signature() -> None:
def test_doc_analyze_effort_annotation_exposes_only_supported_values() -> None:
- """验证同步和异步 Analyze 门面仅公开四个有效 effort。"""
+ """验证同步和异步 Analyze 门面复用统一的 effort 与文件后缀类型。"""
expected_efforts = ("flash", "medium", "high", "xhigh")
+ expected_suffixes = ("pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx")
assert get_args(get_type_hints(analyze.doc_analyze)["effort"]) == expected_efforts
assert get_args(get_type_hints(analyze.aio_doc_analyze)["effort"]) == expected_efforts
+ assert get_args(FileSuffix) == expected_suffixes
+ assert get_args(get_type_hints(analyze.doc_analyze)["file_suffix"]) == expected_suffixes
+ assert get_args(get_type_hints(analyze.aio_doc_analyze)["file_suffix"]) == expected_suffixes
+ assert FILE_SUFFIXES == frozenset(expected_suffixes)
def test_aio_doc_analyze_runs_sync_entrypoint_in_thread_and_forwards_arguments(