Principle:0xranx OpenContext Document Chunking
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, Text_Processing |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
A technique for splitting markdown documents into semantically coherent chunks that respect heading boundaries, enabling more precise retrieval and embedding for search systems.
Description
Document chunking addresses the challenge that embedding models have token/character limits and that retrieving entire documents is too coarse for precise search results. Simply splitting text at fixed character boundaries can break semantic units mid-sentence or mid-paragraph, producing incoherent chunks.
Markdown-aware chunking uses the document's heading structure as natural segmentation boundaries. The chunker tracks the heading hierarchy (e.g., "Introduction > Architecture > Core Modules") and associates each chunk with its heading path, providing context that improves both embedding quality and result display.
Key design decisions:
- Character-based sizing: Chunk size is measured in characters (not bytes) to correctly handle Unicode (CJK, emoji).
- Heading-boundary splitting: New top-level or same-level headings start a new chunk, preserving semantic coherence.
- Overlap for continuity: Adjacent chunks share an overlap region (default 200 chars) to avoid losing context at boundaries.
- Small chunk merging: Very small chunks are merged with neighbors to avoid creating tiny, low-information index entries.
Usage
Use this principle when building a search indexing pipeline for markdown documents. It is essential when documents are long enough that a single embedding cannot represent the full content, or when search results need to point to specific sections rather than whole documents. Heading-aware chunking should be preferred over naive fixed-size splitting whenever the source documents have a meaningful heading structure.
Theoretical Basis
The chunking algorithm operates on the parse tree of the markdown document:
Pseudo-code Logic:
# Abstract algorithm (NOT real implementation)
def chunk(content, max_chars=1500, overlap=200):
heading_path = []
chunks = []
current_text = ""
current_start_line = 1
for event in parse_markdown(content):
if event is HEADING_START:
if current_text not empty:
chunks.append(chunk_with_heading(current_text, heading_path))
current_text = ""
update heading_path with new heading level and text
elif event is TEXT:
current_text += event.text
elif event is PARAGRAPH_END:
current_text += "\n\n"
if len(current_text) > max_chars:
chunk_part, remainder = split_at_boundary(current_text, max_chars, overlap)
chunks.append(chunk_with_heading(chunk_part, heading_path))
current_text = remainder
if current_text not empty:
chunks.append(chunk_with_heading(current_text, heading_path))
return merge_small_chunks(chunks)
The heading path is maintained as a stack: when a new heading at level N is encountered, all headings at level >= N are popped before pushing the new heading. This produces paths like "Overview > Architecture > Core Modules".
Practical Guide
- Default sizes: 1500 characters max chunk, 200 characters overlap — tuned for typical markdown documentation.
- Unicode safety: All size calculations use character counts, not byte counts, ensuring CJK and emoji content is handled correctly.
- Heading context: Each chunk carries its heading path, which is stored alongside the content in the vector index and displayed in search results.
- Post-processing: Chunks smaller than a threshold are merged with adjacent chunks to reduce index fragmentation.