Skip to content

fix(csv): drop the UTF-8 BOM instead of keeping it in the first cell - #4098

Merged
cau-git merged 1 commit into
docling-project:mainfrom
aniketwaghh:fix-csv-utf8-bom
Aug 28, 2026
Merged

cau-git merged 1 commit into
docling-project:mainfrom
aniketwaghh:fix-csv-utf8-bom

Conversation

@aniketwaghh

@aniketwaghh aniketwaghh commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

CsvDocumentBackend decoded with plain utf-8, so a leading BOM ended up inside the first cell of the header row. Excel and Google Sheets both write one when exporting "CSV UTF-8".

Decoding with utf-8-sig strips it when present and behaves exactly like utf-8 when it isn't, so files without a BOM are unaffected. I checked that across comma, semicolon and single-column inputs. html_backend.py already strips \ufeff as noise, so this just brings CSV in line.

Issue resolved by this Pull Request:
Resolves #4097

Before and after

data = "\ufeffName,Age\nAlice,30\n".encode("utf-8")  # what Excel writes
doc = converter.convert(DocumentStream(name="bom.csv", stream=BytesIO(data))).document
doc.tables[0].data.table_cells[0].text

before '\ufeffName', after 'Name'. In the exported markdown that was:

| \ufeffName   |   Age |
|--------|-------|
| Alice  |    30 |

Since the character is invisible, the symptom users actually hit is a header match quietly failing on the first column.

Scope

Only CSV. asciidoc_backend, boxnote_backend, webvtt_backend and md_backend decode the same way, but there a BOM just joins the first text run rather than corrupting a structured field, so I left them out rather than widen the diff. Happy to follow up separately if you want them covered.

Testing

test_utf8_bom_is_not_part_of_the_first_cell covers both the stream and the file path, since each decodes separately. It fails on main and passes here. Written against an in-memory DocumentStream and tmp_path rather than a new fixture, so it doesn't touch tests/data.

uv run pytest tests/test_backend_csv.py     ->  5 passed
uv run pytest tests/ -k "csv or markdown or html or msword or asciidoc or webvtt"
                                            ->  149 passed, 2 skipped

The three existing CSV groundtruth tests pass unchanged, so no exported output moved. make validate is clean.

Checklist:

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

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Aug 28, 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)(?:\(.+\))?(!)?:

CsvDocumentBackend decoded with plain utf-8, so a leading BOM survived into
the first cell of the header row. Excel and Google Sheets both write one when
exporting "CSV UTF-8", so the header came back as "Name" rather than
"Name" and reached the exported document.

The character is invisible, so the practical symptom is that anything matching
on a header silently misses the first column with no visible cause.

Decode with utf-8-sig, which strips a BOM when present and is equivalent to
utf-8 when it is not, so files without one are unaffected. html_backend
already strips  as noise, so this brings CSV in line.

The same plain-utf-8 decode exists in asciidoc, boxnote, webvtt and md, where
a BOM only joins the first text run rather than corrupting a structured field,
so those are left alone here.

Fixes docling-project#4097

Signed-off-by: Aniket Wagh <waniket48@gmail.com>
@codecov

codecov Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@cau-git
cau-git merged commit 0865b9a into docling-project:main Aug 28, 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.

csv: UTF-8 BOM ends up inside the first header cell

2 participants