Skip to content

feat: revize TripletTableSerializer - #574

Open
wanadzhar913 wants to merge 3 commits into
docling-project:mainfrom
wanadzhar913:feature/revize-triplettableserializer
Open

wanadzhar913 wants to merge 3 commits into
docling-project:mainfrom
wanadzhar913:feature/revize-triplettableserializer

Conversation

@wanadzhar913

@wanadzhar913 wanadzhar913 commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Why

TripletTableSerializer relied on first-row/first-column assumptions, causing triplets with vague semantic meaning when converted to text, and sometimes skipped cells (especially in 0,0).

The solution I came up with is verbose, e.g. row_1, col_1 = Value, but I was of the opinion that it's better to be precise with positioning instead of assuming.

As the code edits tests & test data, please let me know if this is acceptable, since it will affect the main docling package downstream.

Issues

Changes

  • Updated TripletTableSerializer in docling_core/transforms/chunker/hierarchical_chunker.py:
    • metadata-first header detection from table cell metadata (TableCell.row_header / TableCell.column_header)
    • safe fallback to the DataFrame columns when metadata is missing
    • consistent row_i/col_j labeling when headers are absent
    • empty/NaN filtering
  • Improved nested table detection/serialization:
    • handles nested DataFrame cells
    • detects serialized inner-table strings and emits a -> relation (instead of =)
    • recursion depth cap (max_depth=3)
  • Rebased onto latest main and kept the behavior added there since this PR was opened: header-only tables, the _flatten_table_text fallback, isolated visited tracking for empty tables, and table footnotes
  • Added focused tests in tests/test_triplet_serializer.py (17 tests, incl. nested tables, merged cells and header/no-header combinations)
  • Updated expectations in existing tests to the new triplet format:
    • tests/test_hierarchical_chunker.py, tests/test_hybrid_chunker.py, tests/test_serialization.py
    • golden fixtures under tests/data/chunker/ (regenerated with DOCLING_GEN_TEST_DATA=1 uv run pytest tests/test_hierarchical_chunker.py)

Behavior changes worth reviewing

  • Tables without header metadata now use positional labels, e.g. a single-column Country / Italy table built with add_row gives row_0, col_0 = Country. row_1, col_0 = Italy instead of Country = Italy. With column_header=True on the header cell it still gives Country = Italy.
  • Single-cell layout tables now emit row_0, col_0 = <text> instead of just <text>. Happy to special-case 1x1 tables back to plain text if preferred.

Sample Output

Comparing against tests/data/chunker/0c_out_chunks.json.

Old:

cell 0,0, 1 = cell 0,1. cell 1,0, 1 = <p><em>text in italic</em></p>. <ul>\n<li>list item 1</li>\n<li>list item 2</li>\n</ul>, 1 = cell 2,1. cell 3,0, 1 = inner cell 0,0, 1 = inner cell 0,1. inner cell 0,0, 2 = inner cell 0,2. inner cell 1,0, 1 = inner cell 1,1. inner cell 1,0, 2 = inner cell 1,2. <p>Some text in a generic group.</p>\n<p>More text in the group.</p>, 1 = cell 4,1

New:

row_0, col_0 = cell 0,0. row_0, col_1 = cell 0,1. row_1, col_0 = cell 1,0. row_1, col_1 = <p><em>text in italic</em></p>. row_2, col_0 = <ul>\n<li>list item 1</li>\n<li>list item 2</li>\n</ul>. row_2, col_1 = cell 2,1. row_3, col_0 = cell 3,0. row_3, col_1 -> row_0, col_0 = inner cell 0,0. row_0, col_1 = inner cell 0,1. row_0, col_2 = inner cell 0,2. row_1, col_0 = inner cell 1,0. row_1, col_1 = inner cell 1,1. row_1, col_2 = inner cell 1,2. row_4, col_0 = <p>Some text in a generic group.</p>\n<p>More text in the group.</p>. row_4, col_1 = cell 4,1

@github-actions

github-actions Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

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

@wanadzhar913
wanadzhar913 marked this pull request as ready for review April 4, 2026 18:55
@wanadzhar913
wanadzhar913 force-pushed the feature/revize-triplettableserializer branch from 16e2541 to 7476951 Compare April 12, 2026 14:27
@wanadzhar913

Copy link
Copy Markdown
Contributor Author

Hi @ceberam! Hope the general approach I'm taking for this PR is correct. Tysm!

@codecov

codecov Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@wanadzhar913

Copy link
Copy Markdown
Contributor Author

Hi @ceberam, I've added wider test coverage! Apols and tysm.

@ceberam

ceberam commented Apr 13, 2026

Copy link
Copy Markdown
Member

Hi @ceberam, I've added wider test coverage! Apols and tysm.

Thanks @wanadzhar913 , I'll review in the next couple of days. In the meantime, can you please remediate the Developer Certificate of Origin (DCO) check failure?

@wanadzhar913

wanadzhar913 commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @ceberam, I've signed the DCO commit checks.

Am also aware of an open issue in docling-project/docling#3335. Am happy to rebase and reimplement if that fix is more pertinent.

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) chunking Document chunking enhancement New feature or request tables Table model, OTSL, rich tables, TableData, table serialization labels Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

This pull request updates TripletTableSerializer to detect table headers from metadata and use explicit row and column labels when headers are absent. It also adds serialization support for nested tables.

CI safety: no concern found. A maintainer must still approve CI.

Reference data changes

Change Files
text 10
  • text tests/data/chunker/0_out_chunks.json: JSON values differ
  • text tests/data/chunker/0c_out_chunks.json: JSON values differ
  • text tests/data/chunker/2a_out_chunks.json: JSON values differ
  • text tests/data/chunker/2a_out_ser_chunks.json: JSON values differ
  • text tests/data/chunker/2b_out_chunks.json: JSON values differ
  • text tests/data/chunker/2c_out_chunks.json: JSON values differ
  • text tests/data/chunker/2d_out_ser_chunks.json: JSON values differ
  • text tests/data/chunker/2f_out_chunks.json: JSON values differ
  • text tests/data/chunker/2g_out_chunks.json: JSON values differ
  • text tests/data/chunker/table_footnotes_496_out_chunks.json: JSON values differ

Assessment: expected. The reference data changes match the updated triplet serialization format for tables in chunker outputs.

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

@mergify

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

1 similar comment
@mergify

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

@PeterStaar-IBM

Copy link
Copy Markdown
Member

Hi @wanadzhar913, thank you for this pull request.

It has a merge conflict with main. Please rebase your branch on the latest main and resolve the conflict:

git fetch upstream
git rebase upstream/main
# resolve the conflicts, then:
git push --force-with-lease

We can continue with the review and CI after that.

… support

Detect row/column headers from table cell metadata and DataFrame columns instead of positional conventions, emit row_N/col_N labels when headers are absent, and serialize nested tables as inner triplets. Keeps upstream handling of header-only tables, the flatten fallback, isolated visited tracking and table footnotes.

Signed-off-by: wanadzhar913 <[email protected]>
Co-authored-by: Cursor <[email protected]>
@wanadzhar913
wanadzhar913 force-pushed the feature/revize-triplettableserializer branch from c593d94 to d043d35 Compare November 9, 2026 16:54
@mergify

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

@wanadzhar913

Copy link
Copy Markdown
Contributor Author

Hi @PeterStaar-IBM & @ceberam, apologies for leaving this for awhile. I've rebased and looking forward to your feedback & review!

@mergify

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

@mergify

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

@wanadzhar913

Copy link
Copy Markdown
Contributor Author

Hi @PeterStaar-IBM & @ceberam, apologies for leaving this for awhile. I've rebased and looking forward to your feedback & review!

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wanadzhar913, thanks again for working on this, and for your patience. Before you invest more time in the current implementation, I would like to align on the approach, because I think a simpler design solves #528 better and removes most of the issues I've seen in my review.

Direction

The triplet text is meant to be consumed by LLMs, so each value should be attached to the labels that the table itself declares, and the serializer should never guess them from the position of a cell. In docling-core that information is already available per cell as TableCell.column_header, TableCell.row_header and TableCell.row_section, which the backends fill and the Doclang format round-trips.

The main change I am asking for is to build the labels from item.data.grid instead of from the DataFrame returned by _export_to_dataframe_with_options. The DataFrame has already flattened headers, spans and rich cells into strings, and that is the reason why the current PR needs the RangeIndex branch, the DataFrame-columns fallback and the regex over the serialized text. The Markdown serializer already follows the same principle with TableData._count_column_header_rows, so the two serializers will also stay consistent.

Proposed behavior

  1. Column labels. The leading rows returned by TableData._count_column_header_rows(grid=...) form the header block. For each column, join the texts of the header cells with " - " (the convention used in the Markdown serializer, see HEADER_ROW_SEPARATOR), ignoring the repetitions of a cell that spans several rows or columns (compare (start_row_offset_idx, start_col_offset_idx)). A two-level header then reads native backend - TTS.
  2. Row labels. For each body row, join the texts of the cells flagged as row_header in that row, wherever they are located (not only in column 0). If the row has none, use the positional label row_{i}.
  3. Positional fallback, no guessing. When the table has no cell flagged as column_header, no row is treated as a header and the columns are labeled col_{j}. The same goes for any column whose header text is empty. The first row and the first column are never promoted to labels. A table without any flag is therefore serialized as row_0, col_0 = a. row_0, col_1 = b. .... This is intentionally strict and verbose: it is more precise than guessing, and the producers that want the compact form just have to set the header flags. Please document this behavior explicitly in the class docstring of TripletTableSerializer, including the meaning of row_{i} and col_{j}.
  4. Meaning of row_{i} and col_{j}. They are the zero-based indices in the table grid (item.data.grid), including the header rows, so that a label can always be traced back to a cell of the table. Please do not mix this with body-row indices.
  5. Section rows. Cells flagged as row_section are not data: their text should be kept as context for the rows that follow, until the next section row (for example Section A, row label, column = value).
  6. Special cases.
    • A table with a single cell (typically a layout table with one RichTableCell) is emitted as plain text, as it is today on main. A single cell has nothing to relate, and row_0, col_0 = ... would only add noise to the chunks.
    • Empty cells are skipped.
    • Tables that only contain headers keep the current behavior, as does the _flatten_table_text fallback for tables that would otherwise produce no text.
    • Spanned cells are emitted once per cell, not once per grid position.
  7. Nested tables. A RichTableCell holds a ref to its content. If that content is a table, serialize it recursively with the same rules and attach the result to the outer label with ->, for example row_0, col_1 -> row_0, col_0 = x. Please work on the real item (resolving the ref and using the doc serializer already available in serialize()), not on the serialized text, and keep a bounded recursion depth. The regex is_serialized_inner_table and the handling of DataFrames inside DataFrames should go away, since a cell whose text happens to look like a triplet must never be treated as an inner table.
  8. Format. Do not change the separators: the triplets stay row, column = value and are joined with ". ". Only the labels change. -> appears exclusively for nested tables, and " - " only for stacked headers.

Scope and expectations

  • We accept that the output of the serializer changes for tables without header flags, and that the chunker reference files in tests/data/chunker have to be regenerated. Please keep the PR title as it is (feat:), without the breaking-change marker, and mention the change of format in the description, including a before/after example on one real table (for example the Year | Revenue (US$ bn) | Net income (US$ bn) | Employees table of the 2a fixture).
  • The change of format is accepted, but unrelated changes in the golden files are not, so please check the diff of each regenerated file and explain any chunk-count change (for example the extra chunk in 2a/2d).
  • Compute item.data.grid once per call (it is a computed property that rebuilds the grid on each access), and avoid the per-cell DataFrame.iloc access and table_df.copy(). The serialization of a 500x20 table should stay in the same order of magnitude as on main.

Tests

Please replace the tests that call _build_triplets with hand-made DataFrames, and the ones that monkeypatch private methods, with tests that build real tables with doc.add_table_cell(...) and assert on the text produced by the public serializer. The valuable cases are:

  • two stacked header levels, including a header cell with a column span
  • a row header that is not in column 0, and a table with column headers but no row headers
  • a table without any flag (the strict positional fallback)
  • a flagged column header that is not in the first row (it must not promote any body row)
  • a 1x1 table with a RichTableCell (plain text)
  • a section row, if included
  • an inner table with and without headers, and a body cell whose text looks like a triplet
  • one test using a real fixture document through HierarchicalChunker, so that the chunker behavior is covered

The existing tests of TripletTableSerializer live in tests/test_hierarchical_chunker.py; please keep the new ones together with them unless the file gets too large.

Code style

In short: prefer the Google-style docstrings with Args:/Returns: for every new method, type annotations on all signatures, no inline comments that narrate the code (keep only comments that explain something non-obvious for a maintainer), and helpers defined at module or class level instead of inside serialize(). A small private method that returns the labels (column labels per column and row label per row) from the grid would keep serialize() short and easy to test through the public API.


Please let us know if any of this is unclear, or if you see a case that the proposed rules do not cover. Once the approach is settled, I am happy to review the next iteration quickly.

@mergify

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

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

ai:ci-safe AI triage found no CI-safety concern (advisory) chunking Document chunking enhancement New feature or request tables Table model, OTSL, rich tables, TableData, table serialization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review the serialization of tables in triplet format

3 participants