Skip to content

fix: drop the UTF-8 BOM when decoding webvtt, markdown and asciidoc - #4109

Merged
cau-git merged 2 commits into
docling-project:mainfrom
aniketwaghh:fix-utf8-bom-text-backends
Aug 31, 2026
Merged

cau-git merged 2 commits into
docling-project:mainfrom
aniketwaghh:fix-utf8-bom-text-backends

Conversation

@aniketwaghh

Copy link
Copy Markdown
Contributor

The webvtt, markdown and asciidoc backends decoded their input as plain utf-8, so a leading
UTF-8 BOM stayed in the first line. This decodes utf-8-sig instead, in both the BytesIO and
the Path branch of each backend. utf-8-sig is identical to utf-8 when no BOM is present, and
it is what #4098 already did for csv.

WebVTT failed outright: the file body grammar in https://www.w3.org/TR/webvtt1 allows an optional
U+FEFF before the WEBVTT string, but verify_signature() requires the content to start with
WEBVTT, so is_valid() was False. Markdown and asciidoc failed silently: \ufeff# Title and
\ufeff= Title are not headings, so the document title was demoted to body text and the BOM
reached the exported output. InputFormat.MD also covers .txt/.qmd/.rmd.

Before, on main at 2.123.1:

sample.vtt   no BOM  [('TextItem', 'Hello there')]
sample.vtt   BOM     ConversionError: Conversion failed for: sample.vtt with status: failure. Errors: The document backend could not parse the input.
sample.md    no BOM  [('TitleItem', 'Title'), ('TextItem', 'Some body text.')]
sample.md    BOM     [('TextItem', '\ufeff# Title'), ('TextItem', 'Some body text.')]
sample.adoc  no BOM  [('TitleItem', 'Document Title'), ('TextItem', 'Some body text.')]
sample.adoc  BOM     [('TextItem', '\ufeff= Document Title'), ('TextItem', 'Some body text.')]

After, same script:

sample.vtt   no BOM  [('TextItem', 'Hello there')]
sample.vtt   BOM     [('TextItem', 'Hello there')]
sample.md    no BOM  [('TitleItem', 'Title'), ('TextItem', 'Some body text.')]
sample.md    BOM     [('TitleItem', 'Title'), ('TextItem', 'Some body text.')]
sample.adoc  no BOM  [('TitleItem', 'Document Title'), ('TextItem', 'Some body text.')]
sample.adoc  BOM     [('TitleItem', 'Document Title'), ('TextItem', 'Some body text.')]

One regression test per backend, each covering the stream and the file-path branch, and each built
in memory or under tmp_path so nothing lands in tests/data. Reverting only the three backend
files makes all three fail:

FAILED tests/test_backend_vtt.py::test_utf8_bom_does_not_invalidate_the_signature
FAILED tests/test_backend_markdown.py::test_utf8_bom_does_not_hide_the_first_heading
FAILED tests/test_backend_asciidoc.py::test_utf8_bom_does_not_hide_the_document_title

Across test_backend_{vtt,markdown,asciidoc,csv,html,epub,boxnote,doclang,docling_json,email}.py,
test_input_doc.py and test_invalid_input.py: 152 passed, 1 skipped, 4 warnings before,
155 passed, 1 skipped, 4 warnings after. make validate passes on the changeset. No groundtruth
files changed.

Scope: html and latex are unaffected, and the docling-json and boxnote backends do reject a
BOM-prefixed file, but that only happens for JSON written by something other than docling, so I
left them out rather than widen this.

Issue resolved by this Pull Request:
Resolves #4108

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

Signed-off-by: aniketwaghh <aniketwaghh@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

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)(?:\(.+\))?(!)?:

@codecov

codecov Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ebarkhordar ebarkhordar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran this in a clean python:3.11-slim container at 8b0f4e7. The three new tests pass on the PR and all three fail on d2283ad with the same tests copied in, so they are load bearing.

Two other backends still decode a whole user supplied file as plain utf-8, and a BOM there does not demote a heading, it fails the conversion outright:

docling/backend/boxnote_backend.py:59 and :61, where the BOM reaches json.loads.

docling/backend/json/docling_json_backend.py:46 and :49, same shape for model_validate_json.

Measured at your head, through DocumentConverter, using tests/data/boxnote/sources/sample.boxnote and a docling json exported from it:

boxnote       stream no BOM    -> OK   texts=20
boxnote       path   no BOM    -> OK   texts=20
boxnote       stream with BOM  -> ConversionError: Could not load Box Note document
boxnote       path   with BOM  -> ConversionError: Could not load Box Note document
docling json  stream no BOM    -> OK   texts=20
docling json  path   no BOM    -> OK   texts=20
docling json  stream with BOM  -> ConversionError: The document backend could not parse it
docling json  path   with BOM  -> ConversionError: The document backend could not parse it

boxnote is the same one line change you already made three times, utf-8-sig on both branches. The json one is not identical: its BytesIO branch hands raw bytes straight to model_validate_json, so it needs the BOM stripped from the bytes rather than an encoding swap.

I also checked xml/doclang_backend.py, which reads utf-8 the same way. It is fine, the docling-core deserializer tolerates a leading BOM, so nothing to do there.

Whether those belong here or in a follow up is your call.

Signed-off-by: aniketwaghh <aniketwaghh@users.noreply.github.com>
@aniketwaghh

aniketwaghh commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Both reproduce here, so I've added them to this PR rather than a follow-up, since it's the same defect.

boxnote is the same one-line change on both branches. For the json one I did it the way you described: utf-8-sig on the path branch, and codecs.BOM_UTF8 stripped off the bytes on the stream branch, since that one never decodes.

I checked the other two whole-file utf-8 readers rather than take the xml result on trust, and doclang is fine as you said: 498 texts, identical with and without a BOM, on both branches. uspto is fine too, 142 texts either way. So boxnote and docling json were the last of them.

With only the backend change reverted both new tests fail, and they pass with it in.

@ebarkhordar

Copy link
Copy Markdown
Contributor

Thanks, that matches what I measured. The byte level strip is the right call on the stream branch specifically, since model_validate_json is handed the raw bytes there and nothing decodes them first.

@cau-git
cau-git merged commit 4e34e7b into docling-project:main Aug 31, 2026
26 checks passed
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.

webvtt/md/asciidoc: a leading UTF-8 BOM breaks WebVTT validation and hides the first heading

3 participants