docling icon indicating copy to clipboard operation
docling copied to clipboard

Since 2.143.1 (#5022), PDF tables lose their cell text with PyPdfiumDocumentBackend + OCR: native words are replaced by OCR lines in table tokens

Open KulovacNedim opened this issue 3 weeks ago • 3 comments

Bug

With the pypdfium2 backend and OCR on, some born-digital PDF tables lose their text from docling 2.143.1 on. Cells come out empty or hold OCR noise, and the tables collapse (23×3 → 1×1, 20×1 → 4×1). The same pages with docling 2.143.0 are correct. This is still present in 2.148.0.

Steps to reproduce

  1. Get the public NIST Cybersecurity Framework 2.0 PDF: https://nvlpubs.nist.gov/nistpubs/CSWP/NIST.CSWP.29.pdf (sha256 3c31f46fee98cac0c4323453e5109291a213b4de7fef8c058af9bf67f717433c).
  2. Keep only pages 4 and 20: the table of contents and "Table 1. CSF 2.0 Core Function and Category names and identifiers". Any page splitter works. We used PyMuPDF doc.select([3, 19]); doc.save(out, garbage=4, deflate=True), which gives a 2-page file. The full PDF shows the same result on those pages.
  3. Run:
import sys
from importlib.metadata import version

from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions, TesseractCliOcrOptions
from docling.document_converter import DocumentConverter, PdfFormatOption

opts = PdfPipelineOptions(ocr_options=TesseractCliOcrOptions(lang=["eng"]))  # do_ocr / do_table_structure default True
conv = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=opts, backend=PyPdfiumDocumentBackend)}
)
doc = conv.convert(sys.argv[1]).document
print("docling", version("docling-slim"))
for i, t in enumerate(doc.tables):
    cells = t.data.table_cells
    print(
        f"table {i}: page={t.prov[0].page_no} rows={t.data.num_rows} cols={t.data.num_cols} "
        f"non_empty_cells={sum(1 for c in cells if c.text.strip())} "
        f"alnum_chars={sum(ch.isalnum() for c in cells for ch in c.text)}"
    )
    print(t.export_to_markdown(doc=doc)[:300])

Expected (2.143.0)

table 0: page=1 rows=20 cols=1 non_empty_cells=20 alnum_chars=1010
| Table of Contents ...
table 1: page=2 rows=23 cols=3 non_empty_cells=53 alnum_chars=667
| Function ... | Category ... | Category Identifier |
| Gover...

Actual (2.143.1, 2.144.0, 2.148.0, all the same)

table 0: page=1 rows=4 cols=1 non_empty_cells=4 alnum_chars=477
| 3.1. CSF Profiles... ec eesesecscseseesssscscsesesscseseseesescseseseesesssesssessesssseeeeeseaseceasesassceeeesesseseeseesasesseeesesaesseees 6 |
table 1: page=2 rows=1 cols=1 non_empty_cells=1 alnum_chars=0
| | =| |

Table 1 loses all 667 characters of cell text. The table of contents keeps 4 of its 20 rows, and its native text is replaced by Tesseract's reading of the dot leaders.

Which options trigger it

Same 2-page file. Total table alnum characters (table 0 + table 1) on 2.143.0 → 2.144.0:

backend OCR other 2.143.0 2.144.0
default (docling-parse) auto (RapidOCR, torch) defaults 1200 1320 (OK)
default Tesseract CLI 1200 1639 (OK)
default off 1200 1200 (OK)
pypdfium2 Tesseract CLI 1677 477 (bug)
pypdfium2 Tesseract CLI images_scale=1.5 1677 477 (bug)
pypdfium2 Tesseract CLI TableFormerMode.FAST (not run) 477 (bug)
pypdfium2 Tesseract CLI replace_broken_text_layer=True n/a 477 (bug)
pypdfium2 auto (RapidOCR) 1320 787 (bug on table 0: 4×1, 120 chars)
pypdfium2 off (not run) 1200 (OK)
pypdfium2 Tesseract CLI do_cell_matching=False (not run) 1199 (OK-ish: 20×1, 16 non-empty cells)
  • Trigger: PyPdfiumDocumentBackend together with any OCR engine. do_cell_matching=False or do_ocr=False avoids it.
  • TableFormerMode and images_scale make no difference. OcrOptions.replace_broken_text_layer (new in 2.144, #4654) is off by default, and setting it does not change the result.
  • With the default docling-parse backend, 2.144 is fine on this file (it is even slightly better on the table of contents).

Suspected change

docling/models/stages/table_structure/table_structure_model.py, from PR #5022 "fix(table): keep OCR text in table tokens in every OCR mode" (commits ce6690b, affa54c, 103edef). It is in v2.143.1 and later, and not in v2.143.0.

  • The table tokens now come from page.parsed_page (before: page._backend.get_segmented_page()) and are passed through _words_and_ocr_cells(word_cells, table_cluster.cells).
  • That function drops every native word that sits under an OCR line kept in the table cluster, and then adds the OCR line as a single token.
  • On page 2 of the reproducer with the pypdfium2 backend, the OCR merge keeps 2 OCR cells in the table cluster (55 native cells). With docling-parse it keeps 0 OCR cells (53 native cells). Those OCR cells cover the native words of the table, so TableFormer's cell matcher gets a few large OCR-line tokens instead of the words. Almost no cells are filled, and post-processing collapses the grid to 1×1.
  • In a local experiment, replacing _words_and_ocr_cells(...) with the native word cells only (the 2.143.0 behavior) restores the tables exactly. A variant that keeps native words and adds OCR lines only where no native word sits did not restore them. The OCR lines inside the cluster are themselves what breaks the matching.

We understand the goal of #5022 (scanned forms with a few native words should not lose their OCR text), and we see that gain on scanned tables. The problem is born-digital tables where a few OCR cells survive the merge.

Possibly related

  • #4654 "feat(ocr): replace broken PDF text layers with OCR" (in 2.144.0). We first suspected it, but it does not touch table_structure_model.py, and its option replace_broken_text_layer does not change the result. Probably unrelated.
  • #5136 (open) "Since docling-parse 7.21.0, separate table columns are read as one text line, so docling leaves the table cells empty". The symptom is similar (empty cells), but the cause is different. Our failing and passing runs use the same docling-parse 7.24.2, our trigger is the pypdfium2 backend, and docling 2.143.0 with docling-parse 7.24.2 is correct.
  • #5138 (open, fix for #5136; head 0b46704d, based on 2.145.0) only changes docling_parse_backend.py. Installed on top of the 2.144 environment, it gives the same 477 / 1×1 result with pypdfium2 + Tesseract and the same correct result with the default backend. It does not fix this case.
  • #4592 "fix(tables): keep table text when TableFormer predicts a runaway structure" (merged 11-05) is in 2.148.0, which still fails.

Environment

Docker python:3.12-slim-bookworm (Debian GNU/Linux 12), Python 3.12.16, CPU only, run with --network none (models downloaded at build time with docling-tools models download). Tesseract 5.3.0 (Debian tesseract-ocr, eng). Installed with pip install docling==<version> plus the PyTorch CPU index.

image docling / docling-slim docling-core docling-ibm-models docling-parse torch others
A 2.143.0 2.106.0 4.0.4 7.24.2 2.15.0+cpu numpy 2.5.4, pillow 13.0.0, pypdfium2 5.15.0, transformers 5.22.2, rapidocr 3.10.0
B 2.144.0 2.106.0 4.0.4 7.24.2 2.15.0+cpu same
C 2.143.1 2.106.0 4.0.4 7.24.2 2.15.0+cpu same (B with docling swapped)
D 2.148.0 2.106.0 4.0.4 7.24.2 2.15.0+cpu same (B with docling swapped)

We first saw the regression in a document-ingestion pipeline that uses the same options (pypdfium2 backend, Tesseract CLI OCR) on docling 2.145.0. There, swapping only docling gave the same split (2.143.0 good, 2.144.0 bad) on torch 2.2.2 / numpy 1.26 as well as on torch 2.14.1 / numpy 2.4, so torch and numpy are not involved.

Workaround

Pin docling==2.143.0 (not 2.143.1). Or use the default docling-parse backend, if the pypdfium2 backend is not needed.

KulovacNedim avatar Nov 07 '26 20:11 KulovacNedim

Hi @cau-git, I'd like to take this one. I'll check what changed around #5022 for the pypdfium path and open a PR with a regression test.

its-amann avatar Nov 07 '26 20:11 its-amann

Classification labels added: bug, priority:medium, pdf.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 16 '26 15:11 github-actions[bot]

Hi! I noticed this issue is still open and has no linked PR.

I'm interested in contributing to the regression investigation, particularly the interaction between OCR cells, native PDF text cells, and TableFormer.

I saw that another contributor had volunteered to investigate this. Is there an opportunity for me to help with regression tests or additional edge cases without duplicating their work?

Thank you!

Saloni248694 avatar Nov 16 '26 15:11 Saloni248694