Table Data in RAG: Practical Parsing and Processing with HTML

    Complex tables can lose essential meaning when document parsing and chunking flatten their structure. This article explains the main challenges of handling tables in retrieval-augmented generation (RAG) and presents a three-step workflow: identify table regions, convert their structure to HTML, and reconnect them with surrounding context. The approach preserves merged cells, header relationships, and business rules so that retrieved content remains easier for language models to interpret.

    Enterprise retrieval-augmented generation (RAG) systems often need to convert large collections of unstructured documents, including PDFs, Word files, and presentations, into knowledge bases that large language models can use.

    These documents contain more than plain text. Charts, layout conventions, and multilevel hierarchies all contribute to their meaning. Tables are particularly challenging because they encode dense, structured information through relationships between rows, columns, headers, and merged cells.

    If those relationships are lost during parsing or chunking, a language model may retrieve the relevant content but still interpret it incorrectly. This article examines how to preserve table structure and context in a RAG pipeline using HTML as an intermediate representation.

    1. Why Table Data Is Difficult to Process in RAG

    A conventional RAG pipeline splits documents into fixed-length text chunks, converts those chunks into embeddings, and stores them in a vector database. When documents contain tables, this approach exposes three major problems.

    Loss of relationships between headers and values

    Converting a table into plain text or flattening it row by row can break the association between column headers and cell values.

    Although language models are strong at understanding natural language, flattened table text no longer explicitly represents the original two-dimensional relationships. The model may struggle to determine which column a value belongs to, leading to inconsistent or incorrect answers.

    Misalignment caused by merged cells

    Complex tables often use cells that span multiple rows or columns to express category hierarchies or business rules. Extracting their contents mechanically, one row or column at a time, can separate values from the context that gives them meaning.

    For example, a category label spanning three rows may appear only once in the extracted text. The subsequent rows may then lose their association with that category, undermining the table’s original semantics.

    Inaccurate extraction of table information

    When extracting content from formats such as PDF, basic OCR may recognize the text without preserving row and column boundaries. Values can become concatenated or disconnected from their headers.

    Once this incomplete representation enters the RAG pipeline, the model lacks the information needed for reliable reasoning or calculations.

    2. Preserve Table Structure with HTML

    A useful intermediate representation must capture a table’s structure while remaining interpretable by a language model. HTML provides a practical option.

    HTML represents tables explicitly through elements such as:

    • <table> for the table itself
    • <tr> for a row
    • <td> for a data cell
    • <th> for a header cell

    The colspan and rowspan attributes describe cells that span multiple columns or rows. These attributes help preserve merged-cell relationships and the hierarchies they represent.

    Mainstream language models have encountered substantial amounts of HTML during pretraining. An HTML table therefore provides a familiar, structured input format that makes relationships between headers, cells, and shared conditions more explicit than flattened text.

    This representation is particularly useful when merged cells carry important business context.

    3. Implementation: Read, Convert, and Reassemble

    The HTML-based workflow consists of three main steps:

    1. Read the document and identify table regions.
    2. Convert the extracted table structure into HTML.
    3. Reassemble tables with their surrounding context before chunking and indexing.

    Step 1: Read the Document and Identify Table Regions

    The first step is to locate tables accurately within the source document. This typically relies on document layout analysis or OCR.

    Modern document-parsing approaches can use visual models to distinguish text paragraphs, images, tables, headers, footers, and other layout regions.

    When a table is detected, extracting its text is only part of the task. The parser also needs to identify:

    • Row and column arrangement
    • Cell boundaries
    • Cells that span multiple rows or columns

    The resulting information can be represented internally as a two-dimensional matrix or a tree-based structure. This representation must retain the table’s topology so that the conversion stage can reconstruct its relationships.

    Step 2: Convert the Table Structure into HTML

    Once the table structure has been extracted, a conversion algorithm can generate its HTML representation. The key task is calculating the correct rowspan and colspan values for each merged cell.

    The algorithm traverses the table’s rows and columns. At the starting position of a merged cell, it emits a <td> element with the appropriate span attributes. It then skips subsequent positions already covered by that cell.

    For example, the following excerpt represents a price-adjustment table with merged cells:

    1. <table>
    2. <thead>
    3. <tr>
    4. <th>Scenario</th>
    5. <th>Average monthly online price change</th>
    6. <th>Example</th>
    7. <th colspan="2">Adjustment logic</th>
    8. <th>Example settlement price adjustment</th>
    9. </tr>
    10. </thead>
    11. <tbody>
    12. <tr>
    13. <td></td>
    14. <td>Change reaches or exceeds 5.00%</td>
    15. <td>5.50%</td>
    16. <td colspan="2">No market survey; use the average monthly online price change</td>
    17. <td>5.50%</td>
    18. </tr>
    19. <tr>
    20. <td rowspan="3"></td>
    21. <td rowspan="3">Change reaches or exceeds -5.00%</td>
    22. <td rowspan="3">-5.50%</td>
    23. <td rowspan="3">Conduct a market survey</td>
    24. <td>6.00%</td>
    25. <td>6.00%</td>
    26. </tr>
    27. <!-- Additional rows omitted -->
    28. </tbody>
    29. </table>

    Here, the condition “Change reaches or exceeds -5.00%” spans three rows. The HTML structure explicitly links that condition to the corresponding entries below it.

    Rather than encountering an isolated condition followed by disconnected values, the model receives a representation showing that those entries share the same prerequisite.

    Step 3: Reassemble Context Before Chunking and Indexing

    Tables rarely stand alone. They commonly appear under a section heading and are accompanied by introductory text, explanatory notes, or follow-up discussion.

    Separating a table from that context during chunking can remove essential business information. After generating the HTML, the pipeline should therefore reconnect it with the relevant surrounding text to form a complete knowledge chunk.

    For example, a reconstructed section might look like this:

    1. 4.2.1 Annual Price Adjustments for Scrap Materials
    2. 4.2.1.1 Price-Adjustment Mechanism for Standard Categories
    3. (Single-Material Categories Such as Scrap Steel and Scrap Aluminum)
    4. Settlement prices remain unchanged for September 20XX.
    5. The following adjustment mechanism applies from October 1.
    6. Example of Monthly Price-Adjustment Logic
    7. [Insert the complete generated HTML table here]
    8. Note: If the market survey result differs from the average monthly
    9. online price change, the market survey result takes precedence.

    In the RAG chunking strategy, this section—including the HTML table—should be treated as a single semantic unit rather than split arbitrarily.

    For files stored in object storage or a cloud-based document repository, a custom loader can implement this processing logic.

    Keeping the table and its context together helps preserve both the structural relationships within the table and the business rules surrounding it. When a user asks, “What adjustment logic applies when the change exceeds -5.00%?”, the retrieval system can return a chunk containing the complete HTML table and its explanatory context. The language model can then interpret the answer using the preserved structure.

    4. Conclusion

    Table processing is an important factor in the quality of enterprise RAG question answering. Flattening complex tables can destroy header relationships, merged-cell context, and the rules needed to interpret individual values.

    Converting extracted tables into HTML provides a structured representation that preserves rows, columns, and cell spans in a format familiar to language models.

    The read–convert–reassemble workflow addresses both structural and contextual information loss:

    • Read: Identify table regions and extract their row, column, and merged-cell structure.
    • Convert: Generate HTML that preserves those relationships through table elements and span attributes.
    • Reassemble: Keep each table with its relevant headings, introductory text, and notes before chunking and indexing.

    As multimodal models and document layout analysis techniques continue to improve, table parsing is expected to become more efficient and accurate. Preserving table structure and context remains essential to making enterprise document knowledge usable in RAG systems.