Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 4 · 3. Data Ingestion & Preparation
Lesson 27 of 68

Markdown & Code Splitting

Preserving structure. Why headers should stick to their content.

Derpx0610 Jan · 1 min read
LangChain Mastery
The Document Object & MetadataLoaders: PDF, HTML, Text, WebCleaning & NormalizationChunking: Why It ExistsRecursiveCharacterTextSplitterToken-Aware & Syntax-Aware SplittingMarkdown & Code SplittingMetadata Enrichment StrategiesDeduplication via Content HashingLazy Loading & Generators
Contents
1. The Lost Context Problem2. MarkdownHeaderTextSplitter3. Why This Wins4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
The Document Object & MetadataLoaders: PDF, HTML, Text, WebCleaning & NormalizationChunking: Why It ExistsRecursiveCharacterTextSplitterToken-Aware & Syntax-Aware SplittingMarkdown & Code SplittingMetadata Enrichment StrategiesDeduplication via Content HashingLazy Loading & Generators
Contents
1. The Lost Context Problem2. MarkdownHeaderTextSplitter3. Why This Wins4. Summary

1. The Lost Context Problem

Imagine this markdown:

Markdown
# Pricing
The cost is $10.

# Refund Policy
No refunds allowed.

If you split blindly in the middle, a chunk might just say "No refunds allowed." When retrieved, the LLM doesn't know what has no refunds. Is it the product? The shipping? It lost the header.

2. MarkdownHeaderTextSplitter

We want to attach the header to the content. Chunk 1: Header: Pricing | Content: The cost is $10. Chunk 2: Header: Refund Policy | Content: No refunds allowed.

markdown_split.py
from langchain_text_splitters import MarkdownHeaderTextSplitter

headers_to_split_on = [
  ("#", "H1"),
  ("##", "H2"),
  ("###", "H3"),
]

markdown_splitter = MarkdownHeaderTextSplitter(
  headers_to_split_on=headers_to_split_on
)

docs = markdown_splitter.split_text(markdown_text)
# Result: metadata={'H1': 'Pricing'} content='The cost is $10.'

3. Why This Wins

By moving stricture into metadata, the LLM always knows the "Parent Context" of any snippet. You eliminate "orphan chunks."

4. Summary

Flat text is bad. Structured text is good. Leverage syntax (Markdown, HTML, Code) to keep context attached to data.

Key Intuition: "Headings are not text; they are metadata."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonToken-Aware & Syntax-Aware SplittingNext lesson →Metadata Enrichment Strategies