Python / Python Modern Generative AI and Agents Interview Questions
What document loaders does LangChain provide, and how do you handle different file types in a RAG pipeline?
A RAG system is only as good as the documents it can ingest. LangChain provides over 100 document loaders for web pages, PDFs, Word files, databases, code repositories, spreadsheets, and cloud storage. Every loader returns a list of Document objects with page_content (the text) and metadata (source, page number, etc.).
from langchain_community.document_loaders import (
PyPDFLoader,
UnstructuredWordDocumentLoader,
WebBaseLoader,
CSVLoader,
DirectoryLoader,
GitLoader,
)
# ── PDF (page-by-page)
pdf_loader = PyPDFLoader('report.pdf')
pdf_docs = pdf_loader.load() # list of Document, one per page
print(pdf_docs[0].page_content[:200])
print(pdf_docs[0].metadata) # {'source': 'report.pdf', 'page': 0}
# ── Web page
web_loader = WebBaseLoader(
web_paths=['https://lilianweng.github.io/posts/2023-06-23-agent/'],
bs_kwargs={'features': 'html.parser'},
)
web_docs = web_loader.load()
# ── CSV with custom column for content
csv_loader = CSVLoader(
file_path='products.csv',
content_columns=['description'],
metadata_columns=['id', 'category'],
)
csv_docs = csv_loader.load()
# ── Load an entire directory (auto-detect file types)
dir_loader = DirectoryLoader(
'./docs',
glob='**/*.pdf', # only PDF files
loader_cls=PyPDFLoader,
show_progress=True,
use_multithreading=True,
)
all_docs = dir_loader.load()
# ── Code repository
git_loader = GitLoader(
repo_path='/local/path/to/repo',
branch='main',
file_filter=lambda path: path.endswith('.py'),
)
code_docs = git_loader.load()
# After loading, split all docs the same way regardless of source
from langchain_text_splitters import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(all_docs)
print(f'Total chunks: {len(chunks)}')
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
