CSV Row Chunking

Split CSV files into one chunk per row with RowChunking.

CSVReader parses the records, then RowChunking creates one chunk for each non-empty normalized row and records its logical row number in metadata.

Create a Python file

from agno.agent import Agent
from agno.knowledge.chunking.row import RowChunking
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.csv_reader import CSVReader
from agno.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = Knowledge(
    vector_db=PgVector(table_name="imdb_movies_row_chunking", db_url=db_url),
)

knowledge_base.insert(
    url="https://agno-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",
    reader=CSVReader(
        chunking_strategy=RowChunking(),
    ),
)

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent.print_response("Tell me about the movie Guardians of the Galaxy", markdown=True)

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno sqlalchemy psycopg pgvector aiofiles openai

Export your OpenAI API key

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.

export OPENAI_API_KEY=sk-***

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Run the script

python csv_row_chunking.py

CSV Row Chunking Params

ParameterTypeDefaultDescription
skip_headerboolFalseSkip the first row before chunking. Row numbering in chunk metadata still starts from the original file's row positions.
clean_rowsboolTrueNormalize internal whitespace in each row. When False, rows are only stripped of leading and trailing whitespace.

Developer Resources