liveBook · Manning

Chunking

As explained in Generative AI in Action

Fixed-length chunking divides text into chunks according to a predetermined character, word, or token length rather than linguistic boundaries.

Fixed-length chunking divides text into pieces containing a predetermined number of characters, words, or tokens. It offers a straightforward way to prepare large corpora for retrieval-augmented generation when model context windows impose size limits. section 7.7.2 section 7.9

Use fixed-length chunking when implementation simplicity, speed, or scale outweighs precise linguistic boundaries:

  • Choose a fixed number of words or tokens to produce predictable chunk sizes and straightforward processing. section 7.7.2
  • Prefer it when context preservation is not critical, computational resources are limited, or the input is poorly structured. section 7.7.5
  • Expect trade-offs: chunks can cut sentences in half, lose context, or require overlap to reduce missed boundaries. section 7.7.2 section 7.7.2
  • Evaluate quality and performance against sentence, semantic, punctuation-based, or hierarchical chunking rather than assuming one strategy fits every corpus. section 7.7.2 section 7.7.5

A fixed-size view of the UK Constitution

Applying fixed-length chunking to the UK Constitution on Wikipedia produces uniform pieces, but some sentences are cut off and surrounding context disappears. An NLP-based strategy preserves more coherent boundaries, making the comparison useful when selecting a production approach. section 7.7.2

Table 7.3 Chunking approaches
Chunking approach
Description
Fixed length
Divide documents into chunks of a fixed number of words or tokens. This is straightforward but may sometimes split information that ideally should be kept together.
Sliding window
Use a fixed-sized sliding window with or without overlapping data. This can ensure that important boundaries within the text are not missed, but it can also lead to redundancy if there’s significant overlap.
Punctuation based
Divide the text based on punctuation, such as paragraphs or sections. This is less arbitrary than fixed-length chunking and often preserves the semantic integrity of the content. However, it can result in variable chunk sizes.
Topic or section breaks
In structured documents such as Wikipedia articles, natural breaks like sections or subsections can be used to define chunks. This method ensures that the content within a chunk is semantically coherent.
Adaptive
Use algorithms or models that adaptively determine the best way to chunk documents based on their content. This can be more complex but might yield semantically cohesive chunks.

Compare fixed length with sliding-window, punctuation-based, and section-based approaches, including their trade-offs around context, redundancy, semantic integrity, and chunk size. section 7.7.2

How Generative AI in Action covers it

How deep it goes
Defines fixed-size splitting, demonstrates lost context, and compares selection factors without detailing a complete implementation.
Examples
Uses the UK Constitution, fixed-size figures, and comparisons with textwrap, NLTK, spaCy, and NLP-based splitting.
What it assumes
Assumes familiarity with text processing and RAG while treating chunking choices and their trade-offs incrementally.
What you can do after
Apply it via a fixed-size splitter, then evaluate retrieval context, performance, resource use, and corpus structure against alternatives.
How it connects
Connects fixed-length chunking to RAG, LLM context windows, vector search, sliding windows, semantic chunking, and NLP-based chunking.

Also explained in

Compare how 5 books explain chunking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.