Python, from Zero, for AILesson 83 of 89

Module 9 · Building the thing: a small AI program, end to end

Chunking: splitting a document that does not fit, without cutting a sentence in half

In this lesson

  • 8 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 4 of 10 in this chapter

This chapter’s goalAssemble a complete tool from the course’s parts (a chat loop with bounded history, injection-resistant prompt templates, a sqlite response cache, a document chunker, a local embedding search over your own notes, a free local model as the fallback provider) expose it through a Gradio interface and a FastAPI endpoint, run it on a schedule without double-processing, and gate every change behind a GitHub Actions job that runs the tests.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:39Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

Why chunk at all

A model has a context window, and a document is often larger than it. Even when the document fits, sending forty pages to answer a question about one paragraph costs forty pages of tokens per question. And an embedding (module 8’s vector) represents one piece of text; a vector for an entire book averages away everything specific. So documents get split into chunks: small enough to embed meaningfully and to send cheaply, large enough to carry a complete thought.

The naive version, and its failure:

Python
chunks = [text[i:i + 1000] for i in range(0, len(text), 1000)]

This cuts at character 1,000 wherever that falls: mid-word, mid-sentence, mid-number. A fact at the boundary is split between two chunks, and neither chunk contains it. The search finds nothing, or finds half, and the answer is confidently wrong. Almost every complaint that “the search does not find things I know are there” traces to this.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Chunking: splitting a document that does not fit, without cutting a sentence in half’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/chunking-text (Accessed: 21 November 2026).