LangChain TextLoader: plain .txt into Document objects
LangChain TextLoader reads .txt files into Documents with source metadata and no chunking, and autodetect_encoding rescues Shift-JIS and other non-UTF-8 files.
This article is published in English.
Sources: How to Use TextLoader in LangChain to Load .txt File
In a RAG pipeline, loading is the first move. Sources arrive as .txt, .csv, .pdf, .md, and more. This note zooms in on TextLoader — LangChain’s loader aimed at plain .txt files.
A document loader reads a source and turns it into one or more Document objects. A Document is LangChain’s standard content unit. It has two fields:
- page_content — the text itself
- metadata — a dict with provenance (often just the path)
TextLoader takes a path, reads the file, and returns Documents. It does not chunk, clean, OCR, or transform. Loading only. Chunking belongs in a later splitter; cleaning belongs in a preprocessor. Keeping those stages separate makes each step easy to swap.
When TextLoader fits
- The source is plain text:
.txt, logs, exported notes, scraped dumps - You load one file or many files of the same text shape
- You only need basic metadata such as the file path
- You want the cheapest possible first step before embedding
When to pick something else
- Mixed formats (PDF, DOCX, HTML, CSV) in one pass
- You need page numbers, headings, sheet names, or authorship metadata
- The file is binary or containerized and needs a specialized parser
Experiment: from file to Document
1. Prepare a sample file
Save article prose as Sample.txt in the project so the loader has something concrete to read. Any UTF-8 text works; longer files make the “one Document per file” behavior more obvious.
2. Construct the loader
Note on Package Location
TextLoader is currently available through langchain_community.document_loaders.
However, LangChain is moving integrations into standalone packages, and this import path may change.
The import shown above works with langchain-community 0.4.2. For new projects, check the current LangChain documentation for the recommended import path.
Construction does not touch disk yet. TextLoader only stores the path (and optional encoding flags) you gave it. That lazy construction is useful when you build a list of loaders and run them later in a batch job.
3. Call .load()
load() reads the file and returns a list. Every LangChain loader returns a list, even when there is a single document. Downstream splitters and vector stores expect that list shape, so loaders stay consistent across file types.
4. Check list length
For Sample.txt the list has exactly one item. A plain text file always yields one Document, no matter how long the file is. Line breaks and paragraphs do not create extra Documents — the whole file is one block of page_content. If you need smaller units, add a text splitter after loading.
5. Inspect the Document
docs[0] is a Document. page_content holds the full text. metadata usually contains only source (the path). TextLoader does not invent titles, authors, or dates. If you need richer metadata, attach it yourself after load or choose a loader that extracts structure.
Constructor parameters
TextLoader(file_path, encoding=None, autodetect_encoding=False)
- file_path — path to the
.txt - encoding — explicit codec for reading (UTF-8, latin-1, …)
- autodetect_encoding — retry with a detected codec if the first decode fails
Encoding gotchas in mixed corpora
Omit encoding and TextLoader uses the platform default (typically UTF-8). A UTF-8 Sample.txt loads cleanly with no extra flags.
Create sample2.txt saved as Shift-JIS with a few Japanese lines:
今日は良い天気です。
私は毎日プログラミングを勉強しています。
Pythonを使って新しいアプリを作っています。
Loading without an encoding argument fails — a UTF-8 decode cannot handle Shift-JIS bytes. Pass autodetect_encoding=True. TextLoader uses chardet to guess the codec after the UTF-8 attempt fails, then retries with the guess. That matters when ingesting files from mixed or unknown sources where one bad encoding would abort the whole batch job.
Summary
TextLoader is the smallest useful loader for plain text in LangChain RAG: path in, list of Documents out, minimal metadata, optional encoding autodetection when your corpus is messy. Use it when the file is already text; graduate to richer loaders when structure or binary formats enter the picture.