Skip to main content
Node parsers transform documents into smaller chunks (nodes) that are optimized for embedding and retrieval. Effective chunking is critical for RAG performance.

Why Chunking Matters

Chunking breaks large documents into smaller pieces because:
  • Embedding models have token limits - Most models work best with 512-2048 tokens
  • Better semantic granularity - Smaller chunks provide more precise retrieval
  • Improved context relevance - Return only the most relevant sections to the LLM
  • Efficient processing - Easier to embed and index smaller text segments

SentenceSplitter

The most commonly used parser that splits text while respecting sentence boundaries.

Basic Usage

Configuration Options

How It Works

  1. Paragraph splitting: First tries to split by paragraph separators
  2. Sentence splitting: Uses sentence tokenizer to find sentence boundaries
  3. Regex fallback: If sentences are too long, uses secondary regex
  4. Word splitting: Final fallback splits by words
  5. Chunk merging: Combines splits into chunks up to chunkSize with chunkOverlap

Metadata-Aware Splitting

MarkdownNodeParser

Splits markdown documents by headers, preserving document structure.

Features

  • Splits on markdown headers (#, ##, ###, etc.)
  • Preserves header hierarchy in metadata
  • Handles code blocks correctly
  • Each chunk contains one section’s content

CodeSplitter

Parses code using tree-sitter for syntax-aware chunking.

Features

  • Syntax-aware: Respects language structure (functions, classes, etc.)
  • Configurable size: Set maxChars for chunk length
  • Multi-language: Works with any tree-sitter grammar
  • Recursive chunking: Splits large syntax nodes intelligently

SentenceWindowNodeParser

Creates overlapping windows around sentences for better context.

TokenTextSplitter

Splits text by token count without respecting sentence boundaries.

SimpleNodeParser (Deprecated)

SimpleNodeParser is deprecated. Use SentenceSplitter instead.

Custom Parsers

Create your own parser by extending NodeParser:

Choosing a Chunking Strategy

Use SentenceSplitter with:
  • chunkSize: 1024 for most cases
  • chunkSize: 512 for more precise retrieval
  • chunkSize: 2048 for broader context
  • chunkOverlap: 200 to maintain continuity
Use MarkdownNodeParser to:
  • Preserve document structure
  • Keep sections together
  • Add header hierarchy to metadata
  • Improve navigation and citations
Use CodeSplitter to:
  • Respect syntax boundaries
  • Keep functions/classes intact
  • Enable code search and analysis
  • Support multiple languages
Use SentenceWindowNodeParser to:
  • Retrieve exact sentences
  • Provide surrounding context
  • Improve answer accuracy
  • Support citation to specific sentences

Complete Example

Best Practices

  1. Match chunk size to your use case
    • Smaller (512) for precise retrieval
    • Larger (2048) for broad context
  2. Use appropriate overlap
    • 10-20% of chunk size typically works well
    • Prevents losing context at boundaries
  3. Respect document structure
    • Use MarkdownNodeParser for markdown
    • Use CodeSplitter for code
    • Don’t split across major boundaries
  4. Consider metadata
    • Account for metadata in chunk size
    • Use metadata to preserve structure
    • Add custom fields for filtering
  5. Test your strategy
    • Evaluate retrieval quality
    • Adjust chunk size based on results
    • Monitor token usage

Next Steps

Documents

Learn about Document structure

Ingestion

Build complete processing pipelines

Embeddings

Configure embedding models

Retrieval

Optimize retrieval strategies