docling icon indicating copy to clipboard operation
docling copied to clipboard

Discussion: PDF bookmarks/ToC as an authoritative heading-hierarchy signal for hierarchy-aware chunking (follow-up #3633)

Open DanielNg0729 opened this issue 5 months ago • 3 comments

Follow-up: use PDF bookmarks / ToC as an authoritative heading-hierarchy signal

Hi Dr @cau-git, Dr @PeterStaar-IBM. I'm writing this to follow-up with PR #3633. As discussed there, PDF bookmarks / table-of-contents are usually the most reliable hierarchy signal when a document has them, and were intentionally left out of that PR. This issue proposes a concrete approach for that follow-up so we can align before I implement.

Why this matters beyond Markdown: chunking

Docling's HierarchicalChunker and HybridChunker (from docling-core) derive both chunk grouping and the heading breadcrumb (DocMeta.headings, used by HybridChunker.contextualize()) from the DoclingDocument heading hierarchy. When every PDF heading collapses to level 1:

  • subsections aren't nested under their parent section, so chunks can merge or split across logical boundaries, and
  • the heading path prepended to each chunk for embedding loses its nesting (e.g. PART I › 1. Definitions › (a) becomes a flat list).

So an accurate hierarchy isn't just cosmetic Markdown, it directly improves retrieval/RAG chunk quality. #3633 helps via numbering/style; bookmarks close the gap for documents that ship a real outline.

Proposed approach

  1. Extract the outline at the backend. Add get_document_outline() to PdfDocumentBackend, returning a tree of (title, page_no, optional y-position, level). Implement it for:
    • the pypdfium2 backend via PdfDocument.get_toc(), and
    • the docling-parse backends where the outline is available.
  2. Surface it on the ConversionResult (or per page), alongside the parsed pages, so it survives into the stage that needs it without bloating the final DoclingDocument.
  3. Consume it in HeadingHierarchyModel. Introduce the _infer_from_bookmarks signal (the extension point from #3633) and wire it as the authoritative source: bookmarks (when matched) > numbering > style.
  4. Matching. Map each bookmark to the nearest detected SECTION_HEADER by title + target page (+ vertical position when present); the bookmark tree depth sets the level. Unmatched bookmarks and unmatched headings fall back to numbering/style.
  5. No chunker changes needed. Because the chunkers already read hierarchy from the document, the improvement flows through automatically. This stays a PDF-pipeline change.

Points worth deciding (for discussion)

  • Matching confidence: how strict should the title/page match be, and should a low-confidence bookmark defer to numbering rather than overriding it?
  • Partial / wrong outlines: many PDFs have outlines that don't cover every heading (or are auto-generated and noisy). Proposal is to apply bookmarks only where confidently matched and leave the rest to numbering/style, so that it does not degrading the #3633 result.
  • **Scope:**Can i keep this PDF-only for now? OCR/VLM-parsed PDFs without an embedded outline simply won't have this signal.

I'm happy to implement along these lines. Opening it up first in case you would shape any of the above differently. Thanks!

DanielNg0729 avatar Jun 23 '26 03:06 DanielNg0729

@DanielNg0729 Thanks for taking the lead on this! I would suggest we finalize #3633 first and then indeed add this new feature.

Note:

  1. I know that sometimes list-items get confused with section-headers, so be aware that if you try to match the headings from a TOC, the missing items might be mis-classified as list-items
  2. often the markers are not in the TOC of the pdf, so you will need to do some fuzzy text matching

PeterStaar-IBM avatar Jun 23 '26 05:06 PeterStaar-IBM

This is a critical signal for legal documents — PDF bookmarks are often the only reliable heading hierarchy in court filings, contracts, and regulatory submissions.

Key legal-domain considerations:

  1. Bookmark ≠ visible heading: Many legal PDFs have bookmarks that don't correspond to any visible text in the document. For example, a contract might have bookmarks for "Schedule A" and "Schedule B" but these only appear as small text in the sidebar. If we only use visible heading detection, we miss the hierarchical structure entirely.

  2. Bookmark order matters more than heading level: In legal documents, the bookmark tree often represents the intended reading order. A bookmark at level 3 might be more important than a level 2 heading if it represents a cross-reference clause. We should consider both depth AND position in the bookmark tree.

  3. Redaction creates bookmark gaps: When legal documents are redacted, bookmarks pointing to redacted sections are sometimes kept (for structural integrity) and sometimes removed (for security). This creates inconsistent hierarchy signals that need special handling.

  4. Suggestion: Consider exposing bookmark hierarchy as a structural_role metadata field alongside the text content. For legal use cases, this would allow chunking algorithms to respect document structure rather than just character/token boundaries. Something like:

    {
      "bookmark_path": ["Agreement", "Section 3", "Representations"],
      "bookmark_level": 3,
      "has_visible_heading": false,
      "heading_source": "bookmark"
    }
    

This would be a huge win for legal document processing where the bookmark tree IS the authoritative TOC.

zeweihan avatar Jun 23 '26 10:06 zeweihan

+1 on keeping bookmarks authoritative only when matched confidently. From a RAG/chunking angle, I’d also preserve the source of the hierarchy decision in metadata rather than flattening it into level alone.

For example, a chunk could carry heading_path, heading_source: bookmark | numbering | style, maybe bookmark_path when available, and a match confidence. That would let downstream chunkers/retrievers make conservative choices: trust bookmark-derived hierarchy more for section routing, but fall back when the outline is partial/noisy.

I’d be careful with bookmark-only nodes that do not correspond to visible text. They are valuable structure, but treating them as normal text chunks can create citation/provenance confusion. I’d prefer them as structural metadata attached to nearby visible content, unless Docling can expose a clear structure-only role.

GioiaZheng avatar Jun 23 '26 12:06 GioiaZheng