Course contentsShow
← AI Engineering
Lesson 344 of 1,8869. Retrieval-Augmented Generation (RAG) - BasicsFree lesson

Document Type-Specific Strategies

Adapting chunking approaches for code, tables, lists, and other structured content types.

Document Type-Specific Strategies

What you'll learn: How to adapt your chunking approach based on whether you're splitting code, tables, lists, or other structured content.

Why Different Documents Need Different Chunking

Not all documents are plain prose. When you apply the same chunking strategy to a Python function, a pricing table, or a bulleted list, you risk breaking semantic boundaries that matter for retrieval quality.

Think of it like cutting food: you slice bread differently than you cube cheese or dice onions. The structure dictates the cutting strategy.

Structure-Aware Chunking Strategies

Code Files

Code has natural boundaries: functions, classes, methods. Splitting mid-function destroys meaning. Instead, chunk by:

  • Complete function definitions
  • Class boundaries
  • Logical code blocks with their docstrings intact

A code chunk should be runnable or at least conceptually complete.

Tables

Tables are relational. Splitting a table row-by-row loses column headers. Better strategies:

  • Keep headers with every chunk
  • Group related rows together (e.g., all products in a category)
  • Include table captions or context in each chunk

Lists and Enumerations

Bulleted or numbered lists often represent related items. Chunk by:

  • Keeping parent item with its sub-items
  • Preserving numbering context
  • Maintaining list hierarchy (don't separate a parent bullet from its children)

Markdown and HTML

Respect heading hierarchy. A chunk under "## Installation" shouldn't bleed into "## Configuration" — the heading provides critical context for retrieval.

The Key Principle

Match your chunking boundaries to the document's inherent structure—don't force uniform chunk sizes when semantic completeness matters more.

Key Takeaway: Structure-aware chunking preserves semantic integrity by respecting the natural boundaries of code blocks, table rows, list hierarchies, and document sections—leading to more accurate RAG retrieval.