AI Layer

Document Parsing for RAG

Compare Docling and Unstructured for converting PDFs and HTML into traceable, reviewable elements and chunks for retrieval augmented generation.

Published by How to Train Your Data

Last updated: 2026-10-26

Sources

Why parsing belongs in the data pipeline

A RAG corpus begins with source documents, not embeddings. PDFs and HTML contain layout, headings, tables, links, and metadata that may be lost when a pipeline extracts plain text without preserving structure. Parsing turns a source file into elements that can be validated, filtered, chunked, and traced. Docling describes conversion into a structured document representation with export options; Unstructured describes partitioning raw documents into elements such as titles and narrative text. Both projects expose a pipeline stage before chunking and vectorization.

The data engineer should treat parsed output as a derived dataset. Record source URI or stable source ID, content hash, parser and configuration version, extraction time, page or section context, and any review flags. Store enough provenance to recreate a chunk and return a citation to its source. Do not assume a parser can infer whether an extracted number, table cell, or footnote is semantically correct. Sampling and validation are still necessary, especially for scanned pages and complex layouts.

A useful pipeline separates acquisition, parsing, normalization, chunking, embedding, indexing, and serving. This makes failures easier to localize: a missing table can be an extraction issue, a broken passage can be a chunk boundary issue, and an irrelevant result can be an embedding or retrieval issue. Preserve an intermediate representation so parser changes can be compared without rerunning every downstream stage immediately.

Docling and Unstructured in context

Docling's official project page lists support for multiple formats and a structured DoclingDocument representation, with exports including Markdown and JSON. Its repository quickstart demonstrates installation and conversion using DocumentConverter. The documentation also includes separate examples for simple conversion, advanced options, formats, and chunking. Consider Docling when its document model, conversion flow, and integrations fit the formats and deployment constraints you have verified.

Unstructured's open-source documentation describes partition functions that detect a file type and produce typed elements, with format-specific functions and strategies for PDFs and images. Its chunking documentation explains that chunking operates on document elements and generally preserves whole elements except where a single element exceeds the configured maximum. Consider this workflow when element-oriented processing and the documented partition/chunk sequence fit your ingestion design. Check the specific package and system requirements for the formats you will process.

This comparison is limited to documented workflow shape, not extraction accuracy. Neither a feature list nor an example PDF establishes which parser performs better on your documents. Build a representative, permissioned corpus that includes born-digital PDFs, scanned pages, tables, headings, and the HTML patterns you actually receive. Compare extraction completeness, element structure, processing errors, runtime environment, and downstream retrieval on manually reviewed examples.

Turn parsed documents into RAG chunks

Start by checking element quality before chunking. Confirm that headings and paragraphs remain in useful order, that tables retain enough structure to interpret values, and that page references can be carried into citations. Exclude navigation or boilerplate only with a documented rule. Preserve the source text or an approved raw artifact so a reviewer can resolve differences between parser output and the original document.

Chunking should respect the source structure where possible. A heading can be attached to its following paragraphs; a table should remain together when feasible; a very long section can be split with enough repeated metadata to keep each chunk interpretable. The Unstructured docs expose options such as maximum and preferred character counts, while Docling documents chunking and serialization examples. Treat example defaults as starting points. Tune using retrieval queries and human judgments, not a generic chunk length rule.

Keep provenance in every chunk: document ID, source version, section or page, parser version, chunker configuration, and any access-control attributes needed by retrieval. If the same content appears in multiple source files, define deduplication and canonical-source rules. The index should be rebuildable from the source and transformations. If the parser changes, compare old and new output and decide whether existing embeddings must be refreshed.

Choosing between the tools

Choose by format coverage and deployment fit first. If documents must remain in a restricted environment, confirm local processing behavior and model dependencies. If you receive HTML, validate the actual page structure rather than assuming every page behaves like a static file. If PDFs contain tables or scans, inspect those cases directly and review OCR output. A tool that handles one sample does not establish reliable support for your full corpus.

Then compare developer workflow and operational costs. Record setup friction, configuration needed for each format, output schema stability, error visibility, reproducibility, and memory or compute requirements on the target host. Identify whether the pipeline can retry a failed document without duplicating its index records. Treat parser upgrades as data migrations: a changed output can affect chunk IDs, citations, embeddings, and answer quality.

A practical rollout begins with a shadow corpus. Run both parsers over an approved sample, compare extracted elements and chunk boundaries, then ask a domain reviewer to inspect the differences. Follow with a retrieval evaluation using known questions and source passages. Record the limitations and select a default with a fallback path for files requiring manual review. Do not publish a universal accuracy or speed ranking without a measured, documented experiment.

Gotchas for documents in production

Parsing can be expensive and inconsistent across input types. Capture per-file status, warnings, and enough error context to retry. Quarantine unsupported or corrupted documents rather than silently indexing empty text. Control batch size and worker parallelism based on the target machine, and keep untrusted files isolated from systems with broad credentials.

HTML and PDFs may include sensitive or copyrighted content. Confirm permission to store, transform, and send each source to any external service. If using a hosted parser or model, review data handling and regional requirements. Do not send restricted content to an API merely because a local prototype did not need credentials.

A parser output is not a citation by itself. Preserve exact source references and render them in the application in a way a user can open and verify. Test deletions and source replacements through the full chain so obsolete chunks cannot continue appearing in search. Monitor parser error rates and retrieval failures with privacy-safe identifiers, then sample output quality after material parser, OCR, or chunking changes.

Learning map

Stage 1: Audit your input documents

Describe file types, layout patterns, permissions, and expected output.

Stage 2: Compare parser outputs

Inspect elements and failures before choosing chunk rules.

Stage 3: Validate retrieval readiness

Check chunk quality, citations, updates, and end-to-end retrieval.

Get hands-on: step by step

  1. Prepare a safe document sample

    Select a small set of documents you are authorized to process. Include one text PDF, one table-heavy file, one scan if available, and a representative HTML page. Keep originals unchanged and write down expected headings, tables, and citation locations.

    mkdir parser-pilot
  2. Install Docling and convert locally

    Follow the official <a href="https://github.com/docling-project/docling" rel="noopener noreferrer">Docling quickstart</a>. The docs show `pip install docling` and `DocumentConverter`. This page was checked against Docling 2.142.1, listed on its official release page on 2026-10-26; keep the installed version and Python version with the output.

    python -m pip install docling==2.142.1
  3. Inspect the Docling representation

    Follow the <a href="https://github.com/docling-project/docling/blob/main/docs/v2.md" rel="noopener noreferrer">official CLI example</a> to convert an approved local PDF. Compare page order, headings, table content, and source references with the original. Keep warnings and conversion settings with the result.

    docling ./sample.pdf
  4. Install and run Unstructured partitioning

    Follow the official <a href="https://docs.unstructured.io/open-source/installation/full-installation" rel="noopener noreferrer">installation guide</a> and <a href="https://docs.unstructured.io/open-source/core-functionality/partitioning" rel="noopener noreferrer">partitioning docs</a> for your platform. Run `partition` on a compatible local file and inspect element types and metadata. This page was checked against Unstructured 0.27.34, listed on the official release page on 2026-10-26. Package extras and system dependencies vary by format.

    python -m pip install unstructured==0.27.34
  5. Create chunks and review boundaries

    Use the current <a href="https://docs.unstructured.io/open-source/core-functionality/chunking" rel="noopener noreferrer">chunking guide</a> or Docling chunking example. Keep a heading with its related content and review oversized elements and tables. Do not select a chunk size from a demo without testing retrieval.

    python chunk_sample.py
  6. Run a retrieval quality check

    Build a small set of questions whose answers are tied to reviewed source passages. Compare whether each parser produces chunks that retrieve those passages and preserve a useful citation. Record parsing errors, missing structure, and stale-source behavior before choosing a production default.

  7. Write the reprocessing plan

    Specify source hashes, parser and chunker versions, chunk IDs, embedding refresh, deletion propagation, restricted data handling, and human review. Keep a procedure for rerunning a document without duplicating index records.

Key resources

Related topics

More in ai-layer

Stay current on AI data engineering

New resources and perspective on building AI-ready data systems, a few times a month. No spam.