RAG · Lesson 22 of 120
Metadata on Chunks
Where it came from.
When we split a document, each chunk must remember where it came from. The source file, the section, maybe the page. Without that, we can't cite, filter, or update anything.

- Every chunk
- Remembers its source
- Cite, filter, update
Chunks that remember
Split documents takes whole documents, not plain text. Every chunk copies its parent's metadata automatically. We split the shipping page, which has a source in its metadata. Run it. Each chunk carries the source along.
chunk_metadata.pypython
from pathlib import Path
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
page = Document(page_content=Path("data/rag/northwind/shipping.md").read_text(),
metadata={"source": "shipping.md"})
splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=0)
for chunk in splitter.split_documents([page]):
print(chunk.metadata, "|", chunk.page_content[:45].replace("\n", " "))Output
{'source': 'shipping.md'} | # Shipping and delivery
{'source': 'shipping.md'} | ## UK delivery Standard delivery in the UK ta
{'source': 'shipping.md'} | Next-day delivery is available for parts and
{'source': 'shipping.md'} | ## International delivery We ship parts and a
{'source': 'shipping.md'} | ## Tracking Every order gets a tracking linkUseful chunk metadata
Good chunk metadata includes the source file, the title and section, the page for PDFs, and a date or version. A chunk number is handy too, so we can fetch its neighbours later.
| key | value |
|---|---|
| source | file name |
| title, section | from headings |
| page | for PDFs |
| updated | date or version |
Summary
Use split documents, so every chunk copies its parent's metadata. Next, a practice lesson, chunking the whole help centre.
Key takeaways- split_documents
- Metadata copied
- Source on every chunk
Next up: Practice: Chunk the Help Centre — all fourteen files