Agentic AI

Document loaders

A loader reads a source, such as a text file, a PDF, a web page, an arXiv paper or a Wikipedia article, and returns a list of Document objects: the text plus metadata about where it came from.

Concept + code Python 3.12

Every retrieval app starts by getting text in. LangChain has loaders for hundreds of sources, and they all return the same thing, a list of Document objects, so the splitters, embeddings and vector stores of the next lessons work with any of them.

Download speech.txt, the opening of President Wilson's 1917 address to Congress, and attention.pdf, "Attention Is All You Need", the paper that introduced the Transformer (also on arXiv as 1706.03762), into your project folder.

Load documents

Five loaders, five kinds of source. The first two read the files from the downloads above; the other three read from the internet. All five return the same thing.

Notebook cellPython
from langchain_community.document_loaders import TextLoader

loader = TextLoader("speech.txt")
text_documents = loader.load()
print(len(text_documents))
print(text_documents[0].metadata)
print(text_documents[0].page_content[:200])
OutputReal output
DeprecationWarning: `langchain-community` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-community/issues/674 for details and migration guidance toward standalone integration packages.
1
{'source': 'speech.txt'}
The world must be made safe for democracy. Its peace must be planted upon the tested foundations of political liberty. We have no selfish ends to serve. We desire no conquest, no dominion. We seek no

load() returns a list; a text file gives one Document, with the file's name as its source.

What a loader returns

Every loader returns a list of Document objects, each with two attributes:

Attribute Holds
page_content The text, as a string
metadata A dict about the text: at least its source; loaders add more, such as page numbers

Metadata travels with the text through splitting and search, so an answer can say where it came from (the script of lesson 6.8 prints them).

Loader Reads One Document per Package it uses
TextLoader A text file File -
PyPDFLoader A PDF Page pypdf
WebBaseLoader Web pages Address beautifulsoup4
ArxivLoader arXiv papers Paper arxiv, pymupdf
WikipediaLoader Wikipedia articles Article wikipedia
langchain-community is being sunset

LangChain stopped maintaining langchain-community in May 2026 and archived its repository in June: it gets no more fixes, and importing it prints the DeprecationWarning in the first output above. Its loaders still work, so this module uses them. For new code, prefer a standalone integration package where one exists, listed on LangChain's integrations page; most model providers and databases have one, such as langchain-openai and langchain-chroma. Where none exists, read the source with its own library and build the Documents yourself: Document belongs to langchain-core, which is maintained.

Notebook cellPython
from pypdf import PdfReader
from langchain_core.documents import Document

reader = PdfReader("attention.pdf")
pages = [
    Document(page_content=page.extract_text(), metadata={"source": "attention.pdf", "page": i})
    for i, page in enumerate(reader.pages)
]
len(pages), pages[0].metadata
OutputReal output
(15, {'source': 'attention.pdf', 'page': 0})

What loaders don't do

A loader extracts text; it doesn't check it. PyPDFLoader reads a PDF's text layer, so a scanned PDF without one loads as empty pages and needs text recognition (OCR) first, and tables or columns come out as plain lines, as on the paper's first page above. Look at page_content before indexing: text the loader misses, retrieval can never find.

Practice

Practice 1Find a section in the PDF. Load attention.pdf and print the page and page_label of the first page that mentions "Scaled Dot-Product Attention".Show solution
Notebook cellPython
pages = PyPDFLoader("attention.pdf").load()
first = next(doc for doc in pages if "Scaled Dot-Product Attention" in doc.page_content)
first.metadata["page"], first.metadata["page_label"]
OutputReal output
(3, '4')

next() with a generator expression stops at the first match (lesson 2.7). Page 3, counted from 0, is the fourth page, printed as 4.

Practice 2Only the title. Load the same blog post, keeping only the element with the class post-title.Show solution
Notebook cellPython
loader = WebBaseLoader(
    web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
    bs_kwargs=dict(parse_only=bs4.SoupStrainer(class_="post-title")),
)
loader.load()[0].page_content.strip()
OutputIllustrative
'LLM Powered Autonomous Agents'

class_ takes one class name or a tuple of them. strip() removes the blank lines and spaces around the title.