Skip to content

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

Closed

Description

@aniketwaghh

Bug

The webvtt, markdown and asciidoc backends decode their input as plain utf-8, so a leading
UTF-8 BOM (U+FEFF) survives into the first line of the source.

For WebVTT this is a hard failure. The file body grammar in https://www.w3.org/TR/webvtt1 lists
"an optional U+FEFF BYTE ORDER MARK (BOM) character" before the WEBVTT string, but
WebVTTFile.verify_signature() requires the content to start with WEBVTT, so is_valid()
returns False and the conversion fails with "The document backend could not parse the input".

For Markdown and AsciiDoc it is silent. The first line becomes \ufeff# Title / \ufeff= Title,
which is no longer a heading, so the document title is demoted to body text and the BOM itself
ends up in the exported text. InputFormat.MD also covers .txt, .qmd and .rmd, which is
where BOM-prefixed files show up most often (PowerShell's Out-File -Encoding utf8 and Notepad's
"UTF-8 with BOM" both write one).

CSV had the same defect and was fixed in #4098 by decoding utf-8-sig. These three are the
remaining text backends where the BOM changes the result. The html backend is unaffected
(BeautifulSoup drops it) and latex is unaffected.

Steps to reproduce

import io

from docling.datamodel.base_models import DocumentStream
from docling.document_converter import DocumentConverter

BOM = "\ufeff"
SOURCES = {
    "sample.vtt": "WEBVTT\n\n00:00:01.000 --> 00:00:05.000\nHello there\n",
    "sample.md": "# Title\n\nSome body text.\n",
    "sample.adoc": "= Document Title\n\nSome body text.\n",
}

converter = DocumentConverter()
for name, text in SOURCES.items():
    for label, payload in (("no BOM", text), ("BOM   ", BOM + text)):
        stream = DocumentStream(name=name, stream=io.BytesIO(payload.encode()))
        try:
            doc = converter.convert(stream, raises_on_error=True).document
            items = [(type(i).__name__, getattr(i, "text", "")) for i, _ in doc.iterate_items()]
            print(f"{name:12} {label}  {items}")
        except Exception as e:
            print(f"{name:12} {label}  {type(e).__name__}: {e}")

Output on main (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.')]

Expected: the BOM row matches the no-BOM row in all three cases.

The same happens when converting a file path instead of a DocumentStream; each backend decodes
the two branches separately.

Docling version

Docling version: 2.123.1
Docling Core version: 2.92.0
Docling IBM Models version: 3.14.0
Docling Parse version: 7.16.0
Python: cpython-312 (3.12.14)
Platform: macOS-26.6.2-arm64-arm-64bit

Python version

Python 3.12.14

Activity

  1. dosubot commented on Aug 29, 2026

    @dosubot

    @aniketwaghh, confirmed — the code on main matches your report exactly.

    • WebVTT: WebVTTDocumentBackend.__init__ decodes with plain "utf-8" [1], and WebVTTFile.verify_signature() in docling-core requires the content to start literally with "WEBVTT" [2]. A leading \ufeff makes content.startswith("WEBVTT") false, so is_valid() returns False and conversion fails.
    • AsciiDoc: AsciiDocBackend.__init__ also decodes with plain "utf-8" on both branches [3], so a BOM survives into self.lines[0] and demotes \ufeff= Title from a heading to body text.
    • Markdown: MarkdownDocumentBackend.__init__ does the same [4], producing \ufeff# Title as body text instead of a TitleItem.

    Your read of #4098 is accurate: that PR switched CsvDocumentBackend to decode with "utf-8-sig" on both the BytesIO and Path branches [5], and its description explicitly scoped itself to CSV only, naming asciidoc_backend, boxnote_backend, webvtt_backend, and md_backend as follow-up candidates using the same decode pattern [6].

    The fix would mirror #4098: swap decode("utf-8") → decode("utf-8-sig") and encoding="utf-8" → encoding="utf-8-sig" in webvtt_backend.py, asciidoc_backend.py, md_backend.py, and likely boxnote_backend.py, for both the stream and file-path branches. For WebVTT specifically, verify_signature and parse operate on self.content after decoding [7], so stripping the BOM at decode time is sufficient — no change needed in docling-core's verify_signature itself.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about docling Share Dosu with your team

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions