Structure-aware chunking splits along heading tags such as H1, H2 and H3, so a title stays with the paragraphs that belong to it. That keeps each chunk self-contained: when a chunk is retrieved on its own, the model can see what section it came from instead of guessing from a fragment. It works best on documents that already have real heading structure.
What you need
- Documents with real heading structure, in Markdown or HTML
- A splitter that can split on heading tags
- A note of the heading levels your documents use
Step 1: Map the heading structure first
List the headings in a sample document and check they mark real sections, not styling. A document whose headings are only bold text will not split cleanly.
Step 2: Split at the headings
Split along the heading tags so each chunk starts with its heading and holds the paragraphs under it. Keep the heading text inside the chunk, not in a separate record.
Step 3: Check a retrieved chunk reads alone
Pick three chunks at random and read each one without its neighbours. If a chunk makes sense on its own, the split works; if it needs the previous chunk to be understood, split finer or add the heading to the chunk text.
Worked example: synthetic heading split of a help article
The table below is a synthetic example. It shows the expected result of splitting by headings; no document was split.
| Section | Heading kept? | Reads alone? |
|---|---|---|
| Installing the app | Yes | Yes |
| Setting up an account | Yes | Yes |
| Troubleshooting login | Yes | Yes |
| Appendix | No – no heading in source | No – needs its parent |
Expected result (UNTESTED). 3 of 4 chunks keep their heading and read alone; the appendix chunk has no heading in the source and needs its parent section. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Headings only help when documents have real structure; flat text with no headings needs fixed-size or sentence-boundary chunking.
- Very long sections still need a second split inside the section.
- Sentence-boundary chunking, using a toolkit such as spaCy or NLTK, is the alternative when headings are unreliable.
- Attach section_heading metadata to every chunk so a query can be restricted to one section.
