Phidata CsvTools: keep getting error with query_csv_file

Hi all,

First of all thanks for reading this topic and thinking with me :slight_smile:

I’ve built an agent which should read a csv file with Phidata CsvTools.

The example CSV file with IMDB movies works, but any other CSV file I use doesn’t work. From the errors from Phidata I understand Phidata is able to read the file using read_csv_file(), but cannot execute SQL queries with its default method query_csv_file().

Already tried changing the headers of the CSV file, removing spaces from the column names, changing list seperators, etc. But none of it works…

Query: whats the phone number for Lisa Cain?

Terminal:

 😎 User : whats the phone number for Lisa Cain?
INFO     Reading columns from file: clients
INFO     Loading csv file: clients
ERROR    Error querying csv: Parser Error: syntax error at or near "s"
INFO     Loading csv file: clients
ERROR    Error querying csv: Parser Error: syntax error at or near "s"
INFO     Loading csv file: clients
ERROR    Error querying csv: Parser Error: syntax error at or near "s"
INFO     Reading file: clients

Response:

Running:

• list_csv_files()

Running:

• get_columns(csv_name=clients)

Running:

• query_csv_file(csv_name=clients, sql_query=SELECT "Phone number" FROM clients WHERE "First name" = 'Lisa' AND "Last name" = 'Cain')

Running:

• query_csv_file(csv_name=clients, sql_query=SELECT "Phone number" FROM clients WHERE First name = 'Lisa' AND Last name = 'Cain')

It seems there's an issue with the column naming or syntax in the query. Let me adjust the query and try again.
Running:

• query_csv_file(csv_name=clients, sql_query=SELECT "Phone number" FROM clients WHERE "First name" = 'Lisa' AND "Last name" = 'Cain')

I encountered a persistent error while querying the CSV file with standard SQL syntax. This might be due to handling issues with spaces and special characters in column names. Let me try a different approach.

I will reattempt querying the CSV to obtain the information. Please hold on.
Running:

• read_csv_file(csv_name=clients)

The phone number for Lisa Cain is 001-862-692-7436.

The code

import httpx
from pathlib import Path
from phi.agent import Agent
from phi.tools.csv_tools import CsvTools
import os

from dotenv import load_dotenv

load_dotenv()



customers_csv = Path(__file__).parent.joinpath("wip").joinpath("clients.csv")

if not os.path.exists(customers_csv):
    url = "https://tomstechacademy.com/wp-content/uploads/2025/01/clients.csv"

    response = httpx.get(url)

    customers_csv.parent.mkdir(parents=True, exist_ok=True)
    customers_csv.write_bytes(response.content)



agent = Agent(
    name="Customer DB Agent",
    tools=[CsvTools(csvs=[customers_csv])],
    markdown=True,
    show_tool_calls=True,
    instructions=[
        "First always get the list of files",
        "Then check the columns in the file",
        "Then run the query to answer the question",
    ],
)


agent.cli_app(stream=False)

Hello @Thom !

Thank you for sharing your code and the Agent output. Looks like there is an issue with the syntax being provided to query_csv_file function. Can you please set debug_mode=True in your Agent config and share the results of the tool calls? This would help me dive deeper into the problem.

Also confirming whether you are using OpenAI as the model

Hi Yash,

Thanks for your reply.

I’m indeed using OpenAI as the model.

Please find below the result of the tool calls:

Thanks in advanced for your help.

DEBUG    Debug logs enabled
 😎 User : Whats the phone number of Lisa Cain?
DEBUG    *********** Agent Run Start: c7c509ad-9e68-4a0e-ac04-a95e38d93d49 ***********
DEBUG    Function read_csv_file from csv_tools added to model.
DEBUG    Function list_csv_files from csv_tools added to model.
DEBUG    Function get_columns from csv_tools added to model.
DEBUG    Function query_csv_file from csv_tools added to model.
DEBUG    ---------- OpenAI Response Start ----------
DEBUG    ============== system ==============
DEBUG    ## Instructions
         - First always get the list of files
         - Then check the columns in the file
         - Then run the query to answer the question
         - Use markdown to format your answers.
DEBUG    ============== user ==============
DEBUG    Whats the phone number of Lisa Cain?
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_uKrJmMyUGktsIMYSX4cMhCES",
             "function": {
               "arguments": "{}",
               "name": "list_csv_files"
             },
             "type": "function"
           }
         ]
DEBUG    **************** METRICS START ****************
DEBUG    * Time to generate response:   0.7512s
DEBUG    * Tokens per second:           15.9734 tokens/s
DEBUG    * Input tokens:                388
DEBUG    * Output tokens:               12
DEBUG    * Total tokens:                400
DEBUG    * Prompt tokens details:       {'audio_tokens': 0, 'cached_tokens': 0}
DEBUG    * Completion tokens details:   {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens':
         0, 'rejected_prediction_tokens': 0}
DEBUG    **************** METRICS END ******************
DEBUG    Getting function list_csv_files
DEBUG    Running: list_csv_files()
DEBUG    ---------- OpenAI Response Start ----------
DEBUG    ============== system ==============
DEBUG    ## Instructions
         - First always get the list of files
         - Then check the columns in the file
         - Then run the query to answer the question
         - Use markdown to format your answers.
DEBUG    ============== user ==============
DEBUG    Whats the phone number of Lisa Cain?
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_uKrJmMyUGktsIMYSX4cMhCES",
             "function": {
               "arguments": "{}",
               "name": "list_csv_files"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_uKrJmMyUGktsIMYSX4cMhCES
DEBUG    ["clients"]
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_YUxln9LSLYNCnaJVXEQ3jl17",
             "function": {
               "arguments": "{\"csv_name\":\"clients\"}",
               "name": "get_columns"
             },
             "type": "function"
           }
         ]
DEBUG    **************** METRICS START ****************
DEBUG    * Time to generate response:   0.7334s
DEBUG    * Tokens per second:           21.8176 tokens/s
DEBUG    * Input tokens:                411
DEBUG    * Output tokens:               16
DEBUG    * Total tokens:                427
DEBUG    * Prompt tokens details:       {'audio_tokens': 0, 'cached_tokens': 0}
DEBUG    * Completion tokens details:   {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens':
         0, 'rejected_prediction_tokens': 0}
DEBUG    **************** METRICS END ******************
DEBUG    Getting function get_columns
DEBUG    Running: get_columns(csv_name=clients)
INFO     Reading columns from file: clients
DEBUG    ---------- OpenAI Response Start ----------
DEBUG    ============== system ==============
DEBUG    ## Instructions
         - First always get the list of files
         - Then check the columns in the file
         - Then run the query to answer the question
         - Use markdown to format your answers.
DEBUG    ============== user ==============
DEBUG    Whats the phone number of Lisa Cain?
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_uKrJmMyUGktsIMYSX4cMhCES",
             "function": {
               "arguments": "{}",
               "name": "list_csv_files"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_uKrJmMyUGktsIMYSX4cMhCES
DEBUG    ["clients"]
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_YUxln9LSLYNCnaJVXEQ3jl17",
             "function": {
               "arguments": "{\"csv_name\":\"clients\"}",
               "name": "get_columns"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_YUxln9LSLYNCnaJVXEQ3jl17
DEBUG    ["Customer ID", "First name", "Last name", "Address", "Zipcode", "City", "Country", "Phone number"]    
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_yHHAis0YURluwhpscdZCd3PA",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM clients 
         WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]
DEBUG    **************** METRICS START ****************
DEBUG    * Time to generate response:   0.9566s
DEBUG    * Tokens per second:           48.0873 tokens/s
DEBUG    * Input tokens:                463
DEBUG    * Output tokens:               46
DEBUG    * Total tokens:                509
DEBUG    * Prompt tokens details:       {'audio_tokens': 0, 'cached_tokens': 0}
DEBUG    * Completion tokens details:   {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens':
         0, 'rejected_prediction_tokens': 0}
DEBUG    **************** METRICS END ******************
DEBUG    Getting function query_csv_file
DEBUG    Running: query_csv_file(csv_name=clients, sql_query=SELECT "Phone number" FROM clients WHERE "First    
         name" = 'Lisa' AND "Last name" = 'Cain')
INFO     Loading csv file: clients
ERROR    Error querying csv: Parser Error: syntax error at or near "s"
DEBUG    ---------- OpenAI Response Start ----------
DEBUG    ============== system ==============
DEBUG    ## Instructions
         - First always get the list of files
         - Then check the columns in the file
         - Then run the query to answer the question
         - Use markdown to format your answers.
DEBUG    ============== user ==============
DEBUG    Whats the phone number of Lisa Cain?
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_uKrJmMyUGktsIMYSX4cMhCES",
             "function": {
               "arguments": "{}",
               "name": "list_csv_files"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_uKrJmMyUGktsIMYSX4cMhCES
DEBUG    ["clients"]
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_YUxln9LSLYNCnaJVXEQ3jl17",
             "function": {
               "arguments": "{\"csv_name\":\"clients\"}",
               "name": "get_columns"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_YUxln9LSLYNCnaJVXEQ3jl17
DEBUG    ["Customer ID", "First name", "Last name", "Address", "Zipcode", "City", "Country", "Phone number"]    
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_yHHAis0YURluwhpscdZCd3PA",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM clients 
         WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_yHHAis0YURluwhpscdZCd3PA
DEBUG    Error querying csv: Parser Error: syntax error at or near "s"
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_7kyuZLSrMJ0ronz5Q2KK141P",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM
         \\\"clients\\\" WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]
DEBUG    **************** METRICS START ****************
DEBUG    * Time to generate response:   1.1083s
DEBUG    * Tokens per second:           43.3100 tokens/s
DEBUG    * Input tokens:                532
DEBUG    * Output tokens:               48
DEBUG    * Total tokens:                580
DEBUG    * Prompt tokens details:       {'audio_tokens': 0, 'cached_tokens': 0}
DEBUG    * Completion tokens details:   {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens':
         0, 'rejected_prediction_tokens': 0}
DEBUG    **************** METRICS END ******************
DEBUG    Getting function query_csv_file
DEBUG    Running: query_csv_file(csv_name=clients, sql_query=SELECT "Phone number" FROM "clients" WHERE "First  
         name" = 'Lisa' AND "Last name" = 'Cain')
INFO     Loading csv file: clients
ERROR    Error querying csv: Parser Error: syntax error at or near "s"
DEBUG    ---------- OpenAI Response Start ----------
DEBUG    ============== system ==============
DEBUG    ## Instructions
         - First always get the list of files
         - Then check the columns in the file
         - Then run the query to answer the question
         - Use markdown to format your answers.
DEBUG    ============== user ==============
DEBUG    Whats the phone number of Lisa Cain?
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_uKrJmMyUGktsIMYSX4cMhCES",
             "function": {
               "arguments": "{}",
               "name": "list_csv_files"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_uKrJmMyUGktsIMYSX4cMhCES
DEBUG    ["clients"]
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_YUxln9LSLYNCnaJVXEQ3jl17",
             "function": {
               "arguments": "{\"csv_name\":\"clients\"}",
               "name": "get_columns"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_YUxln9LSLYNCnaJVXEQ3jl17
DEBUG    ["Customer ID", "First name", "Last name", "Address", "Zipcode", "City", "Country", "Phone number"]    
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_yHHAis0YURluwhpscdZCd3PA",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM clients 
         WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_yHHAis0YURluwhpscdZCd3PA
DEBUG    Error querying csv: Parser Error: syntax error at or near "s"
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_7kyuZLSrMJ0ronz5Q2KK141P",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM
         \\\"clients\\\" WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]
DEBUG    ============== tool ==============
DEBUG    Tool call Id: call_7kyuZLSrMJ0ronz5Q2KK141P
DEBUG    Error querying csv: Parser Error: syntax error at or near "s"
DEBUG    ============== assistant ==============
DEBUG    Tool Calls: [
           {
             "id": "call_vvB4f3wxXUti8t5y87JX8t3C",
             "function": {
               "arguments": "{\"csv_name\":\"clients\",\"sql_query\":\"SELECT \\\"Phone number\\\" FROM clients 
         WHERE \\\"First name\\\" = 'Lisa' AND \\\"Last name\\\" = 'Cain'\"}",
               "name": "query_csv_file"
             },
             "type": "function"
           }
         ]

Apologies, I had to cut a part off, as the number of characters I can share is restricted.

Hello @Thom !

Thanks for sharing the debug logs. Looks like the Agent is getting the formatting for input params wrong for the SQL query.

Could you please add the following instructions to your Agent config:

Always wrap column names with double quotes if they contain spaces or special characters
Remember to escape the quotes in the JSON string (use \")
Use single quotes for string values

Please let me know if this works.