---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://mixpeek.com/.well-known/search?q={query}"
  method: GET
  documentation: "https://mixpeek.com/llms.txt"
---

Retrieval

8 min read

Updated 2026-10-20

# Why Does My Docs Chatbot Return Half a Code Example?

Your docs assistant answers with a code sample that stops mid-function, or cannot say which API it belongs to. The page was chunked by length and the cut landed inside the code block. We measured how often common splitters do this on 1,539 code blocks from Mixpeek's own documentation, and how to keep every block whole.

![Ethan Steininger](/people/ethan.webp)

By [Ethan Steininger](/blog/authors/ethan), Founder & CEO, Mixpeek

RAG

Chunking

Code Blocks

Markdown

Documentation Search

LangChain

First-Party Data

[Index your docs with code blocks kept whole. Try it as you read.Text extractor docs →](https://mixpeek.com/docs/processing/extractors/text)

[One-click templatesStart from a templateEach template deploys a working search app on your own files.Browse templates](/templates)

## Why does my docs chatbot return half a code example?

  
  
The page was cut into chunks by length before it was embedded, and a cut landed inside a code block. Search then finds the chunk holding the first half, and the model answers from what it was given. A splitter that reads markdown structure keeps each fenced block in one chunk and labels it with the headings above it, so the code arrives whole and the chatbot knows which section it came from.  
  
We ran six common splitter settings over the 823 pages of Mixpeek's public documentation on 2026-10-20. Splitting by length cut between 10 and 27 percent of the 1,539 code blocks. Splitting by headings kept the blocks together and stripped their indentation. Splitting by markdown structure cut none.  
  

## What happens when the answer stops mid-function?

  
  
Most RAG tutorials split text into windows of a fixed size, often 1,000 characters with 200 characters of overlap. The splitter has no idea a code block has started. If a 40-line example begins 700 characters into a window, the window ends partway through it and the rest lands in the next chunk. Overlap repeats the last 200 characters in the next chunk, which rescues a sentence that straddles the boundary. A code block longer than the overlap still ends up in two pieces, and each piece is retrieved and ranked on its own.  
  
Two more things go wrong at the same time:  
  
*   **The code loses its context.** The chunk holding the snippet often has no heading in it, so the
chatbot cannot tell whether it configures the Python SDK, the REST API or a CLI.*   **Whitespace gets normalized.** Some splitters strip or collapse whitespace. Prose survives that.
Python and YAML do not, because their indentation is the syntax.  
  
When the model receives half an example, it writes the other half itself. Ethan Steininger, Mixpeek's founder, puts the general problem this way: "LLMs alone hallucinate over unstructured data."  
  

## How often does each splitter cut a code block?

  
  
First-party measurement, run 2026-10-20 on every .mdx page of the Mixpeek docs.  
  

Splitter and settings

Code blocks cut across chunks

Kept in one chunk, indentation lost

LangChain RecursiveCharacterTextSplitter, 1,000 characters, 200 overlap

158 (10.3%)

0

LangChain RecursiveCharacterTextSplitter.from\_language(MARKDOWN), 1,000 / 200

152 (9.9%)

0

LangChain MarkdownHeaderTextSplitter on #, ## and ###

1 (0.1%)

1,363 (88.6%)

Fixed 1,000-character windows, 200 overlap (Mixpeek `split_by: "characters"`)

411 (26.7%)

0

Fixed 300-word windows (Mixpeek `split_by: "words"`)

154 (10.0%)

1,300 (84.5%)

Mixpeek `split_by: "markdown"`

0

0

  
  
**How we counted.** The corpus is 823 .mdx files, 189 of which contain code: 1,539 non-empty fenced blocks with a median of 11 lines. A block counts as cut when no single chunk contains its full text, and as "indentation lost" when a chunk contains it only after whitespace is collapsed. LangChain is [langchain-text-splitters 0.3.8](https://pypi.org/project/langchain-text-splitters/0.3.8/) with its default separators. The Mixpeek rows run the production chunker code.  
  
106 of the blocks are longer than 1,000 characters, so no 1,000-character window could hold them. Leaving those out, the recursive splitter still cut 52 of the 1,433 blocks that would have fit (3.6%), and fixed windows cut 305 (21.3%). Two of our own modes are in the table on purpose: the character and word modes cut code too, which is why the markdown mode exists.  
  

## How do I keep code blocks whole when I chunk markdown?

  
  
1\. **Split on structure first.** Break the page at headings and at fences, and treat each [fenced code block](https://spec.commonmark.org/0.31.2/#fenced-code-blocks) as one unit that is never split. 2. **Put the heading path on every chunk**, such as "Retrievers > Stages > Deduplicate", as text at the top of the chunk or as metadata you can filter on. 3. **Split long prose at paragraph boundaries**, and repeat the section heading at the top of each part so every chunk reads on its own. 4. **Decide what happens to very long blocks.** Keep them whole if your embedding model's input limit fits them (about 512 tokens for many BERT-style models, about 8,000 for OpenAI's text-embedding-3 models). Past that, split at blank lines between functions and repeat the heading in each part. 5. **Never collapse whitespace inside code.**  
  
At query time, a "how do I" question is best answered by a code chunk plus the prose section above it. Because both share a heading path, you can fetch the neighbour by that field after retrieval.  
  

## Can LangChain or LlamaIndex do this?

  
  
Partly. LangChain's [MarkdownHeaderTextSplitter](https://python.langchain.com/docs/how_to/markdown_header_metadata_splitter/) splits on headings and records them as metadata. In version 0.3.8 with default settings it strips leading whitespace from each line, so check indented code after splitting. A common pattern runs a [recursive splitter](https://python.langchain.com/docs/how_to/recursive_text_splitter/) over long sections afterwards, and that step can cut code again. The [language-aware splitter](https://python.langchain.com/docs/how_to/code_splitter/) prefers markdown boundaries and still cut 9.9% of our blocks at 1,000 characters. LlamaIndex ships a [MarkdownNodeParser](https://docs.llamaindex.ai/en/stable/module_guides/loading/node_parsers/modules/) that also splits on headings. Whichever you use, run the check below on your own pages.  
  

## How do I check my own index for cut code blocks?

  
  
Run this over each source page and the chunks you produced from it. Any block it returns was cut.

import re

FENCE = re.compile(r"^ {0,3}(\`{3,}|~{3,})")

def code\_blocks(markdown):
    blocks, lines, i = \[\], markdown.split("\\n"), 0
    while i < len(lines):
        opened = FENCE.match(lines\[i\])
        if not opened:
            i += 1
            continue
        marker, body, i = opened.group(1), \[\], i + 1
        while i < len(lines) and not lines\[i\].strip().startswith(marker):
            body.append(lines\[i\])
            i += 1
        i += 1
        if "".join(body).strip():
            blocks.append("\\n".join(body))
    return blocks

def cut\_blocks(markdown, chunks):
    return \[b for b in code\_blocks(markdown) if not any(b in c for c in chunks)\]

To catch whitespace loss as well, compare with `" ".join(b.split())` against the same normalization of each chunk: a block found only that way kept its words and lost its indentation.  
  

## Doing it in Mixpeek

  
  
Mixpeek reads your files where they already live, in S3, GCS or any bucket, and turns what's in them into data your software can search, classify and moderate. For documentation, set `split_by` to `"markdown"` in the text extractor:

{
  "feature\_extractor": {
    "feature\_extractor\_name": "text\_extractor",
    "version": "v1",
    "input\_mappings": { "text": "page\_markdown" },
    "parameters": {
      "split\_by": "markdown",
      "chunk\_size": 300
    }
  }
}

Each heading section becomes one document and each fenced code block another. Code is never split. Every chunk carries `heading_path` (outermost heading first, joined with " > "), `chunk_kind` (`"section"` or `"code"`) and, for code, `code_language` from the fence. Section prose splits at paragraph boundaries once it passes `chunk_size` words, and each part keeps its heading. Front matter is dropped and its title becomes the outermost heading. The ColBERT extractor accepts the same mode.  
  
Who it fits: teams with docs, runbooks or knowledge bases in markdown that already live in a bucket. Who it does not: if your docs fit in a single prompt, skip retrieval and send the page. Build starts at $25 a month for up to 1M vectors ([pricing](/pricing)).  
  

## Frequently Asked Questions

  
  

### What chunk size should I use for documentation with code?

  
  
Where the cuts fall matters more than the size. Split at headings and fences first, keep prose parts around 200 to 400 words, and let each code block be its own chunk at whatever length it has, up to your embedding model's input limit.  
  

### Does chunk overlap stop code blocks from being split?

  
  
Only for blocks shorter than the overlap. With 200 characters of overlap, LangChain's recursive splitter still cut 158 of 1,539 code blocks in our run, 10.3%.  
  

### Why does my chatbot make up the rest of the code?

  
  
It was given half of it. The model fills the gap with something plausible, which is how a correct first half ends in an API call that does not exist.  
  

### How does the chatbot know which API a snippet belongs to?

  
  
From the headings above the snippet. Store the heading path on every chunk, for example "Python SDK > Collections > Create", so a retrieved code block carries its section with it.  
  

### What is a fenced code block?

  
  
A block of code opened by a line of at least three backticks or tildes and closed by a matching line, as defined in the [CommonMark spec](https://spec.commonmark.org/0.31.2/#fenced-code-blocks). The text after the opening fence, such as "python", names the language.  
  

## Related

  
  
*   [Multimodal chunking strategies](/guides/multimodal-chunking-strategies)
*   [Why does my document search miss the answer that is in a chart or table?](/guides/why-does-my-document-search-miss-answers-in-charts-and-tables)
*   [Best document parsing tools for RAG](/curated-lists/best-document-parsing-tools)
*   [Best rerankers for RAG](/curated-lists/best-rerankers)

## Index your docs with code blocks kept whole

Point Mixpeek at the markdown in your bucket and set split\_by to markdown: one document per section, one per code block, each labelled with the headings above it.

[Text extractor docs](https://mixpeek.com/docs/processing/extractors/text)[Compare document parsing tools](/curated-lists/best-document-parsing-tools)

[Explore MVS](/mvs)[See pricing](/pricing)

## Related guides

[

Retrieval

### Chunk Contextualization: How Late Chunking and Contextual Retrieval Fix Broken RAG

A technical guide to the chunking problem that silently breaks document retrieval for AI agents. Learn why naive chunking strips context, how late chunking pools token embeddings after a long-context forward pass, how Anthropic's contextual retrieval prepends LLM-generated context, the math and tradeoffs of each, and how to apply the pattern in Mixpeek.

Read guide →](/guides/chunk-contextualization-late-chunking-contextual-retrieval)[

Retrieval

### Why Does My AI Agent's Search Ignore the Filter It Chose?

The filter usually never reaches the vector search: the tool schema has no filters argument, the adapter drops it, or the model names a field the index lacks, so the agent returns other brands' or customers' results with no error. A runnable script shows 8 of 10 results leaking, and the schema, database filter and merge that fix it.

Read guide →](/guides/why-does-my-ai-agent-search-ignore-the-filter-it-chose)[

Retrieval

### Should a RAG Pipeline Use a Classification Model or an LLM to Filter and Rerank Results?

Use a classification model for any filter or rerank step whose answer is a choice from a list: keep or drop, a label, a relevance score. Keep a generative LLM for steps that write new text. This compares cross-encoder rerankers, decision models such as Jev and Liquid AI d1, and LLM judges on cost, speed and how far you can trust each score.

Read guide →](/guides/decision-models-in-retrieval-pipelines)