What Is a Document?
DefinitionA Document is a small LangChain object that holds one piece of text. It has two parts:
page_content, the text itself, andmetadata, a dictionary with information about that text, such as where it came from.
docs = [ Document( metadata={'source': 'location of the file', 'page': 0}, page_content='text of page 1 ...'), Document( metadata={'source': 'location of the file', 'page': 1}, page_content='text of page 2 ...'), ]
print(docs) shows.docs[0].page_content is the text of the first Document, a string. This is what we cut into chunks, embed, search and finally give to the LLM.docs[0].metadata is its information, a dictionary. It is about the text, not the text itself: where it came from, which page. Later we use it to say where an answer came from.What Is a Document Loader?
DefinitionA document loader reads a source, such as a text file, a PDF or a web page, and gives back its text as a list of Documents.
loader = SomeLoader(location) docs = loader.load()
.load(). You always get a list of Documents, so the rest of the pipeline does not care where the text came from.Types of Loaders
There is a loader for almost every kind of source. This table shows the common ones.
| Loader | Reads | One Document is |
|---|---|---|
TextLoader | a plain text file | the whole file |
PyPDFLoader | a PDF file | one page |
CSVLoader | a CSV file | one row |
| Many more | Word, Excel, Markdown, JSON, HTML, Wikipedia, YouTube… | depends on the loader |
pip install langchain-communitylangchain-community pypdflangchain-communityfrom langchain_community.document_loaders import TextLoader txtLoader = TextLoader("data/sample_notes.txt", encoding="utf-8") docs = txtLoader.load() print(len(docs)) print(docs[0].metadata)from langchain_community.document_loaders import PyPDFLoader pdfLoader = PyPDFLoader("data/iitd_ug_rules.pdf") docs = pdfLoader.load() print(len(docs)) print(docs[4].metadata["page"]) print(docs[4].metadata["page_label"])from langchain_community.document_loaders import CSVLoader csvLoader = CSVLoader("data/students.csv") docs = csvLoader.load() print(len(docs)) print(docs[0].page_content)
Loader Packages: Why We Install Them
A loader does not read every file format by itself. For each format, a pre-built package already does the hard work. The loader calls that package and puts the result into Documents.
loader.load()langchain-community. It wraps the package.pypdf| Loader | What does the reading | Install |
|---|---|---|
TextLoader | plain Python can read a text file | nothing extra |
PyPDFLoader | pypdf, a package that reads PDF files | pip install pypdf |
CSVLoader | Python's built-in csv module | nothing extra |
| The loader classes themselves | langchain-community: the LangChain package that holds TextLoader, PyPDFLoader and CSVLoader. You import them from langchain_community.document_loaders. | pip install langchain-community |
requirements.txt already lists langchain-community and pypdf.PyPDFLoader: How It Works
A very common loader in RAG, because a lot of company knowledge sits in PDFs.
pypdf.page_content is the text of that page. metadata holds source (the location of the file), page (the page number, starting at 0), page_label, total_pages, and details of the PDF such as its title and author.| You look at | You see |
|---|---|
len(docs) | 30. The PDF has 30 pages, so 30 Documents. |
docs[0].page_content | only 91 characters. It is the cover page: the title and the name of the institute. |
docs[0].metadata | source, page, page_label, total_pages and some facts about the file, like who made it and when. |
docs[4].metadata["page"] | 4 |
docs[4].metadata["page_label"] | 'v' |
page and page_label are different. page is the count the loader makes. It starts at 0. page_label is the number printed on the page, like v (a Roman number). So the fifth page has page 4 and page_label v.Check What You Loaded
A loader gives you Documents even when the text is bad. Look before you go on.
print(len(docs)) print(docs[0].metadata) print(docs[0].page_content[:200])
| Problem | What you see | Why |
|---|---|---|
| No text | page_content is empty | the PDF is a scan. Each page is a picture, and pypdf reads only text. |
| Garbage text | characters like /K37/K2B/K32/K74 | the PDF has a font that pypdf cannot decode. We saw this in a real game-rulebook PDF. |
Loading Methods: load and lazy_load
Every loader has the same methods. They give the same Documents, but they hand them over in different ways.
| Method | It gives | How it works | Use it when |
|---|---|---|---|
load() | a list of Documents | reads everything first, then hands over the whole list | the source is a normal size |
lazy_load() | a generator: one Document at a time | reads the next Document only when your loop asks for it | the source is very big, such as a PDF with thousands of pages, and you want to use little memory |
for doc in loader.lazy_load(): print(doc.metadata)
lazy_load() is a generator: each step of the loop gives one Document, starting with page 0.load() simply collects everything that lazy_load() gives into a list. So the Documents are the same. Only the way they arrive is different.TextLoader01_text_loader.py · 26 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from dotenv import load_dotenv load_dotenv() from langchain.chat_models import init_chat_model from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_community.document_loaders import TextLoader from langchain_core.document_loaders import BaseLoader parser = StrOutputParser() model = init_chat_model("groq:openai/gpt-oss-20b") """TextLoader: one .txt file -> 1 Document""" txtLoader = TextLoader("data/sample_notes.txt", encoding="utf-8") print(type(txtLoader)) # class print(isinstance(txtLoader, BaseLoader)) # TRUE """load() gives a list of Documents: page_content + metadata""" docs = txtLoader.load() print(len(docs)) # -> 1 print(docs) print(docs[0].page_content) |
PyPDFLoader02_pdf_loader.py · 30 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from dotenv import load_dotenv load_dotenv() from langchain.chat_models import init_chat_model from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_community.document_loaders import PyPDFLoader parser = StrOutputParser() """PyPDFLoader: one PDF -> 1 Document per page""" path = "data/iitd_ug_rules.pdf" pdfLoader = PyPDFLoader(path) docs = pdfLoader.load() print(len(docs)) page1 = docs[0] print(page1.page_content) print("-----------------") print(page1.metadata) model = init_chat_model("groq:openai/gpt-oss-20b") parser = StrOutputParser() |