How to beginner · 3 min read

How to load CSV file with LangChain

Quick answer
Use LangChain's CSVLoader from langchain_community.document_loaders to load CSV files easily. Instantiate CSVLoader with the file path, then call load() to get documents ready for processing.

PREREQUISITES

  • Python 3.8+
  • pip install langchain langchain_community pandas
  • OpenAI API key (free tier works) if using LangChain with OpenAI models

Setup

Install the necessary packages with pip and set your environment variables for API keys if you plan to use LangChain with AI models.

bash
pip install langchain langchain_community pandas

Step by step

Use CSVLoader from langchain_community.document_loaders to load CSV files into LangChain documents. This example loads a CSV and prints the first document's content.

python
import os
from langchain_community.document_loaders import CSVLoader

# Path to your CSV file
csv_path = "./data/sample.csv"

# Initialize the CSVLoader
loader = CSVLoader(file_path=csv_path)

# Load documents from CSV
documents = loader.load()

# Print the first document content
print(documents[0].page_content)
output
name,age,city
Alice,30,New York

Common variations

You can customize CSVLoader by specifying encoding or csv_args for delimiter and quoting. For async workflows, use async-compatible loaders or wrap in async functions. You can also combine CSV data with LangChain chains or embeddings.

python
from langchain_community.document_loaders import CSVLoader

loader = CSVLoader(
    file_path="./data/sample.csv",
    encoding="utf-8",
    csv_args={"delimiter": ";"}  # For semicolon-separated CSV
)
documents = loader.load()
print(documents[0].page_content)
output
name;age;city
Alice;30;New York

Troubleshooting

  • If you see FileNotFoundError, verify the CSV file path is correct.
  • If documents are empty, check CSV formatting and delimiter settings.
  • For encoding errors, specify the correct encoding parameter.

Key Takeaways

  • Use CSVLoader from langchain_community.document_loaders to load CSV files efficiently.
  • Customize CSV parsing with csv_args for delimiters and quoting as needed.
  • Always verify file paths and encoding to avoid common loading errors.
Verified 2026-04
Verify ↗