Skip to main content
This lesson shows how to convert a plain CSV file into RAG-ready documents suitable for a retriever. The aim is to produce one clean, searchable document per CSV row with:
  • A human-readable text field (concatenated non-empty columns),
  • A metadata object that preserves the original CSV fields and source information,
  • An optional JSON export that you can ingest into a vector store or retriever pipeline.
We’ll provide a single, self-contained Python script that:
  • reads a CSV file,
  • converts each row into a document with id, text, and metadata,
  • optionally writes the list of documents to a JSON file for ingestion.
CSV quirks to watch for: headers may include leading/trailing whitespace, values may be empty, and fields can contain commas or newlines. The script below handles common cases (skips empty values, uses UTF-8). For inconsistent or complex CSV sources, add normalization (trim headers, unify casing) or a preprocessor that handles quoting and encodings.

Setup (optional virtual environment)

Create and activate a virtual environment if you prefer to isolate dependencies:

main.py — CSV → RAG document converter

Create a file named main.py and paste the following code. This single script reads sample_data.csv (or another CSV you set) and creates a list of documents ready for ingestion.

Document schema (what each parsed item contains)

How the parser works (summary)

  • Uses csv.DictReader to map header names to values per row.
  • For each row:
    • Builds a text string by concatenating non-empty key: value pairs separated by | for easy searchability.
    • Copies all original CSV fields into metadata (preserving string values).
    • Adds source and row_number to metadata.
    • Generates a top-level document id using the doc_{row_index} pattern.
  • Appends each document to a list and optionally writes the full list to a JSON file for ingestion.

Run the script

Place your CSV (for example, sample_data.csv) in the same directory as main.py. Then run:
On success you should see the columns found, the processed document count, and a sample output. Example console output:

Notes, tips, and next steps

  • One CSV row maps to one RAG “chunk” (document). If you need different chunking strategies (e.g., split long text fields or combine rows), update parse_csv_for_rag accordingly.
  • Header names are used as-is. Normalize column names (trim whitespace, lower-case, replace spaces) if you have inconsistent sources.
  • The script generates a top-level id and will also include any id column from the CSV inside metadata. Avoid naming collisions if your downstream store expects a single unique id.
  • For larger CSVs (tens of thousands of rows), consider streaming rows and writing documents incrementally (or sending them directly to your vector store) to avoid high memory use.
  • After generating rag_documents.json, ingest it into your vector database/retriever using the connector or ingestion script supported by your vector store.
If your CSV contains nested JSON, fields with embedded commas, or multi-line values, ensure fields are properly quoted. For complex inputs, use a robust CSV library or preprocessor that properly handles quoting, escaping, and multi-line fields to avoid corrupted records.
This parser offers a straightforward, reproducible method to convert tabular CSV data into RAG-ready documents with both human-readable text and full metadata preservation.

Watch Video