RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /LangChain for Local AI
  6. /Ch. 12
LangChain for Local AI

12. Document Loaders

Chapter 12 of 18 · 20 min
KEY INSIGHT

Document loaders normalize disparate file formats into LangChain's `Document` object, providing a unified interface for downstream processing.

Document loaders read files from disk into LangChain's Document format. Each Document contains page_content (text) and metadata (source, page number, etc.). LangChain supports 50+ loader types including PDFs, CSVs, Markdown, HTML, and proprietary formats.

Start with the simplest loader for plain text files.

from langchain_community.document_loaders import TextLoader

loader = TextLoader("./policy.txt")
documents = loader.load()
print(type(documents[0]))  # <class 'langchain_core.documents.base.Document'>
print(documents[0].page_content[:100])
print(documents[0].metadata)  # {'source': './policy.txt'}

PDF loading requires PyPDFLoader or UnstructuredPDFLoader. The former is faster but extracts text sequentially; the latter handles complex layouts better.

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("./report.pdf")
pages = loader.load_and_split()  # Returns one Document per page
print(f"Loaded {len(pages)} pages")
print(pages[0].page_content)
print(pages[0].metadata)  # Includes {'source': ..., 'page': 1}

CSV files load row by row. Each row becomes a document with column names as keys.

from langchain_community.document_loaders import CSVLoader

loader = CSVLoader("./sales_data.csv")
docs = loader.load()
print(docs[0].page_content)  # "column1: value1\ncolumn2: value2"
print(docs[0].metadata)

For directories, use DirectoryLoader with glob patterns.

from langchain_community.document_loaders import DirectoryLoader

loader = DirectoryLoader(
    "./docs",
    glob="**/*.md",  # Only markdown files
    loader_cls=TextLoader
)
docs = loader.load()

A frequent error: specifying the wrong encoding. Non-UTF8 files crash without explicit encoding.

loader = TextLoader("./legacy_doc.txt", encoding="latin-1")

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Download a PDF, load it with PyPDFLoader, and verify that metadata["page"] increments correctly across pages.

← Chapter 11
Memory: Vector Store
Chapter 13 →
Text Splitters