Recursive Character Text Splitter

medium
Unit 17 · RAG II0/3 to milestone

Fixed-size chunking cuts sentences in half. LangChain's popular recursive splitter tries the most natural boundaries first: split on paragraphs, and if a piece is still too big, split it on lines, then sentences, then words, then characters.

Implement recursive_split(text, chunk_size, separators=("\n\n", "\n", ". ", " ", "")):

  1. If len(text) <= chunk_size, return [text] (or [] if it's empty or only whitespace).
  2. Otherwise split on the first separator that occurs in the text ("" means individual characters). Keep the separator at the end of each piece except the last, so no text is lost.
  3. Merge consecutive pieces greedily while the combined length stays <= chunk_size.
  4. Any merged piece still longer than chunk_size is split recursively with the remaining separators.

Joining all chunks must reproduce the original text exactly.

#rag#chunking#strings2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Recursive Character Text Splitter · TechGradify