Agent cannot read my excel file path

Hi everyone :waving_hand:

I’ve recently started working on a CrewAI project with 3 agents:

  • a data loader agent with a loading task (to handle CSV, Excel, JSON files),

  • an analyst agent,

  • and a visualization agent.

I’m using Gemini as the LLM.

:backhand_index_pointing_right: 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 data folder.

  • 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

:cross_mark: 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.

:white_check_mark: 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.

:backhand_index_pointing_right: 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 :folded_hands:

Hey, Hanen.

I understand you’re asking how to get your Agent to use the tool with the correct file path, and I’ll be upfront: my answer doesn’t directly address that specific issue, okay?

But I wanted to bring up two other points:

  1. Anything you can do yourself in a predictable and reproducible way, you should. Avoid delegating trivial, deterministic tasks to an LLM. You didn’t provide the full picture of what your first Agent does, but if its only job is to read a file’s content based on its extension, just implement all that logic in plain old Python. Open the file, read the content, and then simply pass that content on to the Agent responsible for the analysis.
  2. Whatever you pass to an Agent, remember that it needs to be text or something that can be serialized into text. At the end of the day, all that CrewAI does (and really, any agent framework) is generate and manage prompts. Text prompts, you know? So, when you generate a pandas.DataFrame, for example, I suggest converting that information into a format that’s more easily digestible for the LLM, like a JSON string. Make it a habit to communicate with the LLM by always treating everything as text that will be passed in a prompt.

Assalamualaikum.

You can directly use FileReadTool from crewai_tools by giving file_path as a parameter.

from crewai_tools import FileReadTool
reader_tool = FileReadTool(file_path=’<your file path>’)
agent = Agent(
,tools = [reader_tool]
)