Hi everyone ![]()
I’ve recently started working on a CrewAI project with 3 agents:
-
a data loader agent with a
loadingtask (to handle CSV, Excel, JSON files), -
an analyst agent,
-
and a visualization agent.
I’m using Gemini as the LLM.
My main issue is with the loader agent. It doesn’t properly read the file_path I provide.
-
I placed my test file (Excel) inside a
datafolder. -
I set up an absolute path for it.
-
I even created a custom tool (
FileLoaderTool) to handle the file loading and generate metadata.
Here’s a simplified version of my tool:
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
import pandas as pd
import os
import json
class FileLoaderToolInput(BaseModel):
file_path: str = Field(
...,
description="Absolute or relative path to the dataset file (CSV, Excel,)"
)
class FileLoaderTool(BaseTool):
name: str = "FileLoaderTool"
description: str = "Load CSV, Excel, or JSON files and generate metadata"
args_schema = FileLoaderToolInput
def _run(self, file_path: str):
print("[DEBUG] FileLoaderTool._run called with:", file_path)
if not file_path or not os.path.exists(file_path):
return {"error": f"File not found: {file_path}"}
_, ext = os.path.splitext(file_path)
ext = ext.lower()
try:
if ext == ".csv":
df = pd.read_csv(file_path)
metadata = self.generate_metadata(file_path, df)
return {"data": df, "metadata": metadata}
elif ext in [".xls", ".xlsx"]:
excel_file = pd.ExcelFile(file_path)
sheet_data = {}
sheets_metadata = []
for sheet_name in excel_file.sheet_names:
df_sheet = excel_file.parse(sheet_name)
sheet_data[sheet_name] = df_sheet
sheets_metadata.append({
"name": sheet_name,
"n_rows": df_sheet.shape[0],
"n_cols": df_sheet.shape[1]
})
metadata = self.generate_metadata(file_path, None, sheets_metadata)
return {"data": sheet_data, "metadata": metadata}
elif ext == ".json":
with open(file_path, 'r') as f:
data = json.load(f)
df = pd.json_normalize(data)
metadata = self.generate_metadata(file_path, df)
return {"data": df, "metadata": metadata}
else:
return {"error": f"Unsupported file type: {ext}"}
except Exception as e:
return {"error": str(e)}
def generate_metadata(self, filepath, df=None, sheets_metadata=None):
metadata = {
"file_name": os.path.basename(filepath),
"file_type": filepath.split(".")[-1].lower()
}
if sheets_metadata:
metadata["n_sheets"] = len(sheets_metadata)
metadata["sheets"] = sheets_metadata
if df is not None:
columns_metadata = []
for col in df.columns:
columns_metadata.append({
"name": col,
"dtype": str(df[col].dtype),
"missing_count": df[col].isnull().sum()
})
metadata["columns"] = columns_metadata
return metadata
The problem: when I run my script with the loader agent, I often get errors like “File not found”, even though the file path is correct.
Sometimes, the agent even hallucinates paths and invents filenames that don’t exist.
What I’d like:
-
The loader agent should directly read the file I provide (without hallucinating a path).
-
Then pass the loaded data + generated metadata to the Analyst agent for the overview.
Question:
How can I reliably make the agent use the provided file_path when running the task, instead of inventing or ignoring it?
Is there a recommended pattern for combining a custom tool with agent tasks in CrewAI to avoid this?
Thanks a lot ![]()