Skip to content

fix(doctags): tolerate inverted bbox in DocumentToken.get_location#628

Open
assinscreedFC wants to merge 1 commit into
docling-project:mainfrom
assinscreedFC:fix/doctags-get-location-redundant-bbox-assert
Open

fix(doctags): tolerate inverted bbox in DocumentToken.get_location#628
assinscreedFC wants to merge 1 commit into
docling-project:mainfrom
assinscreedFC:fix/doctags-get-location-redundant-bbox-assert

Conversation

@assinscreedFC

Copy link
Copy Markdown
Contributor

Summary

DocumentToken.get_location() asserted bbox[0] <= bbox[2] and bbox[1] <= bbox[3], but the min() / max() calls right below already emit correctly-ordered location tokens regardless of input ordering. The asserts were redundant with that normalization, yet still crashed export_to_doctags(add_location=True) on near-degenerate elements (e.g. a near-zero-height layout-model artifact whose regressed bbox is slightly inverted):

File ".../docling_core/types/doc/tokens.py", line 307, in get_location
    assert bbox[1] <= bbox[3], f"bbox[1]<=bbox[3] => {bbox[1]}<={bbox[3]}"
AssertionError: bbox[1]<=bbox[3] => 633.39<=625.68

The probability of hitting at least one such element grows with page count, so it bites long documents in particular.

Fix

Drop the two redundant asserts in both docling_core/types/doc/tokens.py and docling_core/types/legacy_doc/tokens.py. The existing min() / max() logic already produces correctly-ordered output, so inverted input is normalized rather than fatal.

Tests

Added test/test_tokens.py:

  • inverted-y, inverted-x, and legacy get_location all no longer raise and yield the same tokens as their normalized counterparts.

Closes #620

get_location() asserted bbox[0] <= bbox[2] and bbox[1] <= bbox[3], but the
following min()/max() calls already emit correctly-ordered location tokens
regardless of input ordering. The asserts were therefore redundant while
still crashing export_to_doctags(add_location=True) on near-degenerate
elements (e.g. a near-zero-height layout-model artifact whose regressed bbox
is slightly inverted). This bites long documents, where the probability of
at least one such element grows with page count.

Drop the redundant asserts in both docling_core/types/doc/tokens.py and
docling_core/types/legacy_doc/tokens.py so the existing min()/max()
normalization handles inverted input.

Fixes docling-project#620

Signed-off-by: ANIS <119749586+assinscreedFC@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

DCO Check Passed

Thanks @assinscreedFC, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 2 protections blocking · waiting on 👀 reviews

Protection Waiting on
🔴 Require two reviewer for test updates 👀 reviews
🟢 Enforce conventional commit

🔴 Require two reviewer for test updates

Waiting for

  • #approved-reviews-by >= 2
This rule is failing.

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@rnevet rnevet left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant bbox assertion in DocumentToken.get_location crashes export_to_doctags on near-degenerate elements

2 participants