Repository navigation
feat(api): expose PDF heading hierarchy options in ConvertDocumentOptions #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edeandrea
merged 3 commits into
docling-project:main
from
edeandrea:feature/heading-hierarchy-options
Oct 22, 2026
+353
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...i/src/main/java/ai/docling/serve/api/convert/request/options/HeadingHierarchyOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package ai.docling.serve.api.convert.request.options; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.annotation.JsonSetter; | ||
| import com.fasterxml.jackson.annotation.Nulls; | ||
|
|
||
| /** | ||
| * Options for configuring PDF heading hierarchy inference. | ||
| * This class mirrors the Python {@code HeadingHierarchyOptions} from docling-core and is | ||
| * used to reconstruct a true heading hierarchy (H1–H6) from PDF bookmarks, outline numbering, | ||
| * and visual style instead of emitting all detected section headers flat at level 1. | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
| @lombok.extern.jackson.Jacksonized | ||
| @lombok.Builder(toBuilder = true) | ||
| @lombok.Getter | ||
| @lombok.ToString | ||
| public class HeadingHierarchyOptions { | ||
|
|
||
| /** | ||
| * Use the PDF outline / table of contents as the authoritative signal for heading levels. | ||
| * | ||
| * @param useBookmarks true to use bookmarks, or null for the default (true) | ||
| * @return the use-bookmarks flag | ||
| */ | ||
| @JsonProperty("use_bookmarks") | ||
| @Nullable | ||
| private Boolean useBookmarks; | ||
|
|
||
| /** | ||
| * Use the leading numbering marker of the heading text (e.g., "1.", "1.1.2", "(a)") | ||
| * as a signal for heading levels. | ||
| * | ||
| * @param useNumbering true to use numbering, or null for the default (true) | ||
| * @return the use-numbering flag | ||
| */ | ||
| @JsonProperty("use_numbering") | ||
| @Nullable | ||
| private Boolean useNumbering; | ||
|
|
||
| /** | ||
| * Fall back to the heading's visual style (font size, weight, slant, letter case) when | ||
| * neither bookmarks nor numbering provide a level. Requires {@code generate_parsed_pages=True} | ||
| * in the pipeline options. | ||
| * | ||
| * @param useStyle true to use visual style, or null for the default (true) | ||
| * @return the use-style flag | ||
| */ | ||
| @JsonProperty("use_style") | ||
| @Nullable | ||
| private Boolean useStyle; | ||
|
|
||
| /** | ||
| * Refine the style fallback with font weight, slant and all-caps detection. Ignored when | ||
| * {@link #getUseStyle()} is off. | ||
| * | ||
| * @param useFontStyle true to use font style, or null for the default (true) | ||
| * @return the use-font-style flag | ||
| */ | ||
| @JsonProperty("use_font_style") | ||
| @Nullable | ||
| private Boolean useFontStyle; | ||
|
|
||
| /** | ||
| * Relative difference below which two font sizes count as one. Higher values merge more | ||
| * sizes into a single level. | ||
| * | ||
| * @param styleSizeTolerance the tolerance value, or null for the default (0.05) | ||
| * @return the style-size-tolerance value | ||
| */ | ||
| @JsonProperty("style_size_tolerance") | ||
| @Nullable | ||
| private Double styleSizeTolerance; | ||
|
|
||
| /** | ||
| * Override the numbering-scheme precedence order (highest level first). When null, the | ||
| * built-in ordering is used. | ||
| * | ||
| * @param numberingSchemes the ordered list of scheme names, or null for the default | ||
| * @return the numbering-schemes list | ||
| */ | ||
| @JsonProperty("numbering_schemes") | ||
| @JsonSetter(nulls = Nulls.AS_EMPTY) | ||
| @lombok.Singular("numberingScheme") | ||
| private List<String> numberingSchemes; | ||
|
|
||
| /** | ||
| * Deepest heading level assigned by the inference stage; headings deeper than this are | ||
| * clamped to this level. | ||
| * | ||
| * @param maxLevel the maximum level, or null for the default (6) | ||
| * @return the max-level value | ||
| */ | ||
| @JsonProperty("max_level") | ||
| @Nullable | ||
| private Integer maxLevel; | ||
|
|
||
| /** | ||
| * Minimum title similarity (0–1) for a bookmark to claim a heading. Higher values are | ||
| * stricter. | ||
| * | ||
| * @param bookmarkMatchThreshold the threshold, or null for the default (0.8) | ||
| * @return the bookmark-match-threshold value | ||
| */ | ||
| @JsonProperty("bookmark_match_threshold") | ||
| @Nullable | ||
| private Double bookmarkMatchThreshold; | ||
|
|
||
| /** | ||
| * Builder for creating {@link HeadingHierarchyOptions} instances. | ||
| */ | ||
| public static class Builder { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
.../test/java/ai/docling/serve/api/convert/request/options/HeadingHierarchyOptionsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package ai.docling.serve.api.convert.request.options; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Unit tests for {@link HeadingHierarchyOptions}. | ||
| */ | ||
| class HeadingHierarchyOptionsTests { | ||
|
|
||
| @Test | ||
| void createWithAllFields() { | ||
| var options = HeadingHierarchyOptions.builder() | ||
| .useBookmarks(true) | ||
| .useNumbering(false) | ||
| .useStyle(true) | ||
| .useFontStyle(true) | ||
| .styleSizeTolerance(0.1) | ||
| .numberingScheme("chapter") | ||
| .numberingScheme("section") | ||
| .maxLevel(4) | ||
| .bookmarkMatchThreshold(0.9) | ||
| .build(); | ||
|
|
||
| assertThat(options) | ||
| .extracting( | ||
| HeadingHierarchyOptions::getUseBookmarks, HeadingHierarchyOptions::getUseNumbering, HeadingHierarchyOptions::getUseStyle, HeadingHierarchyOptions::getUseFontStyle, HeadingHierarchyOptions::getStyleSizeTolerance, HeadingHierarchyOptions::getNumberingSchemes, HeadingHierarchyOptions::getMaxLevel, HeadingHierarchyOptions::getBookmarkMatchThreshold) | ||
| .containsExactly(true, false, true, true, 0.1, List.of("chapter", "section"), 4, 0.9); | ||
| } | ||
|
|
||
| @Test | ||
| void createWithMinimalFields() { | ||
| var options = HeadingHierarchyOptions.builder() | ||
| .useBookmarks(false) | ||
| .build(); | ||
|
|
||
| assertThat(options.getUseBookmarks()).isFalse(); | ||
| assertThat(options.getUseNumbering()).isNull(); | ||
| assertThat(options.getUseStyle()).isNull(); | ||
| assertThat(options.getUseFontStyle()).isNull(); | ||
| assertThat(options.getStyleSizeTolerance()).isNull(); | ||
| assertThat(options.getNumberingSchemes()).isEmpty(); | ||
| assertThat(options.getMaxLevel()).isNull(); | ||
| assertThat(options.getBookmarkMatchThreshold()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void createWithDefaultValues() { | ||
| var options = HeadingHierarchyOptions.builder().build(); | ||
|
|
||
| assertThat(options.getUseBookmarks()).isNull(); | ||
| assertThat(options.getUseNumbering()).isNull(); | ||
| assertThat(options.getUseStyle()).isNull(); | ||
| assertThat(options.getUseFontStyle()).isNull(); | ||
| assertThat(options.getStyleSizeTolerance()).isNull(); | ||
| assertThat(options.getNumberingSchemes()).isEmpty(); | ||
| assertThat(options.getMaxLevel()).isNull(); | ||
| assertThat(options.getBookmarkMatchThreshold()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void headingHierarchyOptionsIsImmutable() { | ||
| var options = HeadingHierarchyOptions.builder() | ||
| .styleSizeTolerance(0.05) | ||
| .numberingScheme("chapter") | ||
| .build(); | ||
|
|
||
| assertThat(options.getNumberingSchemes()).hasSize(1); | ||
| assertThat(options.getNumberingSchemes()).containsExactly("chapter"); | ||
|
|
||
| // Mutating the returned list should not affect the options | ||
| assertThatThrownBy(() -> options.getNumberingSchemes().add("section")) | ||
| .isInstanceOf(UnsupportedOperationException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void toBuilderReturnsEquivalentInstance() { | ||
| var original = HeadingHierarchyOptions.builder() | ||
| .useBookmarks(false) | ||
| .maxLevel(3) | ||
| .numberingScheme("article") | ||
| .numberingScheme("section") | ||
| .bookmarkMatchThreshold(0.85) | ||
| .build(); | ||
|
|
||
| var rebuilt = original.toBuilder().build(); | ||
|
|
||
| assertThat(rebuilt.getUseBookmarks()).isEqualTo(original.getUseBookmarks()); | ||
| assertThat(rebuilt.getUseNumbering()).isEqualTo(original.getUseNumbering()); | ||
| assertThat(rebuilt.getUseStyle()).isEqualTo(original.getUseStyle()); | ||
| assertThat(rebuilt.getUseFontStyle()).isEqualTo(original.getUseFontStyle()); | ||
| assertThat(rebuilt.getStyleSizeTolerance()).isEqualTo(original.getStyleSizeTolerance()); | ||
| assertThat(rebuilt.getNumberingSchemes()).containsExactlyElementsOf(original.getNumberingSchemes()); | ||
| assertThat(rebuilt.getMaxLevel()).isEqualTo(original.getMaxLevel()); | ||
| assertThat(rebuilt.getBookmarkMatchThreshold()).isEqualTo(original.getBookmarkMatchThreshold()); | ||
| } | ||
|
|
||
| @Test | ||
| void nonNullFieldsOnlyArentOmittedFromJson() { | ||
| var options = HeadingHierarchyOptions.builder() | ||
| .useBookmarks(true) | ||
| .build(); | ||
|
|
||
| assertThat(options.getUseBookmarks()).isTrue(); | ||
| assertThat(options.getUseNumbering()).isNull(); | ||
| assertThat(options.getUseStyle()).isNull(); | ||
| assertThat(options.getUseFontStyle()).isNull(); | ||
| assertThat(options.getStyleSizeTolerance()).isNull(); | ||
| assertThat(options.getNumberingSchemes()).isEmpty(); | ||
| assertThat(options.getMaxLevel()).isNull(); | ||
| assertThat(options.getBookmarkMatchThreshold()).isNull(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.