HTML backend merges list items and table cells when optional end tags are omitted
Bug
HTML lets you leave out the closing tag on li, td, th, tr, dt and dd, and plenty of hand-written and older pages do that. The HTML backend parses with html.parser (docling/backend/html_backend.py, BeautifulSoup(raw, "html.parser")), which doesn't apply the implied-end-tag rules. So each new <li> ends up nested inside the previous one, and the whole list comes out as a single item. Same thing for table cells and rows, and for definition lists.
| input | output now | expected |
|---|---|---|
<ul><li>alpha<li>beta<li>gamma</ul> |
- alpha beta gamma |
3 items |
<ol><li>one<li>two</ol> |
1. one two |
2 items |
<table><tr><td>a<td>b<tr><td>c<td>d</table> |
one cell a b c d |
2x2 table |
<table><tr><th>H1<th>H2<tr><td>1<td>2</table> |
one header cell | header row + 1 data row |
<dl><dt>A<dd>B<dt>C<dd>D</dl> |
- **A B C D** |
two term/definition pairs |
For the "expected" column I patched the backend to use the lxml parser instead, and it produces exactly that (3 list items, a proper 2x2 table, two dt/dd pairs). So the walker itself is fine, it's only the parse tree.
On longer pages it goes past wrong output: a list with 5000 <li> and no </li> (or a table with 1000 unclosed rows) nests so deep that convert() fails with RecursionError: maximum recursion depth exceeded.
Steps to reproduce
from io import BytesIO
from docling.datamodel.base_models import DocumentStream
from docling.document_converter import DocumentConverter
html = b"<html><body><ul><li>alpha<li>beta<li>gamma</ul></body></html>"
res = DocumentConverter().convert(DocumentStream(name="x.html", stream=BytesIO(html)))
print(res.document.export_to_markdown())
# - alpha beta gamma
Docling version
main at b38ed49 (2.133.0), same on the 2.133.0 release
Python version
Python 3.14.7
I'd like to fix this. Two options I can see:
- Keep
html.parserand add a small pass right after parsing that moves a misplacedli/dt/dd/td/th/trout of a parent that can't contain it (anlidirectly inside anli, atdinside atd, and so on). Properly nested lists aren't touched since theirliparent is aul/ol. I have a rough version of this that passes the existing HTML, MHTML, EPUB and JATS backend tests. - Switch the parser to
lxml. That fixes it at the source, butlxmlisn't in theformat-htmlextra right now, and changing the parser might shift whitespace/structure in other ground truth files.
I'd go with 1 unless you'd rather switch parsers. Happy to open a PR either way.
Ran the unclosed list and table in our plant. The file was read. alpha, beta, and gamma each came back once, and so did a, b, c, and d. Nothing was glued into one line. The list is nested, beta under alpha and gamma under beta, because the file never closes the li that is still open. That is your tree, printed as indentation instead of one line. The cells are four lines, not a 2 by 2 table. Your lxml result, three siblings and a real table, is the shape that matches the page. We have not added that repair.
https://kbmill.com/