Skip to main content
Many chat or Q&A applications involve chunking input documents prior to embedding and vector storage. These notes from Pinecone provide some useful tips:
As mentioned, chunking often aims to keep text with common context together. With this in mind, we might want to specifically honor the structure of the document itself. For example, a markdown file is organized by headers. Creating chunks within specific header groups is an intuitive idea. To address this challenge, we can use MarkdownHeaderTextSplitter. This will split a markdown file by a specified set of headers. For example, if we want to split this markdown:
We can specify the headers to split on:
And content is grouped or split by common headers:
Let’s have a look at some examples below.

Basic usage:

By default, MarkdownHeaderTextSplitter strips headers being split on from the output chunk’s content. This can be disabled by setting strip_headers = False.
The default MarkdownHeaderTextSplitter strips white spaces and new lines. To preserve the original formatting of your Markdown documents, check out ExperimentalMarkdownSyntaxTextSplitter.

How to return markdown lines as separate documents

By default, MarkdownHeaderTextSplitter aggregates lines based on the headers specified in headers_to_split_on. We can disable this by specifying return_each_line:
Note that here header information is retained in the metadata for each document.

How to constrain chunk size:

Within each markdown group we can then apply any text splitter we want, such as RecursiveCharacterTextSplitter, which allows for further control of the chunk size.

Troubleshooting: chunk_overlap doesn’t seem to apply

  • After header-based splitting (e.g., MarkdownHeaderTextSplitter), use split_documents(docs) (not split_text) so that overlap is applied within each section and per-section metadata (headers) is preserved on chunks.
  • Overlap appears only when a single section exceeds chunk_size and is split into multiple chunks.
  • Overlap does not cross section/document boundaries (e.g., # H1## H2).
  • If the header becomes a tiny first chunk, consider setting strip_headers to True so the header line doesn’t become a standalone chunk.
  • If your text lacks newlines/spaces, keep a fallback "" in separators so the splitter can still split and apply overlap.