Skip to content

feat(markdown): add CellSpanTableSerializer reporting per-cell spans - #1069

Open
peloyl wants to merge 3 commits into
docling-project:mainfrom
peloyl:feat/cell-span-table-serializer
Open

peloyl wants to merge 3 commits into
docling-project:mainfrom
peloyl:feat/cell-span-table-serializer

Conversation

@peloyl

@peloyl peloyl commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #738.

Summary

Markdown table serialization loses which cell each rendered value came from: the result carries a single Span for the whole TableItem, and the per-cell information each TableCell holds (bbox, grid offsets) is dropped because the table is rendered as a flat pipe table.

This implements the design proposed in #738: a CellSpanTableSerializer (a MarkdownTableSerializer subclass) that renders byte-identical text but returns a MarkdownTableSerResult whose cell_spans map every rendered cell occurrence back to its cell:

class TableCellSpan(BaseModel):
    charspan: tuple[int, int]  # range of the cell's rendered text, padding stripped
    cell: AnyTableCell         # the cell itself (RichTableCell refs preserved)

class MarkdownTableSerResult(SerializationResult):
    cell_spans: list[TableCellSpan] = []

Usage: MarkdownDocSerializer(doc=doc, table_serializer=CellSpanTableSerializer()).

Implementation notes

  • The ranges are recovered by parsing the rendered table in the result text — anchored on the separator row, with the header line directly above it and the contiguous pipe lines below it — and mapping segments positionally to grid slots. Duplicate values therefore get distinct ranges by construction, never by text search.
  • Header slots are mapped by replaying _flatten_header_rows' dedup/join per column and locating each part sequentially in the rendered header line, so each part is attributed to the header cell it came from. A header cell repeated over stacked header rows (deduplicated by the flattening) shares the single flattened occurrence.
  • One span per rendered occurrence: a body cell spanning several grid slots yields one entry per covered slot. Empty body cells get a zero-width range. If the rendered table does not structurally match the grid (row/column counts), cell_spans is left empty rather than holding wrong ranges.
  • Passthrough fix (required for the result subclass to survive document-level serialization): DocSerializer.serialize() now returns a single part as-is at both of its create_ser_result() wrap points, and MarkdownDocSerializer.serialize_doc() does the same for a single part when no page breaks are required. The serialize_doc() passthrough goes slightly beyond the item-level fix because the issue's repro goes through MarkdownDocSerializer on a whole document; without it the subclass was still flattened away on that path. Multi-part behavior is byte-identical everywhere, and other formats' serialize_doc() implementations are untouched.

Testing

  • New tests in tests/test_serialization.py (9): the issue's repro end-to-end (result subclass survives, 6 cell spans, distinct ranges for the duplicated 3600, text identical to the default serializer), stacked header rows, stacked-header deduplication, row- and column-spanning body cells, empty cells, compact_tables=True, item-level serialization, and a multi-part document (plain result type, unchanged text).
  • Serializer suites (9 files): 517 passed, 1 xfailed.
  • Full test suite: 1971 passed, 12 skipped, 53 xfailed, 1 failed — tests/test_utils.py::test_connect_time_validation_blocks_dns_rebinding, which fails identically on unmodified main (verified via stash; it depends on sandbox DNS behavior) and is unrelated to this change.
  • ruff check / ruff format clean on the changed files; ty check on the changed serializer files reports the same 7 pre-existing warnings as unmodified main, none in the new code.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 2 protections blocking · waiting on 👀 reviews

Protection Waiting on
🔴 Require two reviewer for test updates 👀 reviews
🟢 Enforce conventional commit —

🔴 Require two reviewer for test updates

Waiting for

  • #approved-reviews-by >= 2
This rule is failing.

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

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)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added enhancement New feature or request serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline labels Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit a7cd4d575d. The analysis read the diff only. It did not run code from this pull request.

Warning

The AI analysis did not finish. Only the deterministic checks are shown.

Topic labels added: enhancement, serialization.

CI safety: not assessed.

Possibly related pull requests

PR State Signal Assessment
#1039 open changes the same code not assessed
#1004 open changes the same code not assessed

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

This branch has not been deployed

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

Labels

enhancement New feature or request serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table serialization loses which cell each value came from

1 participant