Retrieval can only be as good as the chunks it retrieves. A chunk that cuts a table in half, or a clause away from the heading that says which coverage it belongs to, will be found for the wrong questions and missed for the right ones. This article follows a PDF through InsightRAG's extractor, step by step, so you can see where those decisions are made and which ones you control.
The extraction profile
Every DOC task and every direct upload names an extraction profile. It is a small set of settings:
| Setting | What it controls |
|---|---|
chunk_size, chunk_overlap | How long a text chunk may be, and how much consecutive chunks share. |
format | text for plain text, markdown to keep headings, lists and tables as structure. |
pdf_engine | fitz (PyMuPDF) to read page by page, or MarkItDown to convert the whole document in one pass. |
model | Optional. When set, each page is rendered as an image and a model turns it into Markdown sections. |
write_components | Whether to save tables and images found on each page as separate files. |
The same profile is used for every document in a workflow, which is what makes results comparable: if a policy schedule is chunked one way today, it is chunked the same way next month.
Step 1: read the page, or OCR it
With the fitz engine the extractor opens the PDF from bytes and works one page at a time. If a page has extractable text, it
uses it. If the page has no text, which is what a scanned page or a photographed form looks like to a PDF library, it falls back to
OCR (Tesseract) on the page image. Mixed documents (a typed application with a scanned signature page, a binder with a faxed endorsement) are common
in practice, and per-page fallback means one scanned page doesn't force the whole file through the slow path.
For large backfills of scanned material, the pipelines repository also has batch jobs that convert PDFs on Glue with dedicated document-conversion and OCR libraries, so a history load doesn't have to run through the API at all.
Step 2: text or Markdown
With format: text the page text is split recursively to the profile's chunk size and overlap: by paragraph first, then by
line, then by word, so a chunk breaks at the most natural boundary that fits.
With format: markdown the extractor keeps structure. Without a model, a Markdown parser converts the page text. With a model, the
page is rendered to an image and the model returns typed Markdown sections, which helps with layouts that plain text extraction scrambles:
multi-column pages, forms, schedules laid out as boxes.
Step 3: split along the document's own structure
Markdown is split by its headings first (#, ## and ###), keeping the heading in the chunk, and then by
lines that are entirely bold, which is how many policy forms, contracts and procedures mark their sections without real headings. Each
resulting chunk records the headings above it as metadata. A clause from Section II, Exclusions therefore arrives at the model with
"Exclusions" attached, not as a free-floating paragraph.
Step 4: tables and images as components
The profile's write_components switch asks the extractor to pull tables and images out of each page as separate components,
named with the document's id so each one can be traced back to its page. This matters for documents where the numbers live in tables (rate
schedules, limits, loss runs). How much is extracted depends on the PDF engine, so test it on a sample of your own documents before you rely on
it, and keep the Markdown output, which already carries simple tables as text, as the primary path.
Step 5: metadata on every chunk
Each chunk carries its type (text, markdown or a section type from the model), its page and its chunk
number within the page. When the chunk is loaded into the vector index it also gets the document-level fields: file name and file id, source
and source path, who created it and when, and the business fields from the workflow (lob, product,
category, subcategory, catalog_guid). Those are the fields
retrieval can filter on.
Choosing settings
There is no universal chunk size. Short, dense documents (FAQs, policies with numbered clauses) work well with smaller chunks; long narrative documents (reports, procedures) usually need larger chunks and some overlap so that an explanation isn't split from its conclusion. The direct-upload path in the API, for example, uses a large chunk (5,000 characters) with a 1,000-character overlap and Markdown output, which suits long business documents. Our guide to chunk size and extraction settings gives a way to choose with your own documents and questions rather than by rule of thumb.
A worked example: one schedule page, two profiles
Take a made-up commercial property binder whose declarations page is laid out in two columns, with a boxed limits schedule and the section
heading Limits of Insurance in bold type rather than as a real heading. Under a text profile with the fitz
engine, the page's text is read in the order the PDF stores it, which for two-column layouts can interleave the columns, and the limits box
becomes a run of words and numbers. The recursive splitter then cuts it at paragraph and line breaks, and no chunk records which section it
belongs to.
Under a markdown profile, the bold-only heading line is recognised as a section break, so the chunk that holds the limits carries
Limits of Insurance as metadata. Add a layout model and the page is read as an image, which is where the two columns and the
boxed schedule have the best chance of coming out in reading order. The cost is a model call per page and a slower run, which is why it is
worth testing on the document types that need it rather than switching it on everywhere.
Checking the output: what to look for
- Empty or near-empty pages: a scanned page where OCR found little, or an image-only page with nothing to read.
- Interleaved columns: sentences that jump between two topics mid-line.
- Flattened tables: numbers that have lost their row and column labels.
- Missing headings: Markdown chunks whose heading metadata is empty although the page plainly has sections.
- Headers and footers repeated on every page, which add noise to every chunk.
Questions people ask
How well does OCR handle poor scans and handwriting?
The fallback uses Tesseract, which does well on clean typed scans and much less well on faxes, skewed pages and handwriting. Test a sample of your worst documents; for a large scanned backlog, the batch conversion jobs are the better route.
Do page numbers survive?
Yes. Every chunk records its page and chunk number, which is what lets an answer cite the page it came from.
Can different document types use different settings?
Yes, by giving them separate workflows, each naming its own extraction profile. Within one workflow every document gets the same profile.