"""Dependency-light text and Markdown file loader."""
from langchain_core.documents import Document
from .base import BaseFileLoader
[docs]
class TextFileLoader(BaseFileLoader):
"""Load UTF-8 or explicitly encoded plain-text and Markdown files."""
[docs]
def load(self) -> list[Document]:
"""Read the file and return one LangChain document."""
encoding = self.kwargs.get("encoding", "utf-8")
try:
content = self.file_path.read_text(encoding=encoding)
except (OSError, UnicodeError) as exc:
raise RuntimeError(
f"Failed to load text file {self.file_path}: {exc}"
) from exc
return [
Document(page_content=content, metadata={"source": str(self.file_path)})
]
[docs]
@staticmethod
def get_install_command() -> str:
"""Return the installation command for this built-in loader."""
return "No additional dependencies required."