Document Loaders
Relevant source files
The following files were used as context for generating this wiki page:
- libs/langchain/langchain/document_loaders/base.py
- libs/langchain/langchain/document_loaders/pdf.py
- libs/langchain/langchain/document_loaders/web_base.py
- libs/langchain/langchain/document_loaders/directory.py
- libs/langchain/langchain/document_loaders/init.py
- libs/langchain/tests/unit_tests/document_loaders/test_imports.py
- libs/langchain/README.md
Document Loaders are a fundamental component of the LangChain framework, designed to ingest data from a wide variety of sources and represent it as Document objects. Their primary purpose is to provide a standardized interface for loading text data, which is often the first step in building powerful applications like those for Retrieval Augmented Generation (RAG). By abstracting the data loading process, developers can easily switch between different data sources without changing the downstream logic of their application.
This module contains a vast collection of loaders for sources ranging from simple text files and web pages to complex databases and third-party applications. The core design emphasizes modularity and ease of use, enabling seamless integration of external data into LangChain workflows.
Sources: libs/langchain/langchain/document_loaders/init.py:1-5, libs/langchain/README.md:21-25
Core Concepts
The Document Loader system is built around a few key abstractions that ensure consistency and extensibility.
BaseLoader
The BaseLoader is an abstract class that defines the common interface for all document loaders. Every specific loader in the framework inherits from this class. It establishes two primary methods for loading documents: load for eager loading and lazy_load for lazy loading.
This diagram illustrates the abstract methods of the BaseLoader class.
lazy_load(): This abstract method returns an iterator that yieldsDocumentobjects one by one. This is useful for memory-intensive operations where loading all documents at once is not feasible. Subclasses must implement this method.load(): This method loads all documents from the source into a list in memory. It is a concrete method that internally callslazy_load()and collects the results.load_and_split(): This convenience method loads documents and immediately splits them into smaller chunks using a providedTextSplitter, returning a list of the resultingDocumentchunks.
Sources: libs/langchain/langchain/document_loaders/base.py:12-46
Blob and BlobLoader
The Blob and BlobLoader are more recent, flexible abstractions for handling data.
Blob: ABlobis a data container that holds data in memory and includes associated metadata. It can be initialized from various sources like files or in-memory data.BlobLoader: This is an abstract class for loaders that yieldBlobobjects. Its primary method islazy_load(), which returns an iterator ofBlobs.
Sources: libs/langchain/langchain/document_loaders/init.py:199-200
Available Loaders
The langchain package provides an extensive and ever-growing list of document loaders. These loaders are dynamically imported, and many have been migrated to the langchain_community package to improve modularity, while maintaining backward compatibility.
The following table shows a small sample of the diverse loaders available:
| Loader Class | Description | Source Package |
|---|---|---|
TextLoader | Loads a text file. | langchain_community.document_loaders |
PyPDFLoader | Loads a PDF file using the pypdf library. | langchain_community.document_loaders |
WebBaseLoader | Loads content from web pages. | langchain_community.document_loaders |
DirectoryLoader | Loads all documents from a directory. | langchain_community.document_loaders |
CSVLoader | Loads data from a CSV file. | langchain_community.document_loaders |
JSONLoader | Loads data from a JSON file. | langchain_community.document_loaders |
UnstructuredFileLoader | Loads various file types using the unstructured library. | langchain_community.document_loaders |
BigQueryLoader | Loads data from Google BigQuery. | langchain_community.document_loaders |
GitLoader | Loads files from a Git repository. | langchain_community.document_loaders |
YoutubeLoader | Loads transcripts from YouTube videos. | langchain_community.document_loaders |
Sources: libs/langchain/langchain/document_loaders/init.py:12-197, libs/langchain/tests/unit_tests/document_loaders/test_imports.py:3-177
Dynamic Imports and Deprecation
To manage the large number of integrations and improve dependency management, many loaders have been moved from langchain to langchain_community. The framework uses a dynamic importer to handle this transition gracefully.
When a loader is imported from langchain.document_loaders, the __getattr__ function checks a DEPRECATED_LOOKUP dictionary. If the loader is listed, it is imported from langchain_community and a DeprecationWarning is raised. This ensures that older code continues to work while encouraging developers to update their import paths.
This flowchart shows the dynamic import logic for a document loader.
Example Implementations
Below are detailed explanations of a few common document loaders.
DirectoryLoader
The DirectoryLoader is a composite loader that loads all files from a specified directory. It uses another loader class (loader_cls) to handle the actual parsing of each individual file.
Key Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
path | str | The path to the directory. | - |
glob | str | A glob pattern to select which files to load. | "**/[!.]*" |
silent_errors | bool | If True, errors during file loading are ignored. | False |
loader_cls | Type[BaseLoader] | The loader class to use for each file. | UnstructuredFileLoader |
loader_kwargs | Dict | Keyword arguments to pass to loader_cls. | None |
recursive | bool | Whether to search for files recursively. | False |
show_progress | bool | Whether to show a progress bar. | False |
Sources: libs/langchain/langchain/document_loaders/directory.py:21-42
Loading Process
The following diagram illustrates the process flow of the DirectoryLoader.
WebBaseLoader
The WebBaseLoader is designed to fetch and parse content from web pages. It takes a list of URLs, retrieves their HTML content, and parses it to extract the main text.
This sequence diagram shows the interaction for loading a web page.
It uses the requests library for HTTP requests and by default relies on BeautifulSoup for parsing, although the parsing logic is encapsulated in the _scrape method, which can be customized.
Sources: libs/langchain/langchain/document_loaders/web_base.py:16-18, 70-78
PyPDFLoader
The PyPDFLoader specializes in loading data from PDF files. It uses the pypdf library to open and read the content of a PDF. Each page of the PDF is typically loaded as a separate Document object, with metadata indicating the source file and page number.
# Example usage of PyPDFLoader
from langchain.document_loaders import PyPDFLoader
loader = PyPDFLoader("example.pdf")
pages = loader.load_and_split()
Sources: libs/langchain/langchain/document_loaders/pdf.py:12-40
The loader iterates through the pages of the PDF, extracts the text content from each page, and creates a Document object. The metadata for each document includes the source (file path) and the 0-indexed page number.
Sources: libs/langchain/langchain/document_loaders/pdf.py:29-38
Conclusion
Document Loaders are a critical entry point for data in the LangChain ecosystem. They provide a robust and standardized mechanism for ingesting information from a vast array of sources. The simple BaseLoader interface, combined with a rich library of concrete implementations, allows developers to quickly and efficiently connect their LLM applications to the data they need, forming the foundation for advanced capabilities like Retrieval Augmented Generation. The modular design, including the migration of community-maintained loaders, ensures the framework remains clean, scalable, and easy to contribute to.
Sources: libs/langchain/langchain/document_loaders/init.py, libs/langchain/README.md