Strategies
Heading
One chunk per section. With a size limit, a larger section is split into packed blocks.
heading gives one chunk per section. A section starts at a heading at headingLevel or higher, and includes everything up to the next such heading.
const { chunks } = await chunkDocument( { path: "manual.docx" }, { strategy: "heading" },);When to use it
Section titled “When to use it”Use heading when each section of your documents is a complete answer on its own, and you want to retrieve it whole. Examples are knowledge base articles, API reference pages, and manuals where each section covers one task.
Don’t use it for documents with long sections, or with few headings, unless you set size. Without size, a long section becomes one long chunk, which is a poor match for specific questions and may exceed your embedding model’s limit.
How it works
Section titled “How it works”- The document is divided into sections at every heading at
headingLevelor higher. Deeper headings stay inside their section. - Each section becomes one chunk. Text before the first heading becomes a chunk of its own.
- With
size, a section larger thansizeis split into chunks of whole blocks, likestructuredoes. Small sections are not merged.
Example
Section titled “Example”# Returns policy
You can return most items within 30 days of delivery. Items must be unused and in their original packaging.
## Start a return
1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.
## Refunds
We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |Each # and ## section becomes a chunk:
import { chunkDocument } from "docchunk";
const { chunks } = await chunkDocument( { path: "returns-policy.md" }, { strategy: "heading" },);
for (const chunk of chunks) { console.log(chunk.text); console.log("---");}# Returns policy
You can return most items within 30 days of delivery. Items must be unused and in their original packaging.---## Start a return
1. Sign in and open **Orders**.2. Choose the item you want to return.3. Print the prepaid shipping label.---## Refunds
We refund the original payment method within 5 business days of receiving the item. Shipping costs are not refunded.
| Item | Refund || --- | --- || Unused item | Full price || Opened item | 80% |---With headingLevel: 1, only # headings start a section, so the whole file is one chunk of 467 characters:
import { chunkDocument } from "docchunk";
const { chunks } = await chunkDocument( { path: "returns-policy.md" }, { strategy: "heading", headingLevel: 1 },);
console.log(chunks.length, chunks[0]?.charCount);1 467Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
size |
number |
No limit | Maximum characters per chunk. Sections larger than this are split. A positive integer. |
headingLevel |
number |
3 |
Headings at this level or higher start a new section. An integer from 1 to 6. |
Good to know
Section titled “Good to know”- Large sections give a warning. Without
size, a chunk of more than 8,000 characters adds aLARGE_CHUNKwarning to the result. Setsizeto split large sections. - Headings with no text of their own become small chunks. A title followed directly by a subheading, such as
# Guideright before## Setup, becomes a chunk that contains only the title. - With
size,headingandstructureare close. The difference is thatheadingdoesn’t merge small chunks, and has no overlap.
Related strategies
Section titled “Related strategies”structure: packs sections into chunks of a set size.hierarchicalwithby: "heading": a chunk for every section and subsection, nested.