Docling OCRIndependent Docling guide

RAG · chunking

Chunking Docling Output Without Destroying the Document

Most RAG failures are not embedding failures. They are chunking failures: a table split across six chunks, a caption separated from its figure, a number with no header. Docling gives you structure — chunking is where most people throw it away.

Updated 2026-10-26 · 4 min read

The short version

  • –Docling can emit chunks directly (--to chunks), and docling-serve exposes chunk endpoints — no need to chunk Markdown by hand.
  • –The chunkers work from the DoclingDocument, so headings, page numbers, captions and table identity survive into the chunk metadata.
  • –A table row without its header is not retrievable. Any chunker that splits by token count alone will produce exactly that.
  • –Fix chunk size by measuring retrieval on questions with known answers, not by copying "512 tokens" from a blog post.
  • –Keep the source page reference in metadata. It is what lets an answer cite where it came from — and what makes a wrong answer debuggable.

Why structure-first chunking is different

A classical pipeline goes PDF → text → split every N characters → embed. Everything the document knew about itself is gone by step two: which line was a heading, which block was a table, where a section started.

Docling keeps that structure in the DoclingDocument. Chunking on top of it means a chunk can carry its section path, the page it came from, and whether it is a paragraph, a list or part of a table. Retrieval then has something to filter and rank on, and generation has something to cite.

ApproachWhat a chunk containsTypical failure
Fixed-size text splitN tokens of flattened textTable rows with no header; a caption detached from its figure
Markdown split on headingsA section, usuallyOversized sections; tables broken mid-row; no page numbers
Docling hierarchical chunkA structure-bounded unit with metadataChunks too small on their own — needs merging
Docling hybrid chunkStructure-bounded unit, merged/split to a token budgetBudget set without measuring; tokenizer mismatch

The two chunkers

Docling's chunking lives in docling_core.transforms.chunker. Two classes cover almost every need:

  • HierarchicalChunker — emits chunks along the document's own hierarchy. Nothing is merged or split, so you get chunks as the document defines them. Reliable, and sometimes very small.
  • HybridChunker — starts from the same hierarchy, then merges undersized neighbours and splits oversized items to fit a token budget. This is the one you want when your embedding model has a hard limit.
Chunk a converted document (Python)
from docling.document_converter import DocumentConverter
from docling_core.transforms.chunker import HierarchicalChunker

doc = DocumentConverter().convert("filing.pdf").document

chunker = HierarchicalChunker()
chunks = list(chunker.chunk(doc))

for c in chunks:
    print(c.meta.headings, "|", c.meta.doc_items[0].prov[0].page_no)
    print(c.text[:200])

Class locations and constructor arguments have moved between releases (the hybrid chunker accepts a tokenizer and merge behaviour that the hierarchical one does not). Check the import path for your installed docling-core rather than copying an old snippet.

Or: let the CLI / service do it

In a service architecture, chunking belongs on the same side as parsing: the docling-serve v1 API exposes hybrid and hierarchical chunk endpoints, so your ingestion job posts one file and receives ready-to-embed chunks with metadata. That removes an entire class of "the Markdown looked fine but the chunks were garbage" bugs.

Chunk from the command line
# emit chunks instead of (or alongside) Markdown
docling convert manual.pdf --to chunks

# both, in one pass
docling convert manual.pdf --to md --to chunks

The table problem

Tables are where naive chunking does the most damage, and financial documents are almost entirely tables. Consider a balance sheet chunked by token count: chunk 3 contains eight rows of numbers with no column headers, no units, and no statement name. Embedded and retrieved, it is indistinguishable from noise — and worse, it looks like an answer.

Three rules that fix most of it:

  • Keep a table whole when it fits the budget. A 30-row table under your token limit should not be split.
  • When it must be split, repeat the header and the table caption in every piece, and record the row range in metadata.
  • Prefer filtering over hoping: chunk metadata (document name, section, table id, page) lets you restrict retrieval to the table you actually want instead of trusting similarity.

If the downstream task is arithmetic on a table — totals, averages, reconciliation — retrieval is the wrong architecture. Parse the table into a spreadsheet and compute on it. See Docling to Excel.

Choosing a chunk size, honestly

There is no correct number, but there are wrong ways to find it. The method that works:

1

Write 20 questions with known answers

Real questions your users would ask, each answerable from a specific section or table of your documents. This is your test set and it takes 30 minutes.

2

Index at two or three budgets

For example 300, 600 and 1000 tokens with the hybrid chunker, using the tokenizer of the embedding model you will actually deploy.

3

Measure retrieval, not vibes

For each question, does the correct chunk appear in the top 5? A budget that wins by 20 points on your own questions is a real result; a benchmark on someone else's corpus is not.

4

Then measure the answers

Retrieval hit rate can be fine while generation still fails, because three near-duplicate chunks crowd out the one that mattered. Deduplicate and cap chunks per document in the prompt.

Metadata worth keeping

  • Section path — the heading chain above the chunk. Cheap to store, powerful as a filter.
  • Page number — from Docling provenance. Turns "the model said so" into "page 14, table 2".
  • Element type — paragraph, list, table, caption. Lets you exclude captions from arithmetic questions or include them for definitions.
  • Document identity and version — so re-ingestion replaces rather than duplicates.
  • Table identity and row range — for split tables, the minimum needed to reassemble them.

When chunking is the wrong problem

If most questions are "what is the value of X in this document", parsing quality dominates everything. A cleaner parse with mediocre chunking beats a broken parse with perfect chunking, every time.

So before tuning chunk size, look at the parse: are headers attached, are tables cells rather than lines of text, are page numbers present? If not, the OCR or table settings need work first — the chunker cannot recover structure the parser never produced.

Frequently asked questions

Does Docling chunk documents itself?+

Yes. The CLI can emit chunks (--to chunks) and docling-serve has chunk endpoints. Chunking is also available as a library via the hierarchical and hybrid chunkers in docling-core.

What is the difference between the hierarchical and hybrid chunker?+

The hierarchical chunker emits chunks exactly along the document structure. The hybrid chunker additionally merges small neighbours and splits oversized items to a token budget, which is usually what an embedding pipeline needs.

What chunk size should I use for Docling output?+

Measure it. Write twenty questions with known answers and compare retrieval hit rate at two or three budgets using your embedding model's tokenizer. 600 tokens is a common starting point, not a rule.

How do I keep tables intact when chunking?+

Chunk from the DoclingDocument rather than from flattened Markdown, keep tables whole while they fit the budget, and repeat headers plus caption when splitting. Metadata with the table id and row range lets you reassemble later.

Can I use Docling chunks with LangChain or LlamaIndex?+

Yes — the chunks are plain text plus metadata, so they can be mapped into either framework's document type. The important part is preserving the metadata rather than passing only c.text.

Try it online

Run the same pipeline in the browser — no install, no uploads stored.

Try it in the browser

Keep reading