Document Chunking: Optimizing RAG Retrieval Pipelines › Special Document Types
Language AI HandbookAll chapters Document Chunking: Optimizing RAG Retrieval Pipelines › Special Document TypesOne section · Sections in this chapter13 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Different document formats require specialized chunking approaches. The strategies we have discussed work well on well-formed prose, but many real-world documents contain structured content that breaks those assumptions entirely.
Code files present a particularly common challenge. A Python function that spans 80 lines cannot be split sensibly at the character level. The last 10 lines of a function contain variable assignments and return statements that only make sense in the context of the preceding 70 lines. The right unit for code chunking is the function or class definition, not an arbitrary size limit. If possible, parse the abstract syntax tree to identify natural boundaries. For Python, the ast module provides programmatic access to these boundaries. For languages without a Python AST library, regular expressions that match common function signatures often work well enough in practice.
Tables present a different challenge. A table is fundamentally relational: each row is meaningless without the column headers, and each cell value is meaningless without its row and column context. Splitting a table across chunks destroys this relational structure entirely. A chunk containing rows 15-20 of a table without the column headers from row 1 is essentially uninterpretable. The solution is to keep tables as single chunks, even if they exceed the normal chunk size limit, and to convert them to a text representation (like Markdown or CSV format) that preserves row-column relationships. If a table is too large for a single chunk, repeat the header row at the start of each sub-chunk.
The full list of special considerations by document type includes:
- Code files: Chunk along function or class boundaries rather than by line count. Parse the abstract syntax tree if possible to identify natural boundaries.
- Tables: Keep as single chunks even if they exceed the normal size limit. Convert to Markdown or CSV format that preserves row-column relationships.
- Conversational data: Chat logs and dialogue transcripts should be chunked by conversational turns or topic shifts, not by arbitrary size. Each chunk should contain enough turns to understand the context of the conversation.
- Legal and regulatory documents: These often have numbered sections and subsections with precise cross-references. Structural chunking that preserves section numbers and hierarchies is essential. A chunk referencing "as defined in Section 2.1(b)" is useless if you cannot trace that reference.