Skip to main content
The Field Labeled CSV Reader converts CSV rows into field-labeled text documents, making them more readable for natural language processing and agent-based retrieval systems.

Code

cookbook/knowledge/readers/csv_field_labeled_reader.py
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.field_labeled_csv_reader import FieldLabeledCSVReader
from agno.vectordb.pgvector import PgVector

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

reader = FieldLabeledCSVReader(
    chunk_title="🎬 Movie Information",
    field_names=[
        "Movie Rank",
        "Movie Title",
        "Genre",
        "Description",
        "Director",
        "Actors",
        "Year",
        "Runtime (Minutes)",
        "Rating",
        "Votes",
        "Revenue (Millions)",
        "Metascore",
    ],
    format_headers=True,
    skip_empty_fields=True,
)

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

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

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
    instructions=[
        "You are a movie expert assistant.",
        "Use the search_knowledge_base tool to find detailed information about movies.",
        "The movie data is formatted in a field-labeled, human-readable way with clear field labels.",
        "Each movie entry starts with '🎬 Movie Information' followed by labeled fields.",
        "Provide comprehensive answers based on the movie information available.",
    ],
)

agent.print_response(
    "which movies are directed by Christopher Nolan", markdown=True, stream=True
)

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
2

Install libraries

pip install -U agno aiofiles sqlalchemy psycopg[binary] pgvector openai
3

Set environment variables

export OPENAI_API_KEY=xxx
4

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  agno/pgvector:16
5

Run Agent

python cookbook/knowledge/readers/csv_field_labeled_reader.py

Params

ParameterTypeDefaultDescription
fileUnion[Path, IO[Any]]RequiredPath to CSV file or file-like object
chunk_titleOptional[Union[str, List[str]]]NoneTitle to add at the top of each entry. Can be a single string or a list that rotates through entries
field_namesOptional[List[str]][]Custom labels for CSV fields. If not provided, column headers are used
format_headersboolTrueWhether to format column headers by replacing underscores with spaces and applying title case
skip_empty_fieldsboolTrueWhether to skip fields with empty values in the output
delimiterstr","Character used to separate fields in the CSV
quotecharstr'"'Character used to quote fields in the CSV
encodingOptional[str]"utf-8"Character encoding for reading the file
I