Repository navigation
feat: revize TripletTableSerializer - #574
wanadzhar913 wants to merge 3 commits into
Conversation
|
✅ DCO Check Passed Thanks @wanadzhar913, all your commits are properly signed off. 🎉 |
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
16e2541 to
7476951
Compare
|
Hi @ceberam! Hope the general approach I'm taking for this PR is correct. Tysm! |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
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? |
|
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. |
AI triage (advisory)Checked commit This pull request updates CI safety: no concern found. A maintainer must still approve CI. Reference data changes
Assessment: expected. The reference data changes match the updated triplet serialization format for tables in chunker outputs. The |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
1 similar comment
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
|
Hi @wanadzhar913, thank you for this pull request. It has a merge conflict with git fetch upstream
git rebase upstream/main
# resolve the conflicts, then:
git push --force-with-leaseWe 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]>
c593d94 to
d043d35
Compare
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
|
Hi @PeterStaar-IBM & @ceberam, apologies for leaving this for awhile. I've rebased and looking forward to your feedback & review! |
Signed-off-by: wanadzhar913 <[email protected]> Co-authored-by: Cursor <[email protected]>
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Signed-off-by: Faiq Adzlan <[email protected]>
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
|
Hi @PeterStaar-IBM & @ceberam, apologies for leaving this for awhile. I've rebased and looking forward to your feedback & review! |
ceberam
left a comment
There was a problem hiding this comment.
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
- 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, seeHEADER_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 readsnative backend - TTS. - Row labels. For each body row, join the texts of the cells flagged as
row_headerin that row, wherever they are located (not only in column 0). If the row has none, use the positional labelrow_{i}. - 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 labeledcol_{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 asrow_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 ofTripletTableSerializer, including the meaning ofrow_{i}andcol_{j}. - Meaning of
row_{i}andcol_{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. - Section rows. Cells flagged as
row_sectionare not data: their text should be kept as context for the rows that follow, until the next section row (for exampleSection A, row label, column = value). - Special cases.
- A table with a single cell (typically a layout table with one
RichTableCell) is emitted as plain text, as it is today onmain. A single cell has nothing to relate, androw_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_textfallback for tables that would otherwise produce no text. - Spanned cells are emitted once per cell, not once per grid position.
- A table with a single cell (typically a layout table with one
- Nested tables. A
RichTableCellholds arefto 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 examplerow_0, col_1 -> row_0, col_0 = x. Please work on the real item (resolving therefand using the doc serializer already available inserialize()), not on the serialized text, and keep a bounded recursion depth. The regexis_serialized_inner_tableand 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. - Format. Do not change the separators: the triplets stay
row, column = valueand 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/chunkerhave 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 theYear | Revenue (US$ bn) | Net income (US$ bn) | Employeestable of the2afixture). - 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.gridonce per call (it is a computed property that rebuilds the grid on each access), and avoid the per-cellDataFrame.ilocaccess andtable_df.copy(). The serialization of a 500x20 table should stay in the same order of magnitude as onmain.
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.
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Why
TripletTableSerializerrelied 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
TripletTableSerializerindocling_core/transforms/chunker/hierarchical_chunker.py:TableCell.row_header/TableCell.column_header)row_i/col_jlabeling when headers are absentDataFramecells->relation (instead of=)max_depth=3)mainand kept the behavior added there since this PR was opened: header-only tables, the_flatten_table_textfallback, isolatedvisitedtracking for empty tables, and table footnotestests/test_triplet_serializer.py(17 tests, incl. nested tables, merged cells and header/no-header combinations)tests/test_hierarchical_chunker.py,tests/test_hybrid_chunker.py,tests/test_serialization.pytests/data/chunker/(regenerated withDOCLING_GEN_TEST_DATA=1 uv run pytest tests/test_hierarchical_chunker.py)Behavior changes worth reviewing
Country / Italytable built withadd_rowgivesrow_0, col_0 = Country. row_1, col_0 = Italyinstead ofCountry = Italy. Withcolumn_header=Trueon the header cell it still givesCountry = Italy.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:
New: