Course contentsShow
← Machine Learning and Deep Learning
Lesson 1992 of 3,53842. Retrieval-Augmented Generation: FoundationsFree lesson

Handling Code and Structured Data

Specialized chunking strategies for code, tables, and structured content that preserve functional boundaries.

Handling Code and Structured Data

What You'll Learn

You'll learn specialized chunking strategies that preserve the functional meaning of code, tables, and other structured content in RAG systems.

The Challenge with Structured Content

Regular text chunking strategies (fixed-size, sentence-based, etc.) fall apart when applied to code or structured data like tables and JSON. Why? Functional boundaries matter. Splitting a function definition across chunks breaks semantic understanding. Cutting a table row in half renders both halves meaningless.

Think of it like slicing a recipe: cutting mid-instruction ("Preheat oven to 350° / and bake for 30 minutes") makes both fragments useless. Code and structured data have inherent logical units that must stay intact.

Specialized Chunking Strategies

Code Chunking

  • Function/method-level boundaries: Keep entire function definitions together, including docstrings and comments
  • Class-level grouping: Bundle related methods with their class definition
  • Import context: Preserve import statements with the code that uses them
  • Hierarchical splitting: Split large files by classes first, then methods if needed

Table Chunking

  • Row-preserving splits: Never split within a row; keep column headers with every chunk
  • Semantic row grouping: Group related rows (e.g., quarterly data together)
  • Metadata inclusion: Repeat table titles and context in each chunk

JSON/XML Chunking

  • Object-level boundaries: Keep complete JSON objects intact
  • Nested structure awareness: Don't break parent-child relationships
  • Schema preservation: Include schema hints or structure markers

Why This Matters for RAG

When your LLM retrieves a code snippet or table fragment, it needs enough context to understand what it's looking at and how to use it. Preserving functional boundaries ensures retrieved chunks are independently meaningful.

Key Takeaway: Code and structured data require chunking strategies that respect logical boundaries—functions, table rows, and complete objects—rather than arbitrary character or sentence splits.