Skip to content

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

feat: implement lazy loading for S3 IO classes to avoid mandatory boto3 dependency

myhloli3a496333e4c3
diff --git a/docs/en/quick_start/extension_modules.md b/docs/en/quick_start/extension_modules.md
index f6bf34ac..32854b6a 100644
--- a/docs/en/quick_start/extension_modules.md
+++ b/docs/en/quick_start/extension_modules.md
@@ -4,13 +4,21 @@ MinerU supports installing extension modules on demand based on different needs
## Common Scenarios
### Core Functionality Installation
-The `core` module is the core dependency of MinerU, containing all functional modules except `vllm`/`lmdeploy`. Installing this module ensures the basic functionality of MinerU works properly.
+The `core` module is the core dependency of MinerU, containing common parsing features but not optional modules such as `vllm`/`lmdeploy`/`s3`. Installing this module ensures the basic functionality of MinerU works properly.
```bash
uv pip install "mineru[core]"
```
---
+### Using S3 Input and Output
+If you need to read from or write to S3, install the `s3` extension module.
+```bash
+uv pip install "mineru[s3]"
+```
+
+---
+
### Using `vllm` to Accelerate VLM Model Inference
> [!NOTE]
> `vllm` and `lmdeploy` have nearly identical VLM inference acceleration effects and usage methods. You can choose one of them to install and use based on your actual needs, but it is not recommended to install both modules simultaneously to avoid potential dependency conflicts.
@@ -53,4 +61,4 @@ If you need to install a lightweight client on edge devices to connect to an Ope
```bash
uv pip install "mineru[pipeline]"
mineru -p <input_path> -o <output_path> -b hybrid-http-client -u http://127.0.0.1:30000
-```
\ No newline at end of file
+```
diff --git a/docs/zh/quick_start/extension_modules.md b/docs/zh/quick_start/extension_modules.md
index 72bad262..3d2cb68a 100644
--- a/docs/zh/quick_start/extension_modules.md
+++ b/docs/zh/quick_start/extension_modules.md
@@ -4,13 +4,21 @@ MinerU 支持根据不同需求,按需安装扩展模块,以增强功能或
## 常见场景
### 核心功能安装
-`core` 模块是 MinerU 的核心依赖,包含了除`vllm`/`lmdeploy`外的所有功能模块。安装此模块可以确保 MinerU 的基本功能正常运行。
+`core` 模块是 MinerU 的核心依赖,包含了常用解析功能,不包含`vllm`/`lmdeploy`/`s3`等可选模块。安装此模块可以确保 MinerU 的基本功能正常运行。
```bash
uv pip install "mineru[core]"
```
---
+### 使用 S3 输入输出
+如需通过 S3 读取或写入文件,请安装`s3`扩展模块。
+```bash
+uv pip install "mineru[s3]"
+```
+
+---
+
### 使用`vllm`加速 VLM 模型推理
> [!NOTE]
> `vllm`和`lmdeploy`对vlm的推理加速效果和使用方式几乎相同,您可以根据实际情况选择其中之一进行安装和使用,但不建议同时安装这两个模块,以避免潜在的依赖冲突。
diff --git a/mineru/data/data_reader_writer/__init__.py b/mineru/data/data_reader_writer/__init__.py
index 9cc042f6..d55489f3 100644
--- a/mineru/data/data_reader_writer/__init__.py
+++ b/mineru/data/data_reader_writer/__init__.py
@@ -2,8 +2,6 @@
from .base import DataReader, DataWriter
from .dummy import DummyDataWriter
from .filebase import FileBasedDataReader, FileBasedDataWriter
-from .multi_bucket_s3 import MultiBucketS3DataReader, MultiBucketS3DataWriter
-from .s3 import S3DataReader, S3DataWriter
__all__ = [
"DataReader",
@@ -16,3 +14,25 @@ __all__ = [
"MultiBucketS3DataWriter",
"DummyDataWriter",
]
+
+
+def __getattr__(name):
+ """按需加载 S3 DataReader/DataWriter,避免默认安装场景强制依赖 boto3。"""
+ if name in {"MultiBucketS3DataReader", "MultiBucketS3DataWriter"}:
+ from .multi_bucket_s3 import MultiBucketS3DataReader, MultiBucketS3DataWriter
+
+ s3_exports = {
+ "MultiBucketS3DataReader": MultiBucketS3DataReader,
+ "MultiBucketS3DataWriter": MultiBucketS3DataWriter,
+ }
+ globals().update(s3_exports)
+ return s3_exports[name]
+
+ if name in {"S3DataReader", "S3DataWriter"}:
+ from .s3 import S3DataReader, S3DataWriter
+
+ s3_exports = {"S3DataReader": S3DataReader, "S3DataWriter": S3DataWriter}
+ globals().update(s3_exports)
+ return s3_exports[name]
+
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/mineru/data/data_reader_writer/multi_bucket_s3.py b/mineru/data/data_reader_writer/multi_bucket_s3.py
index e4e7e7c9..96bac1f1 100644
--- a/mineru/data/data_reader_writer/multi_bucket_s3.py
+++ b/mineru/data/data_reader_writer/multi_bucket_s3.py
@@ -2,11 +2,22 @@
from ..utils.exceptions import InvalidConfig, InvalidParams
from .base import DataReader, DataWriter
-from ..io.s3 import S3Reader, S3Writer
from ..utils.schemas import S3Config
from ..utils.path_utils import parse_s3_range_params, parse_s3path, remove_non_official_s3_args
+def _load_s3_io_classes():
+ """延迟加载 S3 IO 类,仅在实际读写 S3 时要求安装 boto3。"""
+ try:
+ from ..io.s3 import S3Reader, S3Writer
+ except ModuleNotFoundError as exc:
+ raise ModuleNotFoundError(
+ "S3 reader/writer requires optional dependencies. Install them with `pip install 'mineru[s3]'`."
+ ) from exc
+
+ return S3Reader, S3Writer
+
+
class MultiS3Mixin:
def __init__(self, default_prefix: str, s3_configs: list[S3Config]):
"""Initialized with multiple s3 configs.
@@ -77,6 +88,7 @@ class MultiBucketS3DataReader(DataReader, MultiS3Mixin):
conf = next(
filter(lambda conf: conf.bucket_name == bucket_name, self.s3_configs)
)
+ S3Reader, _ = _load_s3_io_classes()
self._s3_clients_h[bucket_name] = S3Reader(
bucket_name,
conf.access_key,
@@ -118,6 +130,7 @@ class MultiBucketS3DataWriter(DataWriter, MultiS3Mixin):
conf = next(
filter(lambda conf: conf.bucket_name == bucket_name, self.s3_configs)
)
+ _, S3Writer = _load_s3_io_classes()
self._s3_clients_h[bucket_name] = S3Writer(
bucket_name,
conf.access_key,
diff --git a/mineru/data/io/__init__.py b/mineru/data/io/__init__.py
index 43bfe5ee..c8071a57 100644
--- a/mineru/data/io/__init__.py
+++ b/mineru/data/io/__init__.py
@@ -2,6 +2,17 @@
from .base import IOReader, IOWriter
from .http import HttpReader, HttpWriter
-from .s3 import S3Reader, S3Writer
-__all__ = ['IOReader', 'IOWriter', 'HttpReader', 'HttpWriter', 'S3Reader', 'S3Writer']
\ No newline at end of file
+__all__ = ['IOReader', 'IOWriter', 'HttpReader', 'HttpWriter', 'S3Reader', 'S3Writer']
+
+
+def __getattr__(name):
+ """按需加载 S3 IO 类,避免默认安装场景强制依赖 boto3。"""
+ if name in {'S3Reader', 'S3Writer'}:
+ from .s3 import S3Reader, S3Writer
+
+ s3_exports = {'S3Reader': S3Reader, 'S3Writer': S3Writer}
+ globals().update(s3_exports)
+ return s3_exports[name]
+
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/mineru/data/io/s3.py b/mineru/data/io/s3.py
index f1dc94a4..189787e4 100644
--- a/mineru/data/io/s3.py
+++ b/mineru/data/io/s3.py
@@ -1,10 +1,20 @@
# Copyright (c) Opendatalab. All rights reserved.
-import boto3
-from botocore.config import Config
-
from ..io.base import IOReader, IOWriter
+def _load_s3_client_dependencies():
+ """按需加载 S3 客户端依赖,避免导入 S3 类时强制安装 boto3。"""
+ try:
+ import boto3
+ from botocore.config import Config
+ except ImportError as exc:
+ raise ModuleNotFoundError(
+ "S3 IO requires optional dependencies. Install them with `pip install 'mineru[s3]'`."
+ ) from exc
+
+ return boto3, Config
+
+
class S3Reader(IOReader):
def __init__(
self,
@@ -24,6 +34,7 @@ class S3Reader(IOReader):
addressing_style (str, optional): Defaults to 'auto'. Other valid options here are 'path' and 'virtual'
refer to https://boto3.amazonaws.com/v1/documentation/api/1.9.42/guide/s3.html
"""
+ boto3, Config = _load_s3_client_dependencies()
self._bucket = bucket
self._ak = ak
self._sk = sk
@@ -91,6 +102,7 @@ class S3Writer(IOWriter):
addressing_style (str, optional): Defaults to 'auto'. Other valid options here are 'path' and 'virtual'
refer to https://boto3.amazonaws.com/v1/documentation/api/1.9.42/guide/s3.html
"""
+ boto3, Config = _load_s3_client_dependencies()
self._bucket = bucket
self._ak = ak
self._sk = sk
diff --git a/pyproject.toml b/pyproject.toml
index c15a3c6b..516940c7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -31,7 +31,6 @@ classifiers = [
"Programming Language :: Python :: 3.13",
]
dependencies = [
- "boto3>=1.28.43",
"click>=8.1.7",
"loguru>=0.7.2",
"numpy>=1.21.6",
@@ -70,7 +69,8 @@ test = [
"pytest",
"pytest-cov",
"coverage",
- "fuzzywuzzy"
+ "fuzzywuzzy",
+ "tomli; python_version < '3.11'",
]
vlm = [
"torch>=2.6.0,<3",
@@ -87,6 +87,9 @@ mlx = [
"mlx-vlm>=0.3.3,<0.4",
"mlx<=0.31.1",
]
+s3 = [
+ "boto3>=1.28.43",
+]
pipeline = [
"dill>=0.3.8,<1",
"PyYAML>=6.0.1,<7",
@@ -111,6 +114,7 @@ core = [
]
all = [
"mineru[core]",
+ "mineru[s3]",
"mineru[mlx] ; sys_platform == 'darwin'",
"mineru[vllm] ; sys_platform == 'linux'",
"mineru[lmdeploy] ; sys_platform == 'win32'",