Doclingdocling 2.127.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
26 small wins to finish your path

Changing how a table reads

Lesson 22 turned a table into sentences. That is a choice made by a serializer, and swapping it is how you change any part of the text a chunk contains.

A serializer turns a document, or one piece of it, into text. The exports in part 3 are serializers with friendly names on them. The chunker uses its own set, and they can be replaced one kind at a time.

Example
from docling.chunking import HierarchicalChunker
from docling.document_converter import DocumentConverter

refunds = DocumentConverter().convert("refunds.html").document
for chunk in HierarchicalChunker().chunk(refunds):
    print(repr(chunk.text)[:70])

The default table serializer for chunking writes one statement per cell. It reads well to a model and badly to a person, and for a wide table it is much longer than the grid it replaced.

Swapping the table serializer

Two imports and a four line class. The provider's job is to hand the chunker a serializer for one document; everything except the table part is left alone.

python
from docling_core.transforms.chunker.hierarchical_chunker import (
    ChunkingDocSerializer,
    ChunkingSerializerProvider,
)
from docling_core.transforms.serializer.markdown import MarkdownTableSerializer
python
class MarkdownTables(ChunkingSerializerProvider):
    def get_serializer(self, doc):
        return ChunkingDocSerializer(
            doc=doc, table_serializer=MarkdownTableSerializer())
Example
chunker = HierarchicalChunker(serializer_provider=MarkdownTables())
for chunk in chunker.chunk(refunds):
    print(repr(chunk.text)[:70])

The grid is back, inside the chunk. Which one to use depends on what reads the chunk: a model answering questions does better with the sentences, and a model asked to copy a row out of a table does better with the grid.

It works on the hybrid chunker too

Example
from docling.chunking import HybridChunker

hybrid = HybridChunker(max_tokens=64, serializer_provider=MarkdownTables())
for chunk in hybrid.chunk(refunds):
    print(repr(hybrid.contextualize(chunk))[:64])

Same provider, and the token counting from lesson 24 now counts the pipes. That is the trade the grid costs you: a table that fitted in one chunk as sentences may need two as Markdown.

The other serializers

There is one for each kind of piece: text, tables, pictures, lists, inline runs, and a provider wrapping the lot. Subclass any of them the same way. A picture serializer that writes an image's caption instead of a placeholder is four lines, and it is how the enrichment output in lesson 26 reaches a chunk at all.

The exports in part 3 use the same machinery. MarkdownDocSerializer is what export_to_markdown builds internally, so a serializer you write for chunking can be used for an export too.
Try it yourself
  • Write a provider that leaves tables out of chunks entirely.
  • Compare the token counts of the two table chunks with chunker.tokenizer.count_tokens.
  • Use the Markdown table provider on orders.csv and see the whole spreadsheet come back as a grid.
PreviousChunks that fit

Every expert started right here.