Repository navigation
fix: drop the UTF-8 BOM when decoding webvtt, markdown and asciidoc - #4109
Conversation
Signed-off-by: aniketwaghh <aniketwaghh@users.noreply.github.com>
|
✅ DCO Check Passed Thanks @aniketwaghh, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ebarkhordar
left a comment
There was a problem hiding this comment.
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>
|
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: 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. |
|
Thanks, that matches what I measured. The byte level strip is the right call on the stream branch specifically, since |
The webvtt, markdown and asciidoc backends decoded their input as plain
utf-8, so a leadingUTF-8 BOM stayed in the first line. This decodes
utf-8-siginstead, in both theBytesIOandthe
Pathbranch of each backend.utf-8-sigis identical toutf-8when no BOM is present, andit 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
WEBVTTstring, butverify_signature()requires the content to start withWEBVTT, sois_valid()was False. Markdown and asciidoc failed silently:\ufeff# Titleand\ufeff= Titleare not headings, so the document title was demoted to body text and the BOMreached the exported output.
InputFormat.MDalso covers.txt/.qmd/.rmd.Before, on
mainat 2.123.1:After, same script:
One regression test per backend, each covering the stream and the file-path branch, and each built
in memory or under
tmp_pathso nothing lands intests/data. Reverting only the three backendfiles makes all three fail:
Across
test_backend_{vtt,markdown,asciidoc,csv,html,epub,boxnote,doclang,docling_json,email}.py,test_input_doc.pyandtest_invalid_input.py: 152 passed, 1 skipped, 4 warnings before,155 passed, 1 skipped, 4 warnings after.
make validatepasses on the changeset. No groundtruthfiles 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: