Sentence Chunking
Sentence chunking splits documents around sentence boundaries so each chunk keeps complete thoughts together. It is a simple structure-aware approach that can improve retrieval quality compared with arbitrary fixed windows.
By the end of this lesson, you will be able to:
- Explain what sentence chunking is and why sentence boundaries matter.
- Split text into complete sentences with Python.
- Combine sentences into chunks using a target size.
- Understand sentence overlap and its effect on retrieval context.
- Compare sentence chunking with fixed-size and recursive approaches.
- Evaluate whether sentence boundaries are helping your RAG system.
What Is Sentence Chunking?
Sentence chunking divides a document into chunks using sentence boundaries instead of cutting text at an arbitrary character position.
The basic idea is simple: detect complete sentences first, then group one or more sentences into a chunk. This helps a chunk preserve a complete thought when the source document has normal prose.
Why Sentence Boundaries Matter
Imagine a policy sentence says that a refund is available within 30 days if the product is returned unused. Splitting in the middle of that sentence can separate the condition from the rule.
Meaning can be split
Chunk 1: βA refund is available within 30 days if the product is returnedβ¦β
Chunk 2: ββ¦unused and in its original packaging.β
Complete thought
The complete sentence stays together, making the retrieved chunk easier for an embedding model and downstream LLM to interpret.
This does not mean every sentence should become its own chunk. Very short chunks may lose useful context, so sentence chunking usually means grouping several sentences into a practical window.
Sentence Chunking vs Fixed-size Chunking
Step 1: Split Text into Sentences
For a learning example, a small regular expression can split ordinary English prose on sentence-ending punctuation. This is useful for understanding the algorithm, but real production text can contain abbreviations, decimals, quotations, headings, and other edge cases.
import re
text = "RAG retrieves external knowledge. The retrieved text is added to the prompt. The model then generates an answer."
sentences = re.split(r'(?<=[.!?])\s+', text.strip())
for number, sentence in enumerate(sentences, start=1):
print(f"Sentence {number}: {sentence}")Step 2: Group Sentences into Chunks
After splitting the document into sentences, we can keep adding sentences until a target word count is reached. The boundary is checked between sentences, not in the middle of one.
sentences = [
"RAG retrieves external knowledge.",
"The retrieved text is added to the prompt.",
"The model then generates an answer.",
"Good chunking can improve retrieval quality."
]
max_words = 12
chunks = []
current = []
current_words = 0
for sentence in sentences:
words = len(sentence.split())
if current and current_words + words > max_words:
chunks.append(" ".join(current))
current = []
current_words = 0
current.append(sentence)
current_words += words
if current:
chunks.append(" ".join(current))
for number, chunk in enumerate(chunks, start=1):
print(f"Chunk {number}: {chunk}")The result is a set of chunks made from complete sentences. The target is a guide, not a guarantee that every chunk has exactly the same number of words.
Sentence Chunk Size
There are two different decisions: where a sentence ends and how many sentences should be grouped together. A useful starting point is to choose a maximum word or token budget and add complete sentences until the next sentence would exceed it.
1β2 sentences
Precise retrieval, but may lack surrounding context.
3β6 sentences
Often provides a useful balance for ordinary explanatory text.
Many sentences
More context, but potentially less precise retrieval and more tokens.
Sentence Chunking and Overlap
Overlap means repeating some sentences at the beginning of the next chunk. This can preserve context when an important idea spans a chunk boundary.
Python: Sentence Overlap
A simple sentence-window approach can move forward by fewer sentences than the chunk contains.
sentences = [
"Sentence one explains the problem.",
"Sentence two adds important context.",
"Sentence three gives the rule.",
"Sentence four explains the exception.",
"Sentence five gives an example."
]
chunk_size = 3
overlap = 1
step = chunk_size - overlap
chunks = [
" ".join(sentences[i:i + chunk_size])
for i in range(0, len(sentences), step)
]
for number, chunk in enumerate(chunks, start=1):
print(f"Chunk {number}: {chunk}")Here, the last sentence of one window is repeated in the next window. Production systems should also prevent unnecessary tiny final chunks.
Sentence Chunking Is Not Semantic Chunking
Sentence boundaries are linguistic boundaries, not semantic boundaries. Two adjacent sentences can discuss different topics, while a single topic can continue across several paragraphs.
Uses sentence boundaries
Fast and understandable. It focuses on preserving complete sentences rather than measuring semantic change.
Uses meaning changes
Attempts to group text around shifts in semantic similarity or topic, usually with additional computation.
Sentence chunking is therefore a useful middle step between arbitrary fixed windows and more advanced semantic strategies.
Sentence Chunking in RAG
Split by sentences
Convert long documents into manageable groups of complete sentences.
Create vectors
Generate an embedding for each sentence-based chunk.
Index chunks
Store vectors and useful metadata in a vector index or database.
Find relevant chunks
Embed the user query and retrieve the most similar chunks.
The retrieved chunks are then passed to the LLM as context. Better chunk boundaries can help the retrieved context contain complete, relevant information instead of fragments.
Keep Metadata With Each Chunk
Chunk text is not the only useful information. Store metadata such as the source document, section, page, chunk position, and document ID so the application can filter and trace retrieved results.
Where Sentence Chunking Works Well
Complete sentences can keep an answer together when the source is already well structured.
Keeping complete policy sentences together can avoid arbitrary cuts through conditions.
Paragraphs made of clear sentences are a natural fit for sentence-based grouping.
Several neighboring sentences can form a useful retrieval unit.
Where It Can Struggle
Simple punctuation rules may treat βDr.β or similar abbreviations as sentence endings.
Tables may not have conventional sentence boundaries at all.
Periods and punctuation do not represent natural-language sentences in source code.
A heading can be important context even though it is not a sentence.
PDFs can contain lists, captions, columns, and footnotes that need structure-aware handling.
Adjacent sentences may belong to different topics even when they are grammatically complete.
Improve Sentence Chunking With Structure
A practical pipeline does not have to choose between βonly sentencesβ and βonly fixed size.β You can combine rules.
This approach can preserve a section heading while still using sentences as the basic boundary. If a chunk becomes too large, the size rule can split it between sentences rather than through a sentence.
How to Evaluate Sentence Chunking
Do not judge chunking only by whether the chunks look nice. Test whether the right information is retrieved for real questions.
Measure whether relevant source content is present in top-k results.
Check whether retrieved chunks contain enough surrounding information.
Watch for large chunks that retrieve extra unrelated material.
Inspect questions whose answers cross sentence or section boundaries.
More overlap and smaller chunks can increase embeddings and storage.
Evaluate the complete RAG pipeline, not only vector similarity scores.
Common Mistakes
This can make retrieval too narrow and remove important surrounding context.
Grouping sentences forever defeats the purpose of creating manageable chunks.
Simple punctuation rules can fail on real-world documents.
Sentence text may lose the section context that explains what it means.
Repeated sentences can increase cost and duplicate retrieval results.
A theoretically clean chunker can still perform poorly on your actual questions.
Build a Sentence Chunker
Create a Python function that accepts a paragraph, splits it into sentences, and groups sentences until a maximum word count is reached.
- Split the input into sentences.
- Set a maximum chunk size such as 40 words.
- Add complete sentences until the next sentence would exceed the limit.
- Store each finished chunk in a list.
- Try one version with zero overlap and another with one-sentence overlap.
- Compare which version gives better context for five sample RAG questions.
source, section, and chunk_index to every generated chunk.Quick Quiz
Sentence endings rather than arbitrary character positions.
No. Multiple sentences are often grouped to preserve useful context.
Repeating one or more sentences between neighboring chunks to preserve continuity.
No. Sentence chunking uses linguistic boundaries; semantic chunking attempts to follow changes in meaning or topic.
Retrieval relevance, context completeness, noise, cost, and final answer quality.
30-Second Recap
Key Takeaways
- Sentence chunking uses natural sentence boundaries instead of arbitrary hard cuts.
- It can preserve complete thoughts better than simple fixed-size splitting.
- Good sentence chunking still needs a practical size limit.
- Overlap can preserve context but increases duplication and cost.
- Sentence chunking is not the same as semantic chunking.
- Real documents need handling for headings, tables, code, abbreviations, and other structures.
- The best chunking strategy is the one that improves retrieval and final answer quality on your real dataset.