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
from __future__ import annotations
from docvortex.schema import Producer
from mineru.integrations.docvortex import build_metadata
from _span_test_utils import inline as _inline, inline_text
import asyncio
from pathlib import Path
from types import SimpleNamespace
from typing import Any
import pytest
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 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
from docvortex.render._internal.common.planner import build_render_plan
from mineru.render import render_html
from mineru.render.contracts import RenderMode
from mineru.types import (
BlockType,
DocTitleBlock,
MiddleJson,
PageInfo,
ParagraphTitleBlock,
TableBlock,
TableBodyBlock,
)
class _QueuedValidatedClient:
"""按队列返回测试响应,并复用生产代码传入的 validator。"""
def __init__(self, responses: list[Any]) -> None:
"""保存待校验响应及请求记录。"""
self.responses = list(responses)
self.operations: list[str] = []
self.prompts: list[str] = []
async def request_validated_json(
self,
*,
operation: str,
prompt: str,
validator: Any,
temperature: float,
) -> Any:
"""记录操作并把下一项测试响应交给生产 validator。"""
self.operations.append(operation)
self.prompts.append(prompt)
assert prompt
assert temperature >= 0
if not self.responses:
return None
return validator(self.responses.pop(0))
class _FakeCompletions:
"""模拟 OpenAI Chat Completions 调用并记录请求参数。"""
def __init__(self, contents: list[str]) -> None:
"""按顺序保存每次调用应返回的消息文本。"""
self.contents = list(contents)
self.calls: list[dict[str, Any]] = []
async def create(self, **kwargs: Any) -> Any:
"""记录参数并返回最小非流式 completion 对象。"""
self.calls.append(kwargs)
content = self.contents.pop(0)
return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content=content))])
class _ConcurrentValidatedClient:
"""让所有领域请求在返回前汇合,以验证跨功能并发执行。"""
def __init__(self, expected_calls: int) -> None:
"""保存预期请求数及并发观测状态。"""
self.expected_calls = expected_calls
self.operations: list[str] = []
self.active = 0
self.peak_active = 0
self._all_started = asyncio.Event()
async def request_validated_json(
self,
*,
operation: str,
prompt: str,
validator: Any,
temperature: float,
) -> Any:
"""等待全部测试请求进入后再按领域返回合法响应。"""
assert prompt
assert temperature >= 0
self.operations.append(operation)
self.active += 1
self.peak_active = max(self.peak_active, self.active)
if len(self.operations) == self.expected_calls:
self._all_started.set()
try:
await asyncio.wait_for(self._all_started.wait(), timeout=1)
response = {"0": 3} if operation == "title_leveling" else [1, 0]
return validator(response)
finally:
self.active -= 1
class _ConcurrencyCompletions:
"""阻塞底层 completion,以观测共享客户端的并发上限。"""
def __init__(self, expected_limit: int) -> None:
"""初始化请求计数、峰值和释放事件。"""
self.expected_limit = expected_limit
self.calls = 0
self.active = 0
self.peak_active = 0
self.limit_reached = asyncio.Event()
self.release = asyncio.Event()
async def create(self, **kwargs: Any) -> Any:
"""记录并发请求,并在测试释放前保持占用。"""
assert kwargs
self.calls += 1
self.active += 1
self.peak_active = max(self.peak_active, self.active)
if self.active == self.expected_limit:
self.limit_reached.set()
try:
await self.release.wait()
return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content="[0]"))])
finally:
self.active -= 1
def _llm_config(
*,
title: bool = False,
table: bool = False,
enable_thinking: bool | None = None,
max_concurrency: int = 16,
) -> LLMAidedConfig:
"""构造已提供测试凭据的强类型 LLM 配置。"""
return LLMAidedConfig(
api_key="test-key",
base_url="https://example.test/v1",
model="test-model",
enable_thinking=enable_thinking,
max_concurrency=max_concurrency,
features=LLMAidedFeaturesConfig(
title_leveling=title,
cross_page_table_cell_merge=table,
),
)
def _table(index: int, html: str, *, continues_prev: bool = False) -> TableBlock:
"""构造严格的两层表格块。"""
bbox = (0.1, 0.1, 0.9, 0.9)
return TableBlock(
type=BlockType.TABLE,
index=index,
bbox=bbox,
continues_prev=True if continues_prev else None,
content=[
TableBodyBlock(
type=BlockType.TABLE_BODY,
index=index,
bbox=bbox,
content=html,
)
],
)
def _cross_page_table_pages(
*,
continues_prev: bool = True,
previous_html: str | None = None,
current_html: str | None = None,
) -> list[PageInfo]:
"""构造包含重复表头和两个数据行的严格跨页表格页面。"""
previous_html = previous_html or ("<table><tr><th>H1</th><th>H2</th></tr><tr><td>A</td><td>X</td></tr></table>")
current_html = current_html or ("<table><tr><th>H1</th><th>H2</th></tr><tr><td>B</td><td>Y</td></tr></table>")
return [
PageInfo(page_idx=0, blocks=[_table(0, previous_html)]),
PageInfo(page_idx=1, blocks=[_table(0, current_html, continues_prev=continues_prev)]),
]
def _raw_table(html: str, bbox: list[float]) -> dict[str, Any]:
"""构造 doc_analyze 集成测试使用的 raw table model block。"""
return {
"type": BlockType.TABLE,
"bbox": bbox,
"content": html,
}
def _merged_row_texts(middle_json: MiddleJson) -> list[list[str]]:
"""通过真实 render plan 读取跨页合并后的逐行单元格文本。"""
plan = build_render_plan(middle_json, RenderMode.DEFAULT)
merged_table = plan[0][0].block
assert isinstance(merged_table, TableBlock)
body = next(child for child in merged_table.content if isinstance(child, TableBodyBlock))
soup = BeautifulSoup(body.content, "html.parser")
return [[cell.get_text() for cell in row.find_all(["td", "th"])] for row in soup.find_all("tr")]
def _middle_json(pages: list[PageInfo], *, is_full_document: bool = True) -> MiddleJson:
"""为 LLM 后处理单元测试构造最小严格 MiddleJson。"""
return MiddleJson(
pages=pages,
is_full_document=is_full_document,
metadata={"file_suffix": "docx", "producer": Producer(name="mineru", version="test")},
extensions=build_metadata(
effort="flash",
parse_mode="txt",
),
)
def test_llm_client_injects_optional_thinking_parameter() -> None:
"""验证显式 enable_thinking 通过 extra_body 发送且返回经过结构校验。"""
completions = _FakeCompletions(['</think>{"0": 2}'])
openai_client = SimpleNamespace(chat=SimpleNamespace(completions=completions))
client = LLMAidedClient(_llm_config(title=True, enable_thinking=False), client=openai_client)
result = asyncio.run(
client.request_validated_json(
operation="test",
prompt="prompt",
validator=lambda value: value if value == {"0": 2} else None,
temperature=0.1,
)
)
assert result == {"0": 2}
assert completions.calls[0]["extra_body"] == {"enable_thinking": False}
def test_llm_client_omits_unspecified_thinking_parameter() -> None:
"""验证未配置 enable_thinking 时不向兼容服务发送供应商参数。"""
completions = _FakeCompletions(["[0]"])
openai_client = SimpleNamespace(chat=SimpleNamespace(completions=completions))
client = LLMAidedClient(_llm_config(table=True), client=openai_client)
result = asyncio.run(
client.request_validated_json(
operation="test",
prompt="prompt",
validator=lambda value: value if value == [0] else None,
temperature=0.1,
)
)
assert result == [0]
assert "extra_body" not in completions.calls[0]
def test_llm_client_retries_invalid_json_structure() -> None:
"""验证共享请求层会对结构校验失败的响应重试并返回后续合法结果。"""
completions = _FakeCompletions(["{}", "[0, 1]"])
openai_client = SimpleNamespace(chat=SimpleNamespace(completions=completions))
client = LLMAidedClient(_llm_config(table=True), client=openai_client)
result = asyncio.run(
client.request_validated_json(
operation="test",
prompt="prompt",
validator=lambda value: value if value == [0, 1] else None,
temperature=0.1,
)
)
assert result == [0, 1]
assert len(completions.calls) == 2
@pytest.mark.parametrize(("configured_limit", "request_count"), [(3, 4), (16, 17)])
def test_llm_client_uses_configured_shared_concurrency_limit(configured_limit: int, request_count: int) -> None:
"""验证默认和非默认并发数都会控制所有领域共用的底层请求槽。"""
completions = _ConcurrencyCompletions(configured_limit)
openai_client = SimpleNamespace(chat=SimpleNamespace(completions=completions))
client = LLMAidedClient(
_llm_config(title=True, max_concurrency=configured_limit),
client=openai_client,
)
async def _run() -> list[list[int] | None]:
"""并发发起超过上限的请求,并在额外请求进入前检查限流状态。"""
tasks = [
asyncio.create_task(
client.request_validated_json(
operation="test",
prompt=f"prompt-{index}",
validator=lambda value: value if value == [0] else None,
temperature=0.1,
)
)
for index in range(request_count)
]
await asyncio.wait_for(completions.limit_reached.wait(), timeout=1)
await asyncio.sleep(0)
assert completions.calls == configured_limit
assert completions.peak_active == configured_limit
completions.release.set()
return await asyncio.gather(*tasks)
assert asyncio.run(_run()) == [[0]] * request_count
def test_title_leveling_uses_current_page_blocks_and_keeps_global_levels() -> None:
"""验证当前严格标题块可直接分级且文档标题固定为一级。"""
pages = [
PageInfo(
page_idx=0,
blocks=[
DocTitleBlock(
type=BlockType.DOC_TITLE,
index=0,
bbox=(0.1, 0.1, 0.9, 0.2),
content=_inline("Document"),
level=1,
),
ParagraphTitleBlock(
type=BlockType.PARAGRAPH_TITLE,
index=1,
bbox=(0.1, 0.3, 0.9, 0.35),
content=_inline("Section"),
level=3,
),
],
)
]
client = _QueuedValidatedClient([{"0": 1}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert pages[0].blocks[0].level == 1 # type: ignore[union-attr]
assert pages[0].blocks[1].level == 2 # type: ignore[union-attr]
assert client.operations == ["title_leveling"]
def test_title_leveling_keeps_original_levels_for_invalid_response() -> None:
"""验证单个标题组响应缺项时该组标题层级保持不变。"""
title = ParagraphTitleBlock(
type=BlockType.PARAGRAPH_TITLE,
index=0,
bbox=(0.1, 0.1, 0.9, 0.2),
content=_inline("Section"),
level=3,
)
pages = [PageInfo(page_idx=0, blocks=[title])]
client = _QueuedValidatedClient([{}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert title.level == 3
def test_title_leveling_groups_by_doc_title_and_uses_plain_content_only() -> None:
"""验证文档标题切组、组内重新编号,并且提示词只含段落标题纯文本。"""
styled_content = _inline("Leading", styles=["bold"])
leading = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=styled_content, level=2)
first = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=2, content=_inline("First"), level=2)
second = ParagraphTitleBlock(
type=BlockType.PARAGRAPH_TITLE,
index=3,
content=_inline("Second", styles=["italic"]),
level=3,
)
third = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=6, content=_inline("Third"), level=2)
pages = [
PageInfo(
page_idx=0,
blocks=[
leading,
DocTitleBlock(type=BlockType.DOC_TITLE, index=1, content=_inline("Document A"), level=1),
first,
second,
DocTitleBlock(type=BlockType.DOC_TITLE, index=4, content=_inline("Document B"), level=1),
DocTitleBlock(type=BlockType.DOC_TITLE, index=5, content=_inline("Document C"), level=1),
third,
],
)
]
client = _QueuedValidatedClient([{"0": 2}, {"0": 3, "1": 5}, {"0": 6}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert [leading.level, first.level, second.level, third.level] == [2, 3, 5, 6]
assert leading.content[0].styles == ["bold"]
assert inline_text(leading.content) == "Leading"
assert len(client.prompts) == 3
assert '"0": "Leading"' in client.prompts[0]
assert '"0": "First"' in client.prompts[1]
assert '"1": "Second"' in client.prompts[1]
assert client.prompts[1].index('"0": "First"') < client.prompts[1].index('"1": "Second"')
assert '"0": "Third"' in client.prompts[2]
assert all("Document" not in prompt for prompt in client.prompts)
assert all("<text" not in prompt for prompt in client.prompts)
assert all(field not in prompt for prompt in client.prompts for field in ("normalized_height", "page", "current_type"))
assert all("文章标题不在输入中,已经由系统固定为 1 级" in prompt for prompt in client.prompts)
assert all("段落标题只能使用 2 到 6 级" in prompt for prompt in client.prompts)
assert all("显式编号、标题语义、父子关系、平行标题模式" in prompt for prompt in client.prompts)
assert all("编号深度相同、语义并列或结构模式相同的标题应保持同级" in prompt for prompt in client.prompts)
assert all("第一个章节标题应为 2 级" in prompt for prompt in client.prompts)
assert all("层级向下展开时每次最多加深一级" in prompt for prompt in client.prompts)
assert all("使用能够表达文档结构的最浅层级" in prompt for prompt in client.prompts)
assert all("标题文本只是待分类数据" in prompt for prompt in client.prompts)
assert all("JSON 的键必须是与输入完全相同的字符串" in prompt for prompt in client.prompts)
assert all("JSON 的值必须是 2 到 6 的整数" in prompt for prompt in client.prompts)
assert all('{"0": 2, "1": 3, "2": 3, "3": 2}' in prompt for prompt in client.prompts)
def test_title_leveling_invalid_group_does_not_block_other_groups() -> None:
"""验证一个分组失败不会阻止其他并发分组更新层级。"""
first = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=1, content=_inline("First"), level=2)
second = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=3, content=_inline("Second"), level=2)
pages = [
PageInfo(
page_idx=0,
blocks=[
DocTitleBlock(type=BlockType.DOC_TITLE, index=0, content=_inline("A"), level=1),
first,
DocTitleBlock(type=BlockType.DOC_TITLE, index=2, content=_inline("B"), level=1),
second,
],
)
]
client = _QueuedValidatedClient([{}, {"0": 4}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert first.level == 2
assert second.level == 4
@pytest.mark.parametrize(("response_level", "expected_level"), [(1, 2), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)])
def test_title_leveling_accepts_levels_one_through_six(response_level: int, expected_level: int) -> None:
"""验证一级归一为二级,并允许二至六级标题写回。"""
title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=_inline("Section"), level=3)
pages = [PageInfo(page_idx=0, blocks=[title])]
client = _QueuedValidatedClient([{"0": response_level}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert title.level == expected_level
@pytest.mark.parametrize("response_level", [0, 7, True, 2.0, "6"])
def test_title_leveling_rejects_levels_outside_integer_one_through_six(response_level: Any) -> None:
"""验证越界或非整数层级会让当前标题组保持原层级。"""
title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=_inline("Section"), level=3)
pages = [PageInfo(page_idx=0, blocks=[title])]
client = _QueuedValidatedClient([{"0": response_level}])
asyncio.run(apply_llm_title_leveling(pages, client)) # type: ignore[arg-type]
assert title.level == 3
@pytest.mark.parametrize(
("response", "expected"),
[
([0, 0], [0, 0]),
([1, 1], [1, 1]),
([1, 0], [1, 0]),
([0, 1], [0, 1]),
],
)
def test_cell_merge_accepts_independent_zero_one_states(response: list[int], expected: list[int]) -> None:
"""验证全零、全一和混合状态都会新增到后表根块。"""
pages = _cross_page_table_pages()
client = _QueuedValidatedClient([response])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
current_table = pages[1].blocks[0]
assert isinstance(current_table, TableBlock)
assert current_table.cell_merge == expected
def test_cell_merge_expands_rendered_colspan_segment_to_visual_columns() -> None:
"""验证一个 colspan 渲染单元格的状态会展开到其覆盖的全部视觉列。"""
pages = _cross_page_table_pages(
previous_html=("<table><tr><th colspan='2'>H</th></tr><tr><td colspan='2'>A</td></tr></table>"),
current_html=("<table><tr><th colspan='2'>H</th></tr><tr><td colspan='2'>B</td></tr></table>"),
)
client = _QueuedValidatedClient([[1]])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
current_table = pages[1].blocks[0]
assert isinstance(current_table, TableBlock)
assert current_table.cell_merge == [1, 1]
@pytest.mark.parametrize("model_flags,expected", [([0, 0, 0], [0, 0, 0]), ([0, 1, 0], [1, 1, 1])])
def test_blank_merge_rule_requires_a_model_continuation(model_flags: list[int], expected: list[int]) -> None:
"""模型至少确认一列后才补空白延续,其余零标记均为双空格时补齐。"""
original_flags = list(model_flags)
pages = _cross_page_table_pages(
previous_html="<table><tr><td>Group</td><td>sentence</td><td></td></tr></table>",
current_html="<table><tr><td> </td><td>continues</td><td></td></tr></table>",
)
client = _QueuedValidatedClient([model_flags])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == expected
assert model_flags == original_flags
@pytest.mark.parametrize("content", ['<img src="image.png"/>', '<math alttext="x+y"></math>', '<eq data-latex="x"></eq>'])
def test_blank_merge_rule_preserves_non_text_cells(content: str) -> None:
"""无可提取文字的图片或公式格具有实际内容,不作为空白规则的补合并对象。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td>Previous</td><td>sentence</td></tr></table>",
current_html=f"<table><tr><td>{content}</td><td>continues</td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([[0, 1]]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [0, 1]
def test_blank_merge_rule_expands_colspan_after_completing_segments() -> None:
"""规则先在渲染单元格上补标记,再展开跨列范围,避免错用视觉列下标。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td colspan='2'>Group</td><td>sentence</td></tr></table>",
current_html="<table><tr><td colspan='2'></td><td>continues</td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([[0, 1]]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [1, 1, 1]
def test_blank_merge_rule_uses_inherited_nonempty_cell() -> None:
"""上页末行继承的共享单元格仍按其实际内容参与空白续接规则。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td rowspan='2'>Group</td><td>First</td></tr><tr><td>sentence</td></tr></table>",
current_html="<table><tr><td rowspan='2'></td><td>continues</td></tr><tr><td>Next</td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([[0, 1]]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [1, 1]
def test_blank_merge_rule_recognizes_nonempty_image_source() -> None:
"""上页图片格作为非空来源,在其他列确认续接后可连接下页空白区域。"""
pages = _cross_page_table_pages(
previous_html='<table><tr><td><img src="image.png"/></td><td>sentence</td></tr></table>',
current_html="<table><tr><td></td><td>continues</td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([[0, 1]]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [1, 1]
@pytest.mark.parametrize("flags", [[1, 1, 0, 0], [1, 1, 1, 0]])
def test_both_blank_merge_accepts_half_or_more_confirmed_cells(flags: list[int]) -> None:
"""其余零标记全部为双空格时,恰好一半和超过半数的续接均可补齐。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td>A</td><td>B</td><td></td><td></td></tr></table>",
current_html="<table><tr><td>C</td><td>D</td><td></td><td></td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([flags]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [1, 1, 1, 1]
@pytest.mark.parametrize("flags,expected", [([0, 0, 0, 0], [0, 0, 0, 0]), ([1, 0, 0, 0], [1, 1, 1, 1])])
def test_both_blank_merge_accepts_one_confirmed_cell(flags: list[int], expected: list[int]) -> None:
"""一列文字确认续接时补齐另外三列双空格,全零响应保持原结果。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td>Continued</td><td></td><td></td><td></td></tr></table>",
current_html="<table><tr><td>text</td><td></td><td></td><td></td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([flags]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == expected
@pytest.mark.parametrize("separate", ["New item", '<img src="image.png"/>', '<eq data-latex="x"></eq>'])
def test_both_blank_merge_stops_when_an_independent_cell_remains(separate: str) -> None:
"""已有合并时,只要零标记中还有非空或媒体单元格,就保留其余双空格边界。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td>A</td><td>B</td><td>C</td><td></td><td></td></tr></table>",
current_html=f"<table><tr><td>D</td><td>E</td><td>F</td><td>{separate}</td><td></td></tr></table>",
)
flags = [1, 1, 1, 0, 0]
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([flags]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == flags
def test_both_blank_merge_expands_colspan_after_completing_segments() -> None:
"""跨列单元格确认续接后补齐剩余双空格,并正确展开所有视觉列。"""
pages = _cross_page_table_pages(
previous_html="<table><tr><td colspan='4'>A</td><td></td><td></td></tr></table>",
current_html="<table><tr><td colspan='4'>B</td><td></td><td></td></tr></table>",
)
asyncio.run(apply_llm_cross_page_cell_merge(pages, _QueuedValidatedClient([[1, 0, 0]]))) # type: ignore[arg-type]
assert pages[1].blocks[0].cell_merge == [1, 1, 1, 1, 1, 1]
@pytest.mark.parametrize("response", [[], [1], [1, 2], [True, False], "[1, 0]"])
def test_invalid_cell_merge_response_leaves_field_absent(response: Any) -> None:
"""验证非法列表不会在后表新增 cell_merge。"""
pages = _cross_page_table_pages()
client = _QueuedValidatedClient([response])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
current_table = pages[1].blocks[0]
assert isinstance(current_table, TableBlock)
assert current_table.cell_merge is None
assert "cell_merge" not in current_table.model_fields_set
def test_unmarked_table_does_not_request_cell_merge() -> None:
"""验证未被确定性规则标记的相邻表格不会触发后置 LLM。"""
pages = _cross_page_table_pages(continues_prev=False)
client = _QueuedValidatedClient([[1, 0]])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
assert client.operations == []
def test_mixed_cell_merge_flows_through_strict_middle_json_and_renderer() -> None:
"""验证混合状态经严格 MiddleJson 后由真实 renderer 逐单元格消费。"""
pages = _cross_page_table_pages()
client = _QueuedValidatedClient([[1, 0]])
asyncio.run(apply_llm_cross_page_cell_merge(pages, client)) # type: ignore[arg-type]
middle_json = MiddleJson(
pages=pages,
is_full_document=True,
metadata={"file_suffix": "pdf", "producer": Producer(name="mineru", version="test")},
extensions=build_metadata(
effort="high",
parse_mode="txt",
),
)
assert _merged_row_texts(middle_json) == [["H1", "H2"], ["A B", "X"], ["Y"]]
soup = BeautifulSoup(render_html(middle_json), "html.parser")
assert soup.find_all("tr")[1].td["rowspan"] == "2"
def test_postprocess_reuses_one_client_for_both_features() -> None:
"""验证标题与表格功能复用同一个后置请求客户端。"""
table_pages = _cross_page_table_pages()
previous_table = (
table_pages[0]
.blocks[0]
.model_copy( # type: ignore[union-attr]
update={
"index": 1,
"content": [table_pages[0].blocks[0].content[0].model_copy(update={"index": 1})], # type: ignore[union-attr]
}
)
)
pages = [
PageInfo(
page_idx=0,
blocks=[
ParagraphTitleBlock(
type=BlockType.PARAGRAPH_TITLE,
index=0,
bbox=(0.1, 0.01, 0.9, 0.05),
content=_inline("Section"),
level=2,
),
previous_table,
],
),
table_pages[1],
]
client = _QueuedValidatedClient([{"0": 3}, [0, 1]])
middle_json = _middle_json(pages)
apply_llm_aided_postprocess(
middle_json,
_llm_config(title=True, table=True),
client=client, # type: ignore[arg-type]
)
assert client.operations == ["title_leveling", "cross_page_table_cell_merge"]
assert pages[0].blocks[0].level == 3 # type: ignore[union-attr]
assert pages[1].blocks[0].cell_merge == [0, 1] # type: ignore[union-attr]
def test_title_groups_and_table_candidates_run_concurrently() -> None:
"""验证多个标题组和表格候选会跨功能同时请求共享客户端。"""
first_pair = _cross_page_table_pages()
second_pair = _cross_page_table_pages()
first_previous_table = (
first_pair[0]
.blocks[0]
.model_copy( # type: ignore[union-attr]
update={
"index": 2,
"content": [first_pair[0].blocks[0].content[0].model_copy(update={"index": 2})], # type: ignore[union-attr]
}
)
)
second_previous_table = (
second_pair[0]
.blocks[0]
.model_copy( # type: ignore[union-attr]
update={
"index": 2,
"content": [second_pair[0].blocks[0].content[0].model_copy(update={"index": 2})], # type: ignore[union-attr]
}
)
)
first_title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=1, content=_inline("First"), level=2)
second_title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=1, content=_inline("Second"), level=2)
pages = [
PageInfo(
page_idx=0,
blocks=[
DocTitleBlock(type=BlockType.DOC_TITLE, index=0, content=_inline("A"), level=1),
first_title,
first_previous_table,
],
),
PageInfo(page_idx=1, blocks=first_pair[1].blocks),
PageInfo(
page_idx=2,
blocks=[
DocTitleBlock(type=BlockType.DOC_TITLE, index=0, content=_inline("B"), level=1),
second_title,
second_previous_table,
],
),
PageInfo(page_idx=3, blocks=second_pair[1].blocks),
]
client = _ConcurrentValidatedClient(expected_calls=4)
middle_json = _middle_json(pages)
apply_llm_aided_postprocess(
middle_json,
_llm_config(title=True, table=True),
client=client, # type: ignore[arg-type]
)
assert client.peak_active == 4
assert client.operations.count("title_leveling") == 2
assert client.operations.count("cross_page_table_cell_merge") == 2
assert [first_title.level, second_title.level] == [3, 3]
assert pages[1].blocks[0].cell_merge == [1, 0] # type: ignore[union-attr]
assert pages[3].blocks[0].cell_merge == [1, 0] # type: ignore[union-attr]
def test_disabled_postprocess_does_not_construct_client(monkeypatch: pytest.MonkeyPatch) -> None:
"""验证两个功能关闭时不会创建任何外部客户端。"""
from mineru.backend.postprocess import llm_aided
monkeypatch.setattr(llm_aided, "LLMAidedClient", lambda _config: pytest.fail("client must not be created"))
apply_llm_aided_postprocess(_middle_json([]), LLMAidedConfig())
def test_partial_input_skips_title_without_constructing_client(monkeypatch: pytest.MonkeyPatch) -> None:
"""验证抽页输入只启用标题时会正常跳过且不创建外部客户端。"""
from mineru.backend.postprocess import llm_aided
monkeypatch.setattr(llm_aided, "LLMAidedClient", lambda _config: pytest.fail("client must not be created"))
apply_llm_aided_postprocess(_middle_json([], is_full_document=False), _llm_config(title=True))
def test_partial_input_skips_title_but_keeps_table_cell_merge() -> None:
"""验证抽页输入不会触发标题请求,但仍会执行跨页单元格分析。"""
pages = _cross_page_table_pages()
pages[0].blocks[0] = (
pages[0]
.blocks[0]
.model_copy( # type: ignore[union-attr]
update={
"index": 1,
"content": [pages[0].blocks[0].content[0].model_copy(update={"index": 1})], # type: ignore[union-attr]
}
)
)
title = ParagraphTitleBlock(type=BlockType.PARAGRAPH_TITLE, index=0, content=_inline("Section"), level=2)
pages[0].blocks.insert(0, title)
client = _QueuedValidatedClient([[0, 1]])
middle_json = _middle_json(pages, is_full_document=False)
apply_llm_aided_postprocess(
middle_json,
_llm_config(title=True, table=True),
client=client, # type: ignore[arg-type]
)
assert client.operations == ["cross_page_table_cell_merge"]
assert title.level == 2
assert pages[1].blocks[0].cell_merge == [0, 1] # type: ignore[union-attr]
@pytest.mark.parametrize("asynchronous", [False, True])
@pytest.mark.parametrize("configured,explicit", [(False, False), (True, False), (False, True), (True, True)])
@pytest.mark.parametrize("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
@pytest.mark.parametrize("asynchronous", [False, True])
@pytest.mark.parametrize("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"
assert "enable_cross_page_table_merge=False" in runtime_path.read_text(encoding="utf-8")
@pytest.mark.parametrize("page_index_map", [None, []])
def test_doc_analyze_runs_llm_after_deterministic_table_detection(
monkeypatch: pytest.MonkeyPatch,
page_index_map: list[int] | None,
) -> None:
"""验证默认或空映射的整本 PDF 均先检测续表,再执行后置 LLM。"""
from mineru.backend import analyze
from mineru.backend.postprocess import document
previous_html = "<table><tr><th>H1</th><th>H2</th></tr><tr><td>A</td><td>X</td></tr></table>"
current_html = "<table><tr><th>H1</th><th>H2</th></tr><tr><td>B</td><td>Y</td></tr></table>"
result = AnalysisResult(
model_list=[
[_raw_table(previous_html, [0.1, 0.5, 0.9, 0.95])],
[_raw_table(current_html, [0.1, 0.05, 0.9, 0.5])],
],
effort="high",
parse_mode="txt",
elapsed=0.1,
)
calls: list[str] = []
def fake_llm_postprocess(
middle_json: MiddleJson,
_config: LLMAidedConfig,
) -> None:
"""断言确定性续表标记已存在,并模拟新增 cell_merge。"""
assert middle_json.is_full_document is True
current_table = middle_json.pages[1].blocks[0]
assert isinstance(current_table, TableBlock)
assert current_table.continues_prev is True
assert current_table.cell_merge is None
current_table.cell_merge = [1, 0]
calls.append("llm")
monkeypatch.setattr("mineru.backend.analysis.pdf.pipeline.analyze_pdf", lambda *_args, **_kwargs: result)
monkeypatch.setattr(document, "apply_llm_aided_postprocess", fake_llm_postprocess)
middle_json, model_json = analyze.doc_analyze(b"pdf", page_index_map=page_index_map)
current_table = middle_json.pages[1].blocks[0]
assert isinstance(current_table, TableBlock)
assert current_table.cell_merge == [1, 0]
assert model_json.page_index_map == []
assert model_json.is_full_document is True
assert middle_json.is_full_document is True
assert calls == ["llm"]
def test_doc_analyze_does_not_run_llm_for_office(monkeypatch: pytest.MonkeyPatch) -> None:
"""验证 Office 统一入口不会进入仅限 PDF 的 LLM 后处理。"""
from mineru.backend import analyze
from mineru.backend.postprocess import document
result = AnalysisResult(
model_list=[[{"type": BlockType.PARAGRAPH_TITLE, "content": _inline("Slide"), "level": 2}]],
effort="flash",
parse_mode="txt",
elapsed=0.1,
)
calls: list[str] = []
monkeypatch.setattr("mineru.backend.analysis.office.analyze_office", lambda *_args, **_kwargs: result)
monkeypatch.setattr(document, "apply_llm_aided_postprocess", lambda *_args, **_kwargs: calls.append("llm"))
middle_json, _ = analyze.doc_analyze(b"office", file_suffix="pptx")
assert middle_json.metadata.file_suffix == "pptx"
assert middle_json.is_full_document is True
assert calls == []