How RAG Handles Tables and Charts in Documents: A Complete Guide

    This article explains why tables and charts are difficult for retrieval-augmented generation (RAG) systems and how to handle them reliably. It walks through a three-stage pipeline—reading table structure, converting it into HTML, and reassembling it into self-contained context—then covers strategies for non-table charts, retrieval and generation optimizations, and evaluation practices to improve answer accuracy and traceability.

    1. The Starting Problem: Why Charts Are the Hard Part of RAG

    In retrieval-augmented generation (RAG) systems, the pipeline for plain text is relatively mature: chunking, vectorization, retrieval, reranking, context assembly, and answer generation. In real enterprise documents, however, information is often not arranged linearly. It appears densely in the form of tables, flowcharts, statistical charts, and diagrams. Tables are a typical example: they express multi-dimensional semantics through a two-dimensional layout, with relationships such as merging, row spans, column spans, and nesting between cells. Once a chunking strategy cuts blindly by fixed length, the table structure is broken apart, and the retrieved fragments may contain nothing but a pile of numbers without subjects, which the model naturally cannot understand.

    The core difficulty can be summarized in one sentence: a table’s semantics live not only in its text but also in its structure. The processing approach must therefore be upgraded from “extracting text” to a three-stage flow: read the structure, convert the representation, and rebuild the context.

    2. The Overall Pipeline: Read, Convert, Assemble

    2.1 Reading: Detecting Table Regions in a Document

    The first step is layout analysis: separating tables from paragraphs, headings, and images on a page, and determining their boundaries and reading order. A common approach is to detect table regions with a layout analysis model, then recognize cells within each region and align their coordinates. The key metric at this stage is not “how accurately characters are recognized” but “whether structure is preserved”: whether headers are recognized as headers, whether the spans of merged cells are retained, and whether line breaks are mistaken for new rows.

    A schematic intermediate representation can look like this (it expresses structure only and does not involve any specific product interface):

    1. {
    2. "type": "table",
    3. "rows": 4,
    4. "cols": 6,
    5. "cells": [
    6. {"r": 0, "c": 0, "text": "Condition", "rowspan": 1, "colspan": 1, "isHeader": true},
    7. {"r": 0, "c": 3, "text": "Adjustment logic", "colspan": 2, "isHeader": true}
    8. ]
    9. }

    2.2 Conversion: Normalizing to a Structured Representation (HTML)

    Once the structure is available, it needs to be converted into a representation that the model can understand without ambiguity. The industry commonly uses HTML tables as the intermediate format, because HTML naturally expresses rowspan, colspan, and header semantics. An example of the conversion result:

    1. <table>
    2. <thead>
    3. <tr><th>Condition</th><th>Average monthly change online</th><th>Example</th><th>Adjustment logic</th><th>Survey change example</th><th>Settlement adjustment example</th></tr>
    4. </thead>
    5. <tbody>
    6. <tr><td>Change of 5.00% or more</td><td>5.50%</td><td colspan="2">No market survey; the average monthly change online applies</td><td>5.50%</td></tr>
    7. <tr><td>Change not exceeding ±5.00%</td><td>±3.00%</td><td colspan="3">No market survey; the execution price is not adjusted</td></tr>
    8. <tr><td rowspan="3">Change of -5.00% or more</td><td rowspan="3">-5.50%</td><td rowspan="3">Market survey launched</td><td>6.00%</td><td>6.00%</td></tr>
    9. <tr><td>±3.00%</td><td>No adjustment</td></tr>
    10. <tr><td>-6.00%</td><td>-5.50%</td></tr>
    11. </tbody>
    12. </table>

    Three rules must be upheld during conversion: first, never fill in or “correct” values on your own; second, merged cells must explicitly retain their spans; third, separate the header area from the data area so the model does not treat headers as data.

    2.3 Assembly: Rebuilding the Table as Independent Context

    Retrieved fragments should not be handed to the model raw; they must be reassembled into a “self-contained document” that includes the document title, the section path, the table title or caption, the HTML table itself, and any necessary unit and measurement notes. This way, even if the table is retrieved on its own, the model still knows “what this table is about and what the field definitions are.”

    A reusable assembly template:

    1. [Document source] A policy document, Chapter 3, Section 2
    2. [Table description] This table describes the settlement adjustment logic under different change magnitudes
    3. [Unit] Percentage
    4. [Table]
    5. <table>...</table>
    6. [Notes] Merged cells indicate that the same condition covers multiple rows

    3. Handling Charts (Non-Table Graphics)

    For flowcharts, bar charts, line charts, and similar graphics, forcing them into HTML is not advisable. A more reliable approach is a dual track of “image plus structured description”: on one hand, keep the image or an image summary; on the other, generate a structured textual description covering the axes, units, trends, extrema, and key inflection points. On the retrieval side, the description text can be vectorized while the image is placed into a multimodal context, and a model with visual understanding capabilities interprets them together. At a general capability level, Baidu AI Cloud offers multimodal model services, object storage, and log monitoring categories that are often used to support the engineering implementation of such “mixed image-and-text context.”

    4. Key Optimizations on the Retrieval and Generation Sides

    1. Table-aware chunking: do not cut through a table; use “the whole table as one chunk” or “header plus several rows” as the chunk unit to avoid structural fragmentation.
    2. Metadata enrichment: attach the document name, section, table number, and unit to each chunk to improve retrieval explainability.
    3. Reranking and redundancy removal: rerank table chunks after retrieval so that multiple similar tables do not crowd out the context window.
    4. Prompt constraints: explicitly require the model to “answer only based on the given table and state when information is missing,” reducing the risk of fabricated numbers.
    5. Evaluation loop: build a test set containing merged cells, cross-page tables, and empty cells, and run continuous regression.

    5. Conclusion

    The essence of RAG handling charts is to translate “visual layout” into “machine-readable structure” and then rebuild that structure into “context the model can understand.” Reading preserves structure, conversion preserves semantics, and assembly preserves self-containment—none of the three steps can be skipped. In engineering practice, it is recommended to use HTML as the standard intermediate state for tables, handle non-table graphics with “image plus description,” and combine table-aware chunking with strict prompt constraints to significantly improve answer accuracy and traceability.