Skip to content

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

feat: implement backend validation and update backend options for parsing

myhloli742ac25101aa
diff --git a/demo/demo.py b/demo/demo.py
index f1c48a76..e83d05a3 100644
--- a/demo/demo.py
+++ b/demo/demo.py
@@ -95,7 +95,7 @@ async def run_demo(
output_dir: str | Path,
*,
api_url: str | None = None,
- backend: str = "hybrid-auto-engine",
+ backend: str = "hybrid-engine",
parse_method: str = "auto",
language: str = "ch",
formula_enable: bool = True,
@@ -212,12 +212,12 @@ def main() -> None:
api_url = None
# Available examples:
- # "hybrid-auto-engine" -> local hybrid parsing, recommended default
+ # "hybrid-engine" -> local hybrid parsing, recommended default
# "pipeline" -> more general OCR/text pipeline
- # "vlm-auto-engine" -> local VLM parsing
+ # "vlm-engine" -> local VLM parsing
# "vlm-http-client" -> remote OpenAI-compatible VLM server
# "hybrid-http-client" -> remote OpenAI-compatible hybrid server
- backend = "hybrid-auto-engine"
+ backend = "hybrid-engine"
# Available options:
# "auto" -> let MinerU choose between text extraction and OCR
# "txt" -> force text extraction
diff --git a/mineru/cli/api_request.py b/mineru/cli/api_request.py
index 4e640a31..56780fe9 100644
--- a/mineru/cli/api_request.py
+++ b/mineru/cli/api_request.py
@@ -4,6 +4,11 @@ from typing import Annotated, Optional
from fastapi import File, Form, HTTPException, Request, UploadFile
+from mineru.cli.backend_options import (
+ BACKEND_SCHEMA_EXTRA,
+ DEFAULT_BACKEND,
+ validate_backend as validate_public_backend,
+)
from mineru.cli.public_http_client_policy import validate_public_http_client_request
ALLOWED_PARSE_METHODS = {"auto", "txt", "ocr"}
@@ -51,6 +56,14 @@ def validate_parse_method(parse_method: str) -> str:
return parse_method
+def validate_parse_backend(backend: str) -> str:
+ """校验公开 API 允许的解析后端,避免旧入口名进入下游执行链路。"""
+ try:
+ return validate_public_backend(backend)
+ except ValueError as exc:
+ raise HTTPException(status_code=400, detail=str(exc)) from exc
+
+
async def parse_request_form(
request: Request,
files: Annotated[
@@ -89,14 +102,15 @@ async def parse_request_form(
Form(
description="""The backend for parsing:
- pipeline: More general, supports multiple languages, hallucination-free.
-- vlm-auto-engine: High accuracy via local computing power, supports Chinese and English documents only.
+- vlm-engine: High accuracy via local computing power, supports Chinese and English documents only.
- vlm-http-client: High accuracy via remote computing power(client suitable for openai-compatible servers), supports Chinese and English documents only.
-- hybrid-auto-engine: Next-generation high accuracy solution via local computing power, supports multiple languages.
-- hybrid-flash-auto-engine: Experimental hybrid-flash analyze path via local computing power, supports multiple languages.
+- hybrid-engine: Next-generation high accuracy solution via local computing power, supports multiple languages.
+- hybrid-flash-engine: Experimental hybrid-flash analyze path via local computing power, supports multiple languages.
- hybrid-http-client: High accuracy via remote computing power but requires a little local computing power(client suitable for openai-compatible servers), supports multiple languages.
- hybrid-flash-http-client: Experimental hybrid-flash analyze path via remote computing power(client suitable for openai-compatible servers), supports multiple languages.""",
+ json_schema_extra=BACKEND_SCHEMA_EXTRA,
),
- ] = "hybrid-auto-engine",
+ ] = DEFAULT_BACKEND,
parse_method: Annotated[
str,
Form(
@@ -177,6 +191,7 @@ async def parse_request_form(
] = 99999,
) -> ParseRequestOptions:
"""解析 API/Router 共用的 multipart 表单,并保持 Swagger 参数同源。"""
+ backend = validate_parse_backend(backend)
validate_public_http_client_request(
public_bind_exposed=bool(
getattr(request.app.state, "public_bind_exposed", False)
diff --git a/mineru/cli/backend_options.py b/mineru/cli/backend_options.py
new file mode 100644
index 00000000..23f2d67e
--- /dev/null
+++ b/mineru/cli/backend_options.py
@@ -0,0 +1,52 @@
+# Copyright (c) Opendatalab. All rights reserved.
+
+BACKEND_PIPELINE = "pipeline"
+BACKEND_VLM_ENGINE = "vlm-engine"
+BACKEND_HYBRID_ENGINE = "hybrid-engine"
+BACKEND_HYBRID_FLASH_ENGINE = "hybrid-flash-engine"
+BACKEND_VLM_HTTP_CLIENT = "vlm-http-client"
+BACKEND_HYBRID_HTTP_CLIENT = "hybrid-http-client"
+BACKEND_HYBRID_FLASH_HTTP_CLIENT = "hybrid-flash-http-client"
+
+DEFAULT_BACKEND = BACKEND_HYBRID_ENGINE
+
+LOCAL_BACKEND_CHOICES = (
+ BACKEND_PIPELINE,
+ BACKEND_VLM_ENGINE,
+ BACKEND_HYBRID_ENGINE,
+ BACKEND_HYBRID_FLASH_ENGINE,
+)
+HTTP_CLIENT_BACKEND_CHOICES = (
+ BACKEND_VLM_HTTP_CLIENT,
+ BACKEND_HYBRID_HTTP_CLIENT,
+ BACKEND_HYBRID_FLASH_HTTP_CLIENT,
+)
+PUBLIC_BACKEND_CHOICES = LOCAL_BACKEND_CHOICES + HTTP_CLIENT_BACKEND_CHOICES
+BACKEND_SCHEMA_EXTRA = {"enum": list(PUBLIC_BACKEND_CHOICES)}
+LEGACY_BACKEND_ALIASES = {
+ "vlm-auto-engine": BACKEND_VLM_ENGINE,
+ "hybrid-auto-engine": BACKEND_HYBRID_ENGINE,
+ "hybrid-flash-auto-engine": BACKEND_HYBRID_FLASH_ENGINE,
+}
+
+
+def get_backend_choices(include_http_client: bool = True) -> list[str]:
+ """按入口配置返回公开 backend 选项,避免各入口重复维护字符串列表。"""
+ choices = list(LOCAL_BACKEND_CHOICES)
+ if include_http_client:
+ choices.extend(HTTP_CLIENT_BACKEND_CHOICES)
+ return choices
+
+
+def normalize_backend(backend: str) -> str:
+ """将旧 backend 别名规范为当前公开名称,并校验最终名称是否合法。"""
+ normalized_backend = LEGACY_BACKEND_ALIASES.get(backend, backend)
+ if normalized_backend not in PUBLIC_BACKEND_CHOICES:
+ allowed_values = ", ".join(PUBLIC_BACKEND_CHOICES)
+ raise ValueError(f"Invalid backend. Allowed values: {allowed_values}")
+ return normalized_backend
+
+
+def validate_backend(backend: str) -> str:
+ """校验公开入口允许的 backend 名称,并返回规范后的后端名称。"""
+ return normalize_backend(backend)
diff --git a/mineru/cli/client.py b/mineru/cli/client.py
index 05021446..4659d153 100644
--- a/mineru/cli/client.py
+++ b/mineru/cli/client.py
@@ -18,6 +18,11 @@ from mineru.cli.api_protocol import (
DEFAULT_MAX_CONCURRENT_REQUESTS,
DEFAULT_PROCESSING_WINDOW_SIZE,
)
+from mineru.cli.backend_options import (
+ DEFAULT_BACKEND,
+ PUBLIC_BACKEND_CHOICES,
+ normalize_backend,
+)
from mineru.utils.config_reader import (
get_max_concurrent_requests as read_max_concurrent_requests,
)
@@ -85,6 +90,18 @@ SubmitResponse = _api_client.SubmitResponse
LocalAPIServer = _api_client.LocalAPIServer
+def normalize_backend_option(
+ ctx: click.Context,
+ param: click.Parameter,
+ value: str,
+) -> str:
+ """将 CLI 输入的旧 backend 名称规范为当前公开名称。"""
+ try:
+ return normalize_backend(value)
+ except ValueError as exc:
+ raise click.BadParameter(str(exc), ctx=ctx, param=param) from exc
+
+
@dataclass(frozen=True)
class TaskFailure:
task_index: int
@@ -1029,28 +1046,20 @@ async def run_orchestrated_cli(
"-b",
"--backend",
"backend",
- type=click.Choice(
- [
- "pipeline",
- "vlm-http-client",
- "hybrid-http-client",
- "hybrid-flash-http-client",
- "vlm-auto-engine",
- "hybrid-auto-engine",
- "hybrid-flash-auto-engine",
- ]
- ),
- default="hybrid-auto-engine",
+ type=str,
+ default=DEFAULT_BACKEND,
+ callback=normalize_backend_option,
+ metavar="[" + "|".join(PUBLIC_BACKEND_CHOICES) + "]",
help="""\b
the backend for parsing pdf:
pipeline: More general.
- vlm-auto-engine: High accuracy via local computing power.
+ vlm-engine: High accuracy via local computing power.
vlm-http-client: High accuracy via remote computing power(client suitable for openai-compatible servers).
- hybrid-auto-engine: Next-generation high accuracy solution via local computing power.
- hybrid-flash-auto-engine: Experimental hybrid-flash analyze path via local computing power.
+ hybrid-engine: Next-generation high accuracy solution via local computing power.
+ hybrid-flash-engine: Experimental hybrid-flash analyze path via local computing power.
hybrid-http-client: High accuracy but requires a little local computing power(client suitable for openai-compatible servers).
hybrid-flash-http-client: Experimental hybrid-flash analyze path via remote computing power(client suitable for openai-compatible servers).
- Without method specified, hybrid-auto-engine will be used by default.""",
+ Without method specified, hybrid-engine will be used by default.""",
)
@click.option(
"-l",
diff --git a/mineru/cli/gradio_app.py b/mineru/cli/gradio_app.py
index 04c4b068..aa73b759 100644
--- a/mineru/cli/gradio_app.py
+++ b/mineru/cli/gradio_app.py
@@ -38,6 +38,11 @@ from mineru.cli.common import (
read_fn,
)
from mineru.cli import api_client as _api_client
+from mineru.cli.backend_options import (
+ DEFAULT_BACKEND,
+ HTTP_CLIENT_BACKEND_CHOICES,
+ LOCAL_BACKEND_CHOICES,
+)
from mineru.cli.client_side_output import regenerate_client_side_outputs
from mineru.cli.output_paths import resolve_parse_dir
from mineru.cli.vlm_preload import resolve_gradio_local_api_cli_args
@@ -224,17 +229,8 @@ STATUS_QUEUED_ON_SERVER = "Queued on server"
STATUS_PROCESSING_ON_SERVER = "Processing on server"
STATUS_QUEUED_LOCALLY_PREFIX = "Queued locally:"
-BACKEND_CHOICE_DEFINITIONS = [
- "pipeline",
- "vlm-auto-engine",
- "hybrid-auto-engine",
- "hybrid-flash-auto-engine",
-]
-HTTP_CLIENT_BACKEND_CHOICE_DEFINITIONS = [
- "vlm-http-client",
- "hybrid-http-client",
- "hybrid-flash-http-client",
-]
+BACKEND_CHOICE_DEFINITIONS = list(LOCAL_BACKEND_CHOICES)
+HTTP_CLIENT_BACKEND_CHOICE_DEFINITIONS = list(HTTP_CLIENT_BACKEND_CHOICES)
STATUS_STEP_DEFINITIONS = [
("status_step_prepare", STATUS_PREPARING_REQUEST),
("status_step_check", STATUS_CHECKING_SERVER),
@@ -1778,7 +1774,7 @@ def main(ctx,
file_types=suffixes,
elem_classes=["mineru-upload-file"],
)
- preferred_option = "hybrid-auto-engine"
+ preferred_option = DEFAULT_BACKEND
backend = gr.Dropdown(
build_backend_choices(http_client_enable, i18n),
label=i18n("backend"),