promptdojo_

Chunking that respects structure — don't shred your own documents — step 8 of 9

Write recursive_split(text, max_chars, separators) that returns a list of chunks, each ≤ max_chars. Logic:

  • Base case: if len(text) <= max_chars, return [text].
  • Otherwise iterate separators in order. For the first separator that exists in text:
    • Split text on that separator into parts.
    • Reassemble parts greedily: accumulate parts into current (joined with the separator); when adding the next part would exceed max_chars, push current into chunks and start a new accumulator.
    • For any chunk still bigger than max_chars, recurse with separators advanced past the current one.
    • Return the result.
  • If no separator fits, fall through and hard-cut every max_chars characters.

The separators are ["\n\n", ". ", " ", ""]. Use them in order.

Two cases run for you. Expected output:

paragraph case: 3 chunks
long-sentence case: 3 chunks

full-screen editor opens — close anytime to keep reading.