Neural Mastery
← Practice
Sentence-Boundary-Aware Chunkingmedium

Sentence-Boundary-Aware Chunking

medium RAG & Retrieval 15 min

NumPy allowed · plain Python earns 10 bonus points

RAG step 1/4

A query retrieves context

Embedding similarity ranks the documents before generation.
Open lesson →
01query
02embed
03retrieve
04context
What is RAG?→ embed →top context
retrieval.md44%
rag-systems.md33%
agents.md24%

Cosine-like similarity scores ranked before context assembly

Implement sentence-boundary-respecting chunking -- a real improvement over blind fixed-size chunking that never splits mid-sentence.

Task

Implement semantic_chunk(sentences, max_chunk_chars). Greedily pack consecutive sentences (joined with a single space) into a chunk as long as adding the next sentence keeps the chunk's total length <= max_chunk_chars. Start a new chunk when the next sentence would exceed the limit. A single sentence longer than max_chunk_chars becomes its own chunk unchanged.

Function Signature

semantic_chunk(sentences: list[str], max_chunk_chars: int) -> list[str]

Examples

Example 1: sentences fit in one chunk
Input: sentences = ['Hi there.', 'How are you?'] max_chunk_chars = 100
Output: ['Hi there. How are you?']
Example 2: sentences split across chunks
Input: sentences = ['AAAAA', 'BBBBB', 'CCCCC'] max_chunk_chars = 11
Output: ['AAAAA BBBBB', 'CCCCC']
Example 3: single long sentence own chunk
Input: sentences = ['AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'] max_chunk_chars = 10
Output: ['AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA']

Constraints

  • sentences non-empty
  • max_chunk_chars > 0
Python3Saved ✓
def semantic_chunk(sentences, max_chunk_chars):
# Your implementation here
pass

Case 1: sentences fit in one chunk
Input: sentences = ['Hi there.', 'How are you?'] max_chunk_chars = 100
Expected: ['Hi there. How are you?']
Case 2: sentences split across chunks
Input: sentences = ['AAAAA', 'BBBBB', 'CCCCC'] max_chunk_chars = 11
Expected: ['AAAAA BBBBB', 'CCCCC']
Case 3: single long sentence own chunk
Input: sentences = ['AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'] max_chunk_chars = 10
Expected: ['AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA']