LangChain Document Loaders
LangChain document loaders read data from files, websites and other sources and turn it into Document objects. A Document has two fields: page_content (the text) and metadata (a dictionary with things like source file and page number). Loaders are the first step of any retrieval app.
The Document Object
from langchain_core.documents import Document
doc = Document(
page_content="Returns are accepted within 30 days of purchase.",
metadata={"source": "policy.txt", "section": "returns"},
)
print(doc.page_content)
print(doc.metadata["source"])Returns are accepted within 30 days of purchase. policy.txt
Loading a Text File
from langchain_community.document_loaders import TextLoader
docs = TextLoader("notes.txt", encoding="utf-8").load()
print(len(docs), docs[0].metadata)1 {'source': 'notes.txt'}Loading a PDF
PyPDFLoader creates one Document per page and records the page number. Install pypdf first (pip install pypdf).
from langchain_community.document_loaders import PyPDFLoader
pages = PyPDFLoader("handbook.pdf").load()
print(len(pages))
print(pages[3].metadata["page"], pages[3].page_content[:80])Sample output: 24 3 Chapter 2: Leave Policy. Full-time employees receive 20 days of paid leave...
Page numbers in metadata are zero-based. Keeping them lets your app cite "page 4" in answers later.
Loading Folders and Web Pages
from langchain_community.document_loaders import DirectoryLoader, WebBaseLoader
folder_docs = DirectoryLoader("docs/", glob="**/*.md", loader_cls=TextLoader).load()
web_docs = WebBaseLoader("https://example.com").load()
print(len(folder_docs), web_docs[0].metadata["source"])Sample output: 12 https://example.com
Useful Loaders
- PyPDFLoader, PyMuPDFLoader: PDF files
- CSVLoader: one Document per row
- WebBaseLoader, RecursiveUrlLoader: web pages (needs beautifulsoup4)
- UnstructuredFileLoader / Docling integrations: Word, PowerPoint and complex layouts
- lazy_load(): yields documents one by one to save memory on large collections
Writing Your Own Loader
You do not need a special class. Any function that returns a list of Document objects works, for example reading rows from your own database and putting the ID and title into metadata.
Frequently asked questions
Which LangChain loader is best for PDFs?
PyPDFLoader is simple and fast for text PDFs. For scanned PDFs or complex tables, use a loader with OCR and layout support such as Unstructured or Docling.
Why is page_content empty for my PDF?
The PDF is probably a scanned image with no text layer. You need an OCR-based loader.
Where are document loaders in LangChain 1.0?
Most live in langchain-community or in dedicated integration packages. The Document class itself is in langchain-core.