Heading level detection for Roman numeral headings (I, II) in PDF to Markdown conversion
Hi Docling team and dev, I am currently working on a legal contract analysis project and using Docling to convert contract PDFs into Markdown for downstream processing with LLMs. While testing on several contract documents, I noticed what appears to be an issue with heading hierarchy detection.
Many contracts use a structure similar to:
PART I
-
Definitions
-
Interpretation
PART II
- Obligations
where "PART I", "PART II", etc. are higher-level sections, and the numbered items beneath them are subsections.
However, after conversion, Docling sometimes assigns the same Markdown heading level to both the PART headings and the numbered section headings, which causes the document hierarchy to be flattened.
I would like to understand whether this is expected behavior, a limitation of the current heading detection logic, or if there is a configuration/recommended approach to preserve this hierarchy.
Bug
Docling does not correctly infer heading hierarchy when a document uses Roman numerals for major sections and Arabic numerals for subsections.
For example, in the source PDF:
I. Introduction
1. Background
2. Motivation
II. Methodology
1. Data Collection
The Roman numeral headings (I., II.) represent higher-level sections, while the Arabic numeral headings (1., 2.) are subsections.
However, Docling assigns the same heading level to both types of headings in the generated Markdown.
Steps to reproduce
- Convert a PDF containing Roman numeral section headings and Arabic numeral subsection headings.
- Export to Markdown.
- Observe that both heading types receive the same Markdown heading level.
Docling version
<Docling version: 2.97.0>
Python version
<Python 3.13.6>
Additional information
- Latest Docling release.
- PDF is text-based
- The document contains only text and bold text with index in Section, roman number, interger.
- Is there any configuration option or customization mechanism to preserve this heading hierarchy? Thanks!
Hi @DanielNg0729! I'm Dosu and I'm helping the IBM Docling team.
This is a known architectural limitation rather than a bug. Docling's PDF pipeline assigns all detected section headers to heading level 1 by default — the layout model classifies regions as SECTION_HEADER but does not distinguish between different heading levels [1]. Font size, boldness, and numbering patterns (including Roman vs. Arabic numerals) are not used to infer hierarchy in PDF conversion [2].
This contrasts with other backends (e.g., DOCX, LaTeX) that can extract heading levels from native format metadata [3].
Why it happens: The layout model provides binary classification (section header vs. not) without level information. All headings end up as level=1 with parent=#/body, flattening the hierarchy [4].
No built-in configuration exists to customize heading level detection for PDFs.
Workarounds:
hierarchize()transform fromdocling-core— restructures the document tree after conversion by inferring hierarchy from font properties [1]. See docling-core PR #572.- Post-process programmatically — after conversion, iterate over the
DoclingDocumentand reassign heading levels based on your own heuristics (e.g., detecting Roman numeral patterns and assigning them a higher level). - If your source is available in DOCX format, converting from that instead of PDF will preserve native heading hierarchy.
There have been draft/closed PRs attempting to add font-based hierarchy inference (#2676, #2421) but none have been merged yet, indicating this remains an open area of development.
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
I'm following up on this with more detail after digging into the pipeline.
I initially filed this as a font/styling issue, but on inspecting readingorder_model.py I think the root cause is more fundamental: the standard PDF pipeline calls add_heading(text=..., prov=..., hyperlink=...) without a level argument, so it defaults to level=1. There doesn't appear to be any heading-level inference step in the PDF path, every detected SECTION_HEADER collapses to the same level regardless of numbering or styling. (The markup backends pass level= because they have native heading markup; PDFs have none.) So this is closer to an unimplemented feature than a detection bug. Happy to relabel.
My use case is legal/regulatory contracts, where hierarchy is driven almost entirely by consistent numbering (PART I → 1. → 1.1 → (a) → (i)). My thought was that numbering patterns could serve as a reliable signal for relative heading level — and on legal documents specifically, numbering is more dependable than font size, since styling is often uniform.
Before proposing anything I found the prior discussions (#386, #1170, #2774) and the docling-hierarchical-pdf package, which already implements numbering-based + style-based + TOC-bookmark inference. I understand the author considered upstreaming it but found integration complex, partly, I gather, because the font/style data needed for inference lives in the ConversionResult rather than the final DoclingDocument. Given all that, i have some question:
- Is in-pipeline PDF heading-level inference something you'd want upstreamed, or is the standalone-package approach the intended long-term answer?
- If upstream is welcome, would you prefer a contribution that builds on the existing package's approach, or a fresh, narrower numbering-based step? I'm specifically interested in the numbering-driven path, which is robust for the legal/regulatory document class.
- Any guidance on the right place to thread font/style info from ConversionResult into a level-assignment step would help me scope this correctly.
Happy to take this on if it aligns with your direction. Thanks!
Hi @DanielNg0729 , I’d like to investigate this issue.
My initial plan is to reproduce the heading-level flattening with a small text-based PDF containing patterns like: PART I → 1. → 1.1 → (a) → (i)
Then I’ll check where SectionHeaderItem.level is assigned in the PDF pipeline and explore whether a narrow numbering-based post-processing step could safely infer levels for legal/regulatory documents.
Before opening a PR, I’ll share the reproduction and proposed scope here to confirm whether this direction aligns with the maintainers’ expectations.