Reader¶
Introduction¶
The Reader component is designed to read files homogeneously which come from many different formats and extensions. All of these readers are implemented sharing the same parent class, BaseReader
.
Which Reader should I use for my project?¶
Each Reader component extracts document text in different ways. Therefore, choosing the most suitable Reader component depends on your use case.
- If you want to preserve the original structure as much as possible, without any kind of markdown parsing, you can use the
VanillaReader
class. - In case that you have documents which have presented many tables in its structure or with many visual components (such as images), we strongly recommend to use
DoclingReader
. - If you are looking to maximize efficiency or make conversions to markdown simpler, we recommend using the
MarkItDownReader
component.
Note
Remember to visit the official repository and guides for these two last reader classes:
- Docling Developer guide
- MarkItDown GitHub repository.
Additionally, the file compatibility depending on the Reader class is given by the following table:
Reader | Unstructured files & PDFs | MS Office suite files | Tabular data | Files with hierarchical schema | Image files | Markdown conversion |
---|---|---|---|---|---|---|
VanillaReader |
txt , md , pdf |
xlsx , docx , pptx |
csv , tsv , parquet |
json , yaml , html , xml |
jpg , png , webp , gif |
Yes |
MarkItDownReader |
txt , md , pdf |
docx , xlsx , pptx |
csv , tsv |
json , html , xml |
jpg , png , pneg |
Yes |
DoclingReader |
txt , md , pdf |
docx , xlsx , pptx |
– | html , xhtml |
png , jpeg , tiff , bmp , webp |
Yes |
Output format¶
Bases: BaseModel
Pydantic model defining the output structure for all readers.
Attributes:
Name | Type | Description |
---|---|---|
text |
Optional[str]
|
The textual content extracted by the reader. |
document_name |
Optional[str]
|
The name of the document. |
document_path |
str
|
The path to the document. |
document_id |
Optional[str]
|
A unique identifier for the document. |
conversion_method |
Optional[str]
|
The method used for document conversion. |
reader_method |
Optional[str]
|
The method used for reading the document. |
ocr_method |
Optional[str]
|
The OCR method used, if any. |
page_placeholder |
Optional[str]
|
The placeholder use to identify each page, if used. |
metadata |
Optional[Dict[str, Any]]
|
Additional metadata associated with the document. |
Source code in src/splitter_mr/schema/models.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
default_document_id(v)
¶
Generate a default UUID for document_id if not provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v
|
str
|
The provided document_id value. |
required |
Returns:
Name | Type | Description |
---|---|---|
document_id |
str
|
The provided document_id or a newly generated UUID string. |
Source code in src/splitter_mr/schema/models.py
33 34 35 36 37 38 39 40 41 42 43 44 |
|
from_variable(variable, variable_name)
¶
Generate a new ReaderOutput object from a variable (str or dict).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable
|
Union[str, Dict[str, Any]]
|
The variable to use as text. |
required |
variable_name
|
str
|
The name for document_name. |
required |
Returns:
Name | Type | Description |
---|---|---|
ReaderOutput |
ReaderOutput
|
The new ReaderOutput object. |
Source code in src/splitter_mr/schema/models.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
append_metadata(metadata)
¶
Append (update) the metadata dictionary with new key-value pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata
|
Dict[str, Any]
|
The metadata to add or update. |
required |
Source code in src/splitter_mr/schema/models.py
81 82 83 84 85 86 87 88 89 90 |
|
Readers¶
BaseReader¶
BaseReader
¶
Bases: ABC
Abstract base class for all document readers.
This interface defines the contract for file readers that process documents and return
a standardized dictionary containing the extracted text and document-level metadata.
Subclasses must implement the read
method to handle specific file formats or reading
strategies.
Methods:
Name | Description |
---|---|
read |
Reads the input file and returns a dictionary with text and metadata. |
is_valid_file_path |
Check if a path is valid. |
is_url |
Check if the string provided is an URL. |
parse_json |
Try to parse a JSON object when a dictionary or string is provided. |
Source code in src/splitter_mr/reader/base_reader.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
is_valid_file_path(path)
staticmethod
¶
Checks if the provided string is a valid file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The string to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the string is a valid file path to an existing file, False otherwise. |
Example
BaseReader.is_valid_file_path("/tmp/myfile.txt")
True
Source code in src/splitter_mr/reader/base_reader.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
is_url(string)
staticmethod
¶
Determines whether the given string is a valid HTTP or HTTPS URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
The string to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the string is a valid URL with HTTP or HTTPS scheme, False otherwise. |
Example
BaseReader.is_url("https://example.com")
True
BaseReader.is_url("not_a_url")
False
Source code in src/splitter_mr/reader/base_reader.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
parse_json(obj)
staticmethod
¶
Attempts to parse the provided object as JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[dict, str]
|
The object to parse. If a dict, returns it as-is. If a string, attempts to parse it as a JSON string. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The parsed JSON object. |
Raises:
Type | Description |
---|---|
ValueError
|
If a string is provided that cannot be parsed as valid JSON. |
TypeError
|
If the provided object is neither a dict nor a string. |
Example
BaseReader.try_parse_json('{"a": 1}')
{'a': 1}
BaseReader.try_parse_json({'b': 2})
{'b': 2}
BaseReader.try_parse_json('[not valid json]')
ValueError: String could not be parsed as JSON: ...
Source code in src/splitter_mr/reader/base_reader.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
read(file_path, model=None, **kwargs)
abstractmethod
¶
Reads input and returns a ReaderOutput with text content and standardized metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to the input file, a URL, raw string, or dictionary. |
required |
model
|
Optional[BaseModel]
|
Optional model instance to assist or customize the reading or extraction process. Used for cases where VLMs or specialized parsers are required for processing the file content. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for implementation-specific options. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ReaderOutput |
ReaderOutput
|
Dataclass defining the output structure for all readers. |
Raises:
Type | Description |
---|---|
ValueError
|
If the provided string is not valid file path, URL, or parsable content. |
TypeError
|
If input type is unsupported. |
Example
class MyReader(BaseReader):
def read(self, file_path: str, **kwargs) -> ReaderOutput:
return ReaderOutput(
text="example",
document_name="example.txt",
document_path=file_path,
document_id=kwargs.get("document_id"),
conversion_method="custom",
ocr_method=None,
metadata={}
)
Source code in src/splitter_mr/reader/base_reader.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
📚 Note: file examples are extracted from the
data
folder in the GitHub repository: link.
VanillaReader¶
VanillaReader
¶
Bases: BaseReader
Read multiple file types using Python's built-in and standard libraries. Supported: .json, .html, .txt, .xml, .yaml/.yml, .csv, .tsv, .parquet, .pdf
For PDFs, this reader uses PDFPlumberReader to extract text, tables, and images, with options to show or omit images, and to annotate images using a vision model.
Source code in src/splitter_mr/reader/readers/vanilla_reader.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
|
read(file_path=None, **kwargs)
¶
Read a document from various sources and return standardized output.
This method supports:
- Local file paths (file_path
or positional arg)
- URLs (file_url
)
- JSON/dict objects (json_document
)
- Raw text strings (text_document
)
If multiple sources are provided, the priority is:
file_path
> file_url
> json_document
> text_document
.
If only file_path
is provided, auto-detects whether it is a path, URL,
JSON, YAML, or plain text.
file_path (str | Path): Path to the input file (overridden by
kwargs['file_path']
if present).
**kwargs: Optional arguments that adjust behavior:
Source selection:
file_path (str): Path to the input file (overrides positional arg).
file_url (str): HTTPS/HTTP URL to read from.
json_document (dict | str): JSON-like document (dict or JSON string).
text_document (str): Raw text content.
Identification/metadata:
document_id (str): Explicit document id. Defaults to a new UUID.
metadata (dict): Additional metadata to attach to the output.
PDF extraction:
scan_pdf_pages (bool): If True, rasterize and describe pages using a
vision model (VLM). If False (default), use element-wise extraction.
model (BaseModel): Vision-capable model used for scanned PDFs and/or
image captioning (also used for image files).
prompt (str): Prompt for image captioning / page description. Defaults to
``DEFAULT_IMAGE_CAPTION_PROMPT`` for element-wise PDFs and
``DEFAULT_IMAGE_EXTRACTION_PROMPT`` for scanned PDFs/images.
resolution (int): DPI when rasterizing pages for VLM. Default: 300.
show_base64_images (bool): Include base64-embedded images in PDF output.
Default: False.
image_placeholder (str): Placeholder for omitted images in PDFs.
Default: ``"<!-- image -->"``.
page_placeholder (str): Placeholder inserted between PDF pages (only
surfaced when scanning or when the placeholder occurs in text).
Default: ``"<!-- page -->"``.
vlm_parameters (dict): Extra keyword args forwarded to
``model.extract_text(...)``.
Excel / Parquet reading:
as_table (bool): For Excel (``.xlsx``/``.xls``), if True read as a table
using pandas and return CSV text. If False (default), convert to PDF
and run the PDF pipeline.
excel_engine (str): pandas Excel engine. Default: ``"openpyxl"``.
parquet_engine (str): pandas Parquet engine (e.g. ``"pyarrow"``,
``"fastparquet"``). Default: pandas auto-selection.
Returns:
Name | Type | Description |
---|---|---|
ReaderOutput |
ReaderOutput
|
Unified result containing text, metadata, and extraction info. |
Raises:
Type | Description |
---|---|
ValueError
|
If the source is invalid/unsupported, or if a VLM is required but not provided. |
TypeError
|
If provided arguments are of unsupported types. |
Notes
- PDF extraction now supports image captioning/omission indicators.
- For
.parquet
files, content is loaded via pandas and returned as CSV-formatted text.
Example
from splitter_mr.readers import VanillaReader
from splitter_mr.models import AzureOpenAIVisionModel
model = AzureOpenAIVisionModel()
reader = VanillaReader(model=model)
output = reader.read(file_path="https://raw.githubusercontent.com/andreshere00/Splitter_MR/refs/heads/main/data/lorem_ipsum.pdf")
print(output.text)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget purus non est porta
rutrum. Suspendisse euismod lectus laoreet sem pellentesque egestas et et sem.
Pellentesque ex felis, cursus ege...
Source code in src/splitter_mr/reader/readers/vanilla_reader.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
SimpleHTMLTextExtractor
¶
Bases: HTMLParser
Extract HTML Structures from a text
Source code in src/splitter_mr/reader/readers/vanilla_reader.py
695 696 697 698 699 700 701 702 703 704 705 706 |
|
VanillaReader
uses a helper class to read PDF and use Visual Language Models. This class is PDFPlumberReader
.
DoclingReader¶
DoclingReader
¶
Bases: BaseReader
High-level document reader leveraging IBM Docling for flexible document-to-Markdown conversion, with optional image captioning or VLM-based PDF processing. Supports automatic pipeline selection, seamless integration with custom vision-language models, and configurable output for both PDF and non-PDF files.
Source code in src/splitter_mr/reader/readers/docling_reader.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
read(file_path, **kwargs)
¶
Reads a document, automatically selecting the appropriate Docling pipeline for extraction. Supports PDFs (per-page VLM or standard extraction), as well as other file types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
Path or URL to the document file. |
required |
**kwargs
|
Any
|
Keyword arguments to control extraction, including: - prompt (str): Prompt for image captioning or VLM-based PDF extraction. - scan_pdf_pages (bool): If True (and model provided), analyze each PDF page via VLM. - show_base64_images (bool): If True, embed base64 images in Markdown; if False, use image placeholders. - page_placeholder (str): Placeholder for page breaks in output Markdown. - image_placeholder (str): Placeholder for image locations in output Markdown. - image_resolution (float): Resolution scaling factor for image extraction. - document_id (Optional[str]): Optional document ID for metadata. - metadata (Optional[dict]): Optional metadata dictionary. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ReaderOutput |
ReaderOutput
|
Extracted document in Markdown format and associated metadata. |
Raises:
Type | Description |
---|---|
Warning
|
If a file extension is unsupported, falls back to VanillaReader and emits a warning. |
ValueError
|
If PDF pipeline requirements are not satisfied (e.g., neither model nor show_base64_images provided). |
Source code in src/splitter_mr/reader/readers/docling_reader.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
To execute pipelines, DoclingReader has a utils class, DoclingUtils
.
MarkItDownReader¶
MarkItDownReader
¶
Bases: BaseReader
Read multiple file types using Microsoft's MarkItDown library, and convert the documents using markdown format.
This reader supports both standard MarkItDown conversion and the use of Vision Language Models (VLMs) for LLM-based OCR when extracting text from images or scanned documents.
Source code in src/splitter_mr/reader/readers/markitdown_reader.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
read(file_path=None, **kwargs)
¶
Reads a file and converts its contents to Markdown using MarkItDown.
Features
- Standard file-to-Markdown conversion for most formats.
- LLM-based OCR (if a Vision model is provided) for images and scanned PDFs.
- Optional PDF page-wise OCR with fine-grained control and custom LLM prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to the input file to be read and converted. |
None
|
**kwargs
|
Any
|
|
{}
|
Returns:
Name | Type | Description |
---|---|---|
ReaderOutput |
ReaderOutput
|
Dataclass defining the output structure for all readers. |
Example
from splitter_mr.model import OpenAIVisionModel
from splitter_mr.reader import MarkItDownReader
model = AzureOpenAIVisionModel()
reader = MarkItDownReader(model=model)
output = reader.read(file_path="https://raw.githubusercontent.com/andreshere00/Splitter_MR/refs/heads/main/data/lorem_ipsum.pdf")
print(output.text)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget purus non est porta
rutrum. Suspendisse euismod lectus laoreet sem pellentesque egestas et et sem.
Pellentesque ex felis, cursus ege...
Source code in src/splitter_mr/reader/readers/markitdown_reader.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|