RAG Knowledge Bases: Embedding Tables, Images, and Other Non-Text Content
Learn how to bring tables and images into a RAG retrieval pipeline through document layout parsing, structured text conversion, multimodal embeddings, and visual descriptions. This article outlines practical processing workflows, chunking strategies, and metadata design for preserving context and improving retrieval quality.
When building a vector database for a retrieval-augmented generation (RAG) system, developers often encounter the same challenge: tables, images, formulas, and other non-text content are difficult to incorporate into retrieval. Conventional text embedding models process text passages, leaving gaps when important information resides in other formats.
For example, the most important figures in a financial report may appear in tables, while the central argument in a technical white paper may depend on an architecture diagram. If this content cannot be retrieved, the RAG system’s answers become less complete.
This article presents an engineering-oriented workflow for handling non-text information, focusing on tables and images.
1. Why Non-Text Content Is Difficult to Embed
Text embedding models are primarily trained on natural-language sentences and paragraphs. Their representation spaces distinguish the semantics of continuous prose well, but non-text content has different structural requirements:
- Tables are two-dimensional. Meaning depends on relationships between rows and columns. Simply flattening rows into text can lose the connection between headers and values, making queries such as “What was this metric in this quarter?” harder to match.
- Images contain visual signals. Pixel data differs fundamentally from the input accepted by ordinary text embedding models.
- Complex layouts disrupt context. PDF tables may span multiple pages, images may be separated from their captions, and multi-column layouts may interleave content. Poor chunking can break semantic continuity.
The core requirement is to establish a conversion path that places non-text content in the same embedding space as text—or in a representation that supports joint retrieval.
2. Processing Tables
2.1 Convert Tables into Structured Text
The most broadly applicable approach is to parse tables into structured data and then serialize that data as text. Common strategies include:
- HTML or Markdown: Represent the table with markup that preserves rows and columns. Some embedding models with code-understanding capabilities can make effective use of this structure.
- Cell-level statements: Convert each cell into a statement that explicitly associates its value with the relevant row and column headers. For example: “Company A’s revenue in Q3 2024 was RMB 1.25 billion.” This format works well with text-only embedding models, particularly when likely query patterns are predictable.
Example:
def table_to_statements(headers, rows):"""Convert table cells into statements to improve text embedding retrieval."""statements = []for row in rows:row_name = row[0]for i, cell in enumerate(row[1:], start=1):statements.append(f"{row_name}'s {headers[i]} is {cell}")return statements
2.2 Summarize Large Tables
For large tables, an LLM can generate a summary. Store both the summary and the structured representation to provide two complementary retrieval views: exact data and a semantic overview.
3. Processing Images
3.1 Use Multimodal Embeddings
One common approach is to use a multimodal embedding model, such as an image-text alignment model trained through contrastive learning. These models encode images and text into an aligned vector space, allowing image content and text queries to participate in similarity retrieval within the same index.
3.2 Generate Descriptions with a Vision-Language Model
Another flexible approach is to use a vision-language model (VLM) to generate detailed image descriptions, or structured questions and answers, and then process that text through the standard text embedding pipeline:
caption = vlm_generate(image,prompt="Describe the data, trends, and key conclusions in this image in detail")embedding = text_embedder.encode(caption)
This approach supports domain-specific customization. For charts, prompts can ask the model to describe data points, trends, axes, and other relevant elements. For FAQ-style queries, these targeted descriptions can provide better recall than generic captions.
3.3 Preserve Image–Text Relationships
Store images together with their surrounding text and captions as linked chunks. When an image is retrieved, return its context to the generation model as well. This helps avoid retrieving an image without enough information to interpret its role in the document.
4. Document Layout Parsing: The Foundation of the Pipeline
High-quality document parsing is the first step for both tables and images. Important capabilities include:
- Layout analysis: Identify headings, paragraphs, tables, images, headers, and footers, along with their reading order.
- Table structure reconstruction: Recognize bordered and borderless tables, tables spanning multiple pages, and merged cells.
- A consistent intermediate format: Output parsed content as Markdown or structured JSON. Markdown is convenient for LLM processing, while JSON preserves coordinates and hierarchy for customized chunking strategies.
Document parsing tools and services, including general-purpose services offered by cloud providers, often package these capabilities. OCR and document format conversion services can help developers build a pipeline without implementing layout analysis models from scratch.
A typical workflow looks like this:
Source document → Layout parsing → Route by content type├─ Text → Text chunking → Text embeddings├─ Tables → Structured conversion│ → Serialization/summaries → Embeddings└─ Images → Multimodal embeddingsor VLM descriptions → EmbeddingsAll vectors → Vector databaseMetadata: page number, content type, coordinates
A shared index is appropriate when embeddings use an aligned representation space; other representations must remain jointly retrievable through the retrieval pipeline.
5. Chunking and Metadata Recommendations
- Keep tables intact where possible. Avoid splitting tables at arbitrary fixed-length boundaries. Treat each table as a complete chunk. If a table is too large, divide it into row groups and repeat the headers in each group.
- Attach rich metadata. Store source page numbers, content types—text, table, or image—and contextual anchors with each vector. These fields also support filtered retrieval.
- Use hybrid retrieval. For queries involving numerical table data, combine vector retrieval with keyword search or exact database queries. This compensates for the limited sensitivity of vector similarity to exact numerical values.
6. Conclusion
Handling non-text content in RAG depends on three stages: parsing, conversion, and alignment.
High-quality layout parsing reconstructs document structure. Structured serialization and multimodal representations make heterogeneous content searchable. An aligned embedding space—or a pipeline that supports joint retrieval—then brings that content into the retrieval process.
Combined with appropriate chunking and metadata, this workflow can improve retrieval accuracy and answer quality while preserving information that text-only pipelines often miss.
