Skip to content

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

feat(llm): allow configured title leveling on selected pages

myhloli17701846e485
diff --git a/docs/next/config.md b/docs/next/config.md
index 8eb458fd..10136d73 100644
--- a/docs/next/config.md
+++ b/docs/next/config.md
@@ -122,14 +122,18 @@ llm_aided:
model: qwen3.8-flash
enable_thinking: false
max_concurrency: 16
+ allow_partial_title_leveling: false
features:
title_leveling: false
cross_page_table_cell_merge: false
```
两个功能默认关闭并共享同一套 OpenAI-compatible 连接参数。`title_leveling` 在严格
-`MiddleJson.pages` 上优化标题层级,但只在 `MiddleJson.is_full_document=true` 的整本 PDF 输入中运行;
-抽页结果会跳过标题优化。`cross_page_table_cell_merge` 不受整本输入限制,只消费确定性后处理
+`MiddleJson.pages` 上优化标题层级,默认仅对 `MiddleJson.is_full_document=true` 的整本 PDF 执行。
+设置 `allow_partial_title_leveling: true` 后,抽页结果也可根据本次选中的标题建立局部层级,
+包括 Demo 默认截取的前 20 页、中间片段和非连续选页。抽页结果的 `is_full_document` 为 `false`,
+页码对应原文件。`features.title_leveling` 是标题分级的总开关。
+`cross_page_table_cell_merge` 只消费确定性后处理
已经写入的 `continues_prev`,再把逐单元格的 `0/1` 结果写入后表根块 `cell_merge`。两个功能
通过同一个异步客户端并发请求,`max_concurrency` 是不小于 1 的共享请求上限,默认值为 16。
标题分级结果允许 1 到 6,段落标题的 1 会归一为 2,最终严格标题层级范围为 2 到 6。前置
@@ -138,6 +142,7 @@ MinerU VLM 的跨页单元格检测保持关闭。任一功能启用时,`api_k
环境变量可覆盖任意强类型字段,例如 `MINERU_LLM_AIDED_API_KEY`、
`MINERU_LLM_AIDED_MAX_CONCURRENCY`、
+`MINERU_LLM_AIDED_ALLOW_PARTIAL_TITLE_LEVELING`、
`MINERU_LLM_AIDED_FEATURES_TITLE_LEVELING` 和
`MINERU_LLM_AIDED_FEATURES_CROSS_PAGE_TABLE_CELL_MERGE`。
diff --git a/mineru/backend/postprocess/llm_aided.py b/mineru/backend/postprocess/llm_aided.py
index a673b9d8..faa5de90 100644
--- a/mineru/backend/postprocess/llm_aided.py
+++ b/mineru/backend/postprocess/llm_aided.py
@@ -22,8 +22,10 @@ def _resolve_enabled_features(
*,
allow_partial_title_leveling: bool = False,
) -> tuple[bool, bool]:
- """根据整本输入约束解析本次实际启用的标题与表格功能。"""
- title_enabled = config.features.title_leveling and (middle_json.is_full_document or allow_partial_title_leveling)
+ """按整本标记、启动配置和显式放行参数确定标题与表格功能。"""
+ title_enabled = config.features.title_leveling and (
+ middle_json.is_full_document or config.allow_partial_title_leveling or allow_partial_title_leveling
+ )
table_enabled = config.features.cross_page_table_cell_merge
if config.features.title_leveling and not title_enabled:
logger.info("Skipping LLM title leveling because the input is not a full document")
diff --git a/mineru/config.py b/mineru/config.py
index 056378fd..ac1bdc5d 100644
--- a/mineru/config.py
+++ b/mineru/config.py
@@ -486,6 +486,7 @@ class LLMAidedConfig(BaseModel):
model: str = "qwen3.8-flash"
enable_thinking: bool | None = None
max_concurrency: int = Field(default=16, ge=1)
+ allow_partial_title_leveling: bool = False
features: LLMAidedFeaturesConfig = Field(default_factory=LLMAidedFeaturesConfig)
@field_validator("max_concurrency", mode="before")
diff --git a/tests/unittest/test_doclib_config.py b/tests/unittest/test_doclib_config.py
index c16675c3..5143cec7 100644
--- a/tests/unittest/test_doclib_config.py
+++ b/tests/unittest/test_doclib_config.py
@@ -178,6 +178,7 @@ def test_llm_aided_config_defaults_to_disabled() -> None:
assert llm_config.model == "qwen3.8-flash"
assert llm_config.enable_thinking is None
assert llm_config.max_concurrency == 16
+ assert llm_config.allow_partial_title_leveling is False
assert llm_config.features.title_leveling is False
assert llm_config.features.cross_page_table_cell_merge is False
@@ -193,6 +194,7 @@ llm_aided:
model: test-model
enable_thinking: false
max_concurrency: 24
+ allow_partial_title_leveling: true
features:
title_leveling: true
cross_page_table_cell_merge: true
@@ -207,6 +209,7 @@ llm_aided:
assert cfg.llm_aided.model == "test-model"
assert cfg.llm_aided.enable_thinking is False
assert cfg.llm_aided.max_concurrency == 24
+ assert cfg.llm_aided.allow_partial_title_leveling is True
assert cfg.llm_aided.features.title_leveling is True
assert cfg.llm_aided.features.cross_page_table_cell_merge is True
@@ -218,6 +221,7 @@ def test_llm_aided_config_supports_environment_overrides(monkeypatch: pytest.Mon
monkeypatch.setenv(f"{prefix}LLM_AIDED_MODEL", "env-model")
monkeypatch.setenv(f"{prefix}LLM_AIDED_ENABLE_THINKING", "false")
monkeypatch.setenv(f"{prefix}LLM_AIDED_MAX_CONCURRENCY", "32")
+ monkeypatch.setenv(f"{prefix}LLM_AIDED_ALLOW_PARTIAL_TITLE_LEVELING", "true")
monkeypatch.setenv(f"{prefix}LLM_AIDED_FEATURES_TITLE_LEVELING", "true")
monkeypatch.setenv(f"{prefix}LLM_AIDED_FEATURES_CROSS_PAGE_TABLE_CELL_MERGE", "true")
@@ -228,6 +232,7 @@ def test_llm_aided_config_supports_environment_overrides(monkeypatch: pytest.Mon
assert cfg.llm_aided.model == "env-model"
assert cfg.llm_aided.enable_thinking is False
assert cfg.llm_aided.max_concurrency == 32
+ assert cfg.llm_aided.allow_partial_title_leveling is True
assert cfg.llm_aided.features.title_leveling is True
assert cfg.llm_aided.features.cross_page_table_cell_merge is True
diff --git a/tests/unittest/test_llm_aided_postprocess.py b/tests/unittest/test_llm_aided_postprocess.py
index 8c4bd22e..3d04c66f 100644
--- a/tests/unittest/test_llm_aided_postprocess.py
+++ b/tests/unittest/test_llm_aided_postprocess.py
@@ -13,7 +13,7 @@ from bs4 import BeautifulSoup
from mineru.backend.analysis.contracts import AnalysisResult
from mineru.backend.postprocess.llm_client import LLMAidedClient
-from mineru.backend.postprocess.llm_aided import apply_llm_aided_postprocess
+from mineru.backend.postprocess.llm_aided import aio_apply_llm_aided_postprocess, apply_llm_aided_postprocess
from mineru.backend.postprocess.table_merge.llm_cell_merge import apply_llm_cross_page_cell_merge
from mineru.backend.postprocess.title_leveling import apply_llm_title_leveling
from mineru.config import LLMAidedConfig, LLMAidedFeaturesConfig
@@ -795,6 +795,58 @@ def test_partial_input_skips_title_but_keeps_table_cell_merge() -> None:
assert pages[1].blocks[0].cell_merge == [0, 1] # type: ignore[union-attr]
[email protected]("asynchronous", [False, True])
[email protected]("configured,explicit", [(False, False), (True, False), (False, True), (True, True)])
[email protected]("indices", [tuple(range(20)), (4, 5, 6), (0, 4, 19)])
+def test_partial_title_scope_preserves_document_identity(
+ asynchronous: bool, configured: bool, explicit: bool, indices: tuple[int, ...]
+) -> None:
+ """同步和异步分级按配置或显式参数放行,覆盖截断、中间片段和非连续原页号。"""
+ titles = [
+ ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=_inline(f"Section {index}"), level=2)
+ for index in indices
+ ]
+ middle = _middle_json(
+ [PageInfo(page_idx=index, blocks=[title]) for index, title in zip(indices, titles)], is_full_document=False
+ )
+ metadata = middle.metadata.model_dump()
+ config = _llm_config(title=True).model_copy(update={"allow_partial_title_leveling": configured})
+ client = _QueuedValidatedClient([{str(index): 3 for index in range(len(titles))}])
+ if asynchronous:
+ asyncio.run(aio_apply_llm_aided_postprocess(
+ middle, config, client=client, allow_partial_title_leveling=explicit # type: ignore[arg-type]
+ ))
+ else:
+ apply_llm_aided_postprocess(
+ middle, config, client=client, allow_partial_title_leveling=explicit # type: ignore[arg-type]
+ )
+ enabled = configured or explicit
+ assert client.operations == (["title_leveling"] if enabled else [])
+ assert [title.level for title in titles] == [3 if enabled else 2] * len(titles)
+ assert middle.is_full_document is False
+ assert [page.page_idx for page in middle.pages] == list(indices)
+ assert middle.metadata.model_dump() == metadata
+
+
[email protected]("asynchronous", [False, True])
[email protected]("title_enabled", [False, True])
+def test_partial_title_policy_respects_main_switch_and_empty_titles(asynchronous: bool, title_enabled: bool) -> None:
+ """放行部分文档仍由标题总开关控制,空标题集合不发送模型请求。"""
+ pages = []
+ if not title_enabled:
+ title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=_inline("Section"), level=2)
+ pages = [PageInfo(page_idx=4, blocks=[title])]
+ middle = _middle_json(pages, is_full_document=False)
+ config = _llm_config(title=title_enabled).model_copy(update={"allow_partial_title_leveling": True})
+ client = _QueuedValidatedClient([])
+ if asynchronous:
+ asyncio.run(aio_apply_llm_aided_postprocess(middle, config, client=client)) # type: ignore[arg-type]
+ else:
+ apply_llm_aided_postprocess(middle, config, client=client) # type: ignore[arg-type]
+ assert client.operations == []
+ assert middle.is_full_document is False
+
+
def test_front_vlm_cell_merge_detection_remains_disabled() -> None:
"""守卫前置 MinerUClient 的跨页 cell_merge 生成机制永久关闭。"""
runtime_path = Path(__file__).resolve().parents[2] / "mineru/model/vlm/runtime.py"