docling icon indicating copy to clipboard operation
docling copied to clipboard

HybridChunker: Option to use markdown table format instead of flat text serialization

Open kjoth opened this issue 5 months ago • 5 comments

Description

When HybridChunker produces chunks for table elements, the chunk.text output uses a flat text format:

**Use case**, 1 = Least-leaking devices. **Persona**, 1 = Marketing analyst. **Summary**, 1 = A marketing analyst wants to... This format is problematic for RAG pipelines:

  • No clear row/column boundaries
  • The , row_number = pattern adds noise
  • Embedding models produce diluted vectors (formatting artifacts instead of content)
  • LLMs struggle to parse the structure vs standard markdown tables

Expected behavior

An option (e.g., table_format="markdown") to produce pipe-table format for table chunks:

Use case Persona Summary
Least-leaking devices Marketing analyst A marketing analyst wants to...

Context

  • In our evaluation, 53% of chunks from HTML documents are table chunks
  • The repeat_table_header=True parameter correctly repeats column names in split table chunks, but the flat serialization makes them hard to use
  • Docling already has export_to_markdown() on table elements which produces pipe-table format — but HybridChunker doesn't use it

Workaround

Post-processing table chunks after chunking by detecting is_table via chunk.meta.doc_items and calling export_to_markdown() on the table item.

Proposed solution

Add a parameter to HybridChunker:

python chunker = HybridChunker(tokenizer=tokenizer, table_serialization="markdown") # or "text" for current behavior

kjoth avatar Jun 18 '26 05:06 kjoth

@ceberam Tagging on this for your feddback.

kjoth avatar Jun 18 '26 06:06 kjoth

Great writeup, and the 53% stat is the part that should make maintainers treat this as a default-wrong-serialization rather than a nice-to-have option — if over half your corpus chunks come from tables, the serialization choice isn't an edge case, it's the dominant factor in embedding quality.

One angle that doesn't show up in the "diluted vectors" framing: the flat serialization isn't just producing worse embeddings, it's producing embeddings for the wrong structure. The field, value, field, value collapse destroys the cell→column-header binding — once serialized, 1 = Least-leaking devices is an unattributed value with no record that "Least-leaking devices" binds to the "Use case" column vs. the "Persona" column. That binding is the entire semantic payload of a table; the flat text preserves the words but loses the relational structure they were arranged to express. An embedding of flat-serialized table content optimizes for token proximity that has nothing to do with the table's actual meaning.

Why this matters beyond embedding quality — for regulated corpora (contracts, fee schedules, penalty matrices, term-amendment tables), tables are cross-reference targets. A retrieval query like "what's the breach penalty under Section 7?" needs to hit a row where column = penalty-type. Flat serialization turns that into a bag of words where 5% and late delivery and 2% float with no column binding — the LLM gets plausible-looking tokens and can't tell which penalty binds to which trigger, so it produces confidently-wrong answers with no degradation signal. The extraction "succeeded" (all words present), but the structural semantics that made the table answerable are gone. Same failure class as TextItems that extract fine but vanish from body.children (#3473) and tables that parse but go missing in chunking (#16008 in ragflow) — structure is silently lost at a serialization boundary while the content looks intact.

On repeat_table_header=True: that solves the split-table continuity problem (column names recur in each split), but it's solving a problem downstream of the serialization choice. The serialization format is a one-way door — once you flatten, no amount of header repetition reconstructs the cell-to-column binding. So the table_serialization="markdown" parameter is the right fix because it makes the structural decision at the point where the table object is still in scope, which is also the only point where the binding is knowable.

One concern with the post-processing workaround: it works, but it requires round-tripping back to the table item via chunk.meta.doc_items to call export_to_markdown(). That's fragile because it assumes the chunk retains its provenance link through storage — once a chunk is embedded and persisted to a vector store without its meta.doc_items payload (common in production), the table object is unreachable and the structure is unrecoverable. Making table_serialization a chunking-time parameter bakes the decision into the chunk itself so it survives storage. Essentially: serialize at the source, don't reconstruct downstream — provenance links are the first thing lost in a real pipeline.

zeweihan avatar Jun 21 '26 22:06 zeweihan

Hi @kjoth, this looks useful for RAG workflows. I’d like to work on this.

My plan is to add a backward-compatible option to HybridChunker, something like table_serialization="text" | "markdown", keeping the current flat text behavior as default. When set to markdown, table chunks would serialize table items using the existing export_to_markdown() path, with tests covering both default and markdown output.

Please let me know if this direction sounds okay before I start.

sriharan0804 avatar Jun 28 '26 07:06 sriharan0804

I've implemented this in docling-core: https://github.com/docling-project/docling-core/pull/674

Usage

from docling_core.transforms.chunker import HybridChunker

# Default behavior (unchanged)
chunker = HybridChunker(tokenizer=tokenizer, table_serialization="triplet")

# New: markdown pipe-table format
chunker = HybridChunker(tokenizer=tokenizer, table_serialization="markdown")

# New: markdown + compact (no column padding, saves tokens)
chunker = HybridChunker(tokenizer=tokenizer, table_serialization="markdown", compact_tables=True)

Before — triplet (current default, unchanged)

Apple M3 Max, Thread budget. = 4. Apple M3 Max, native backend.TTS = 177 s 167 s.
Apple M3 Max, native backend.Pages/s = 1.27 1.34. Apple M3 Max, native backend.Mem = 6.20 GB.
Apple M3 Max, pypdfium backend.TTS = 103 s 92 s. Apple M3 Max, pypdfium backend.Pages/s = 2.18 2.45.
Apple M3 Max, pypdfium backend.Mem = 2.56 GB.

After — markdown + compact_tables=True

| CPU | Thread budget | native backend | native backend | native backend | pypdfium backend | pypdfium backend | pypdfium backend |
| - | - | - | - | - | - | - | - |
|  |  | TTS | Pages/s | Mem | TTS | Pages/s | Mem |
| Apple M3 Max | 4 | 177 s 167 s | 1.27 1.34 | 6.20 GB | 103 s 92 s | 2.18 2.45 | 2.56 GB |

Implementation details

  • Added TableSerializationFormat enum ("triplet" | "markdown")
  • When table_serialization="markdown", internally wires up MarkdownTableSerializer via a custom ChunkingSerializerProvider
  • compact_tables=True strips column padding to reduce token count
  • Fully backward compatible — default is "triplet"
  • Works with repeat_table_header=True (headers are repeated in split table chunks)
  • All existing tests pass, new test added

Happy to adjust the API if maintainers prefer a different approach.

kjoth avatar Jul 04 '26 10:07 kjoth

@kjoth Thanks for your willingness in contributing to Docling.

What you are trying to achieve is already possible by creating a markdown ChunkingSerializerProvider and passing it to the hybrid chunker. This use case is explained in the documentation below: https://docling-project.github.io/docling/_generated/examples/advanced_chunking_and_serialization/#configuring-a-different-strategy

You can use the same strategy to serialize a table in HTML format. This may be useful if your tables are too complex for the markdown format (e.g., nested tables).

I will close this issue but please feel free to add any question or suggestion.

ceberam avatar Jul 06 '26 07:07 ceberam