Skip to content

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

feat: normalize text content handling in VLM models to prevent None values and improve formatting

myhlolib6146e0131a7
diff --git a/mineru/backend/hybrid/hybrid_magic_model.py b/mineru/backend/hybrid/hybrid_magic_model.py
index 096e15bc..40eb6e79 100644
--- a/mineru/backend/hybrid/hybrid_magic_model.py
+++ b/mineru/backend/hybrid/hybrid_magic_model.py
@@ -156,6 +156,10 @@ class MagicModel:
block_type = BlockType.INTERLINE_EQUATION
span_type = ContentType.INTERLINE_EQUATION
+ if span_type == ContentType.TEXT and block_content is None:
+ # 文本类块缺失 content 时按空文本处理,避免 VLM mkcontent 渲染阶段遇到 None。
+ block_content = ""
+
# code 和 algorithm 类型的块,如果内容中包含行内公式,则需要将块类型切换为 algorithm
switch_code_to_algorithm = False
diff --git a/mineru/backend/vlm/vlm_magic_model.py b/mineru/backend/vlm/vlm_magic_model.py
index b05e3759..ef930583 100644
--- a/mineru/backend/vlm/vlm_magic_model.py
+++ b/mineru/backend/vlm/vlm_magic_model.py
@@ -107,6 +107,10 @@ class MagicModel:
block_type = BlockType.INTERLINE_EQUATION
span_type = ContentType.INTERLINE_EQUATION
+ if span_type == ContentType.TEXT and block_content is None:
+ # 文本类块缺失 content 时按空文本处理,避免下游 mkcontent 遇到 None。
+ block_content = ""
+
# code 和 algorithm 类型的块,如果内容中包含行内公式,则需要将块类型切换为 algorithm
switch_code_to_algorithm = False
diff --git a/mineru/backend/vlm/vlm_middle_json_mkcontent.py b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
index c679be20..89a4673c 100644
--- a/mineru/backend/vlm/vlm_middle_json_mkcontent.py
+++ b/mineru/backend/vlm/vlm_middle_json_mkcontent.py
@@ -61,6 +61,13 @@ def _format_embedded_html(html, img_buket_path):
return _replace_eq_tags_in_table_html(_prefix_table_img_src(html, img_buket_path))
+def _normalize_text_content(content):
+ """将 VLM 文本 span 的空 content 规范为空字符串,再做全角转半角。"""
+ if content is None:
+ return ''
+ return full_to_half_exclude_marks(content)
+
+
def _build_media_path(img_buket_path, image_path):
if not image_path:
return ''
@@ -245,7 +252,7 @@ def merge_para_with_text(
for line in para_block['lines']:
for span in line['spans']:
if span['type'] in [ContentType.TEXT]:
- span['content'] = full_to_half_exclude_marks(span['content'])
+ span['content'] = _normalize_text_content(span.get('content'))
block_text += span['content']
block_lang = detect_lang(block_text)
escape_markdown_text = para_block.get('type') != BlockType.CODE_BODY
@@ -768,7 +775,7 @@ def merge_para_with_text_v2(para_block):
for line in para_block['lines']:
for span in line['spans']:
if span['type'] in [ContentType.TEXT]:
- span['content'] = full_to_half_exclude_marks(span['content'])
+ span['content'] = _normalize_text_content(span.get('content'))
block_text += span['content']
block_lang = detect_lang(block_text)