MinerU OCR Local API
Hard Rules
- 1. Use
python scripts/mineru_caller.py for every MinerU request. - Do not parse the document yourself as a fallback.
- If MinerU returns an error, show the error and stop.
- Treat the saved JSON envelope and generated artifact files as the source of truth.
- Prefer the top-level
text field when the user asks for the full extracted document.
Choose The Mode
- - Use
--mode api when the user wants the hosted MinerU service, already has MINERU_API_TOKEN, needs URL input, or wants the official cloud API workflow. - Use
--mode local when the user wants the open-source MinerU runtime from https://github.com/opendatalab/MinerU, wants data to stay local, or explicitly asks for local parsing. - Use
--mode auto only when the user does not care which mode is used. auto prefers API when MINERU_API_TOKEN is configured and falls back to local only for local files.
Standard Workflow
- 1. For a hosted API parse from URL:
CODEBLOCK0
- 2. For a hosted API parse from a local file:
CODEBLOCK1
- 3. For a local open-source MinerU parse:
CODEBLOCK2
- 4. When the input is a local file and the user will need an IDE-accessible path, prefer saving beside the source file:
CODEBLOCK3
- 5. Read these output fields before answering:
-
mode: actual execution mode used
-
text: complete document Markdown from
full.md or
<file_stem>.md
-
result.submit: raw task-creation response for API URL parsing
-
result.batch: raw upload-batch response for API local-file parsing
-
result.poll: final API task-status payload
-
result.local: local MinerU CLI invocation summary
-
artifacts.full_md_path: absolute path to the main Markdown file
-
artifacts.local_parse_dir: local MinerU parse directory when using
--mode local
-
artifacts.downloaded_zip: downloaded MinerU archive when using INLINECODE21
Useful Flags
- -
--mode api|local|auto: choose hosted API, local runtime, or automatic selection - INLINECODE23 : return after submission without polling; API mode only
- INLINECODE24 : skip downloading
full_zip_url; API mode only - INLINECODE26 : store API downloads or local MinerU outputs in a specific directory
- INLINECODE27 : pass a language hint
- INLINECODE28 : force OCR mode
- INLINECODE29 : turn off formula extraction
- INLINECODE30 : explicit path to
mineru.exe or INLINECODE32 - INLINECODE33 : explicit Python path for INLINECODE34
- INLINECODE35 : choose the local MinerU backend
- INLINECODE36 : choose the local MinerU parse method
- INLINECODE37 : useful in environments where Hugging Face access is restricted
- INLINECODE38 : force a local inference device when needed
Present Results
- - If the user asks for all text, show the top-level
text field. - If the user asks where files were saved, report the paths in
artifacts. - If the output is large, give the saved file paths first and then the requested excerpt or summary.
- If API mode completed but archive download failed, report
artifacts.full_zip_url.
Configuration
For API mode:
CODEBLOCK4
Windows PowerShell examples:
CODEBLOCK5
Use the first command for the current terminal only. Use setx to persist it for future terminals, then restart Codex/Cursor or open a new terminal.
For local mode, configure at least one runtime entry point:
CODEBLOCK6
Local mode only supports --file-path. It does not accept --file-url.
Error Handling
- - Missing API token: show the configuration error, tell the user to set
MINERU_API_TOKEN, and include one-shot plus persistent commands. - Missing local runtime: show the configuration error and stop.
- Failed task or failed local CLI run: show the error and stop.
- Poll timeout: tell the user the task id and that polling timed out.
- API archive download TLS error: rely on the built-in
curl fallback before reporting failure. - Missing expected output files: return any artifact paths that do exist and report the missing output.
References
- -
references/output_schema.md: JSON envelope and artifact layout for both modes.
Load the reference file when:
- - You need to explain which saved files matter.
- You need to inspect mode-specific artifacts such as
downloaded_zip, local_parse_dir, middle_json, or content_list.
Verification
Validate the skill folder:
CODEBLOCK7
Run a local runtime check against a sample PDF:
CODEBLOCK8
Run an API runtime check against a remote PDF:
CODEBLOCK9
MinerU OCR 本地 API
硬性规则
- 1. 每次 MinerU 请求必须使用 python scripts/mineru_caller.py。
- 不要自行解析文档作为后备方案。
- 如果 MinerU 返回错误,显示错误并停止。
- 将保存的 JSON 信封和生成的工件文件视为事实来源。
- 当用户请求完整的提取文档时,优先使用顶层的 text 字段。
选择模式
- - 当用户想要托管的 MinerU 服务、已有 MINERUAPITOKEN、需要 URL 输入或想要官方云 API 工作流时,使用 --mode api。
- 当用户想要来自 https://github.com/opendatalab/MinerU 的开源 MinerU 运行时、希望数据保留在本地或明确要求本地解析时,使用 --mode local。
- 仅当用户不关心使用哪种模式时,使用 --mode auto。auto 模式在配置了 MINERUAPITOKEN 时优先使用 API,仅对本地文件回退到本地模式。
标准工作流程
- 1. 从 URL 进行托管 API 解析:
bash
python scripts/mineru_caller.py --mode api --file-url https://example.com/paper.pdf --pretty
- 2. 从本地文件进行托管 API 解析:
bash
python scripts/mineru_caller.py --mode api --file-path C:/docs/paper.pdf --pretty
- 3. 本地开源 MinerU 解析:
bash
python scripts/mineru_caller.py --mode local --file-path C:/docs/paper.pdf --pretty
- 4. 当输入是本地文件且用户需要 IDE 可访问的路径时,优先保存到源文件旁边:
bash
python scripts/mineru_caller.py --mode local --file-path C:/docs/paper.pdf --download-dir C:/docs/paper.mineru --pretty
- 5. 在回答前读取以下输出字段:
- mode:实际使用的执行模式
- text:来自 full.md 或
.md 的完整文档 Markdown
- result.submit:API URL 解析的原始任务创建响应
- result.batch:API 本地文件解析的原始上传批次响应
- result.poll:最终 API 任务状态负载
- result.local:本地 MinerU CLI 调用摘要
- artifacts.fullmdpath:主 Markdown 文件的绝对路径
- artifacts.localparsedir:使用 --mode local 时的本地 MinerU 解析目录
- artifacts.downloaded_zip:使用 --mode api 时下载的 MinerU 归档文件
有用的标志
- - --mode api|local|auto:选择托管 API、本地运行时或自动选择
- --no-wait:提交后返回而不轮询;仅 API 模式
- --no-download:跳过下载 fullzipurl;仅 API 模式
- --download-dir DIR:将 API 下载或本地 MinerU 输出存储在特定目录中
- --language en:传递语言提示
- --ocr:强制 OCR 模式
- --disable-formula:关闭公式提取
- --local-cmd PATH:mineru.exe 或 mineru 的显式路径
- --local-python PATH:python -m mineru.cli.client 的显式 Python 路径
- --local-backend pipeline:选择本地 MinerU 后端
- --local-method auto|txt|ocr:选择本地 MinerU 解析方法
- --local-model-source modelscope:在 Hugging Face 访问受限的环境中很有用
- --local-device cpu:需要时强制使用本地推理设备
呈现结果
- - 如果用户请求所有文本,显示顶层的 text 字段。
- 如果用户询问文件保存位置,报告 artifacts 中的路径。
- 如果输出很大,先给出保存的文件路径,然后给出请求的摘录或摘要。
- 如果 API 模式完成但归档下载失败,报告 artifacts.fullzipurl。
配置
API 模式:
text
MINERUAPITOKEN
MINERUAPIBASE_URL=https://mineru.net
MINERUAPITIMEOUT=60
MINERUAPIPOLL_TIMEOUT=900
MINERUAPIPOLL_INTERVAL=5
Windows PowerShell 示例:
powershell
$env:MINERUAPITOKEN=YOURMINERUTOKEN
setx MINERUAPITOKEN YOURMINERUTOKEN
第一个命令仅用于当前终端。使用 setx 使其持久化到未来终端,然后重启 Codex/Cursor 或打开新终端。
对于本地模式,至少配置一个运行时入口点:
text
MINERULOCALCMD=C:\path\to\mineru.exe
MINERULOCALPYTHON=C:\path\to\python.exe
MINERULOCALBACKEND=pipeline
MINERULOCALMETHOD=auto
MINERULOCALLANG=ch
MINERULOCALMODEL_SOURCE=modelscope
MINERULOCALDEVICE_MODE=cpu
MINERULOCALTIMEOUT=3600
本地模式仅支持 --file-path。不接受 --file-url。
错误处理
- - 缺少 API 令牌:显示配置错误,告诉用户设置 MINERUAPITOKEN,并包含一次性命令和持久化命令。
- 缺少本地运行时:显示配置错误并停止。
- 任务失败或本地 CLI 运行失败:显示错误并停止。
- 轮询超时:告诉用户任务 ID 以及轮询已超时。
- API 归档下载 TLS 错误:在报告失败之前依赖内置的 curl 回退。
- 缺少预期的输出文件:返回任何存在的工件路径并报告缺少的输出。
参考资料
- - references/output_schema.md:两种模式的 JSON 信封和工件布局。
在以下情况下加载参考文件:
- - 需要解释哪些保存的文件重要。
- 需要检查特定模式的工件,如 downloadedzip、localparsedir、middlejson 或 content_list。
验证
验证技能文件夹:
bash
python /path/to/quick_validate.py /path/to/mineru-ocr-api-local
对示例 PDF 运行本地运行时检查:
bash
python scripts/mineru_caller.py --mode local --file-path D:/path/to/file.pdf --pretty --stdout
对远程 PDF 运行 API 运行时检查:
bash
python scripts/mineru_caller.py --mode api --file-url https://example.com/file.pdf --pretty --stdout