I am working on a project with an agent designed to read the text from a file and print to terminal (for now). I’ve gone through and set it up as I though it should based off the tutorials:
in Crew.py: #initializing the tool: file_read_tool = FileReadTool(file_path='./tests/testFile.txt')
#adding the new agent that uses the tool: @agent def fileReadTool(self) -> Agent: return Agent( config=self.agents_config['fileReadTool'], # type: ignore[index] verbose=True, tools=file_read_tool )
I have also specified the Agent to use the file_read_tool.
When I attempt to run it, I get a number of errors but don’t actually specify what is wrong:
Traceback (most recent call last):
File "...PycharmProjects/DetoxBot/detoxbot/src/detoxbot/main.py”, line 26, in run
Detoxbot().crew().kickoff(inputs=inputs)
~~~~~~~~^^
File “...PycharmProjects/DetoxBot/detoxbot/.venv/lib/python3.13/site-packages/crewai/project/crew_base.py”, line 39, in init
self.map_all_agent_variables()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File “...PycharmProjects/DetoxBot/detoxbot/.venv/lib/python3.13/site-packages/crewai/project/crew_base.py”, line 181, in map_all_agent_variables
self._map_agent_variables(
~~~~~~~~~~~~~~~~~~~~~~~~~^
agent_name,
^^^^^^^^^^^
…<4 lines>…
callbacks,
^^^^^^^^^^
)
^
File “...PycharmProjects/DetoxBot/detoxbot/.venv/lib/python3.13/site-packages/crewai/project/crew_base.py”, line 207, in _map_agent_variables
tool_functionstool for tool in tools
~~~~~~~~~~~~~~^^^^^^
KeyError: ‘f’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “...PycharmProjects/DetoxBot/detoxbot/.venv/bin/run_crew”, line 10, in
sys.exit(run())
~~~^^
File “...PycharmProjects/DetoxBot/detoxbot/src/detoxbot/main.py”, line 28, in run
raise Exception(f"An error occurred while running the crew: {e}")
Exception: An error occurred while running the crew: ‘f’
An error occurred while running the crew: Command ‘[‘uv’, ‘run’, ‘run_crew’]’ returned non-zero exit status 1.
Any suggestions about what I can check to ensure using tools isn’t throwing an error?
The error you’re encountering is a common issue with CrewAI when passing tools to agents. The problem is in how you’re passing the tool to the agent.
The Issue
In your code you have
tools=file_read_tool
You’re passing a single tool instance directly, but CrewAI expects tools to be passed as a list (even if there’s only one tool).
The KeyError: 'f' happens because CrewAI tries to iterate over what it thinks is a collection of tools, but instead it’s iterating over the characters of the tool object’s string representation.
The Solution
Change your agent definition to wrap the tool in a list:
python
@agent
def fileReadTool(self) -> Agent:
return Agent(
config=self.agents_config['fileReadTool'],
verbose=True,
tools=[file_read_tool] # ← Wrap in a list!
)
Try making that change and the error should be resolved! Let me know if you encounter any other issues.
I’m still getting errors, but now I’m getting: Exception: An error occurred while running the crew: ‘[’ so I think I’m still missing a [ ] wrapper somewhere? Thanks!
The problem is in your agents.yaml file. You don’t need to (and shouldn’t) specify tools in the YAML config file when you’re already passing them programmatically in the Python code.
Pass tools in your Python code (what you’re doing now):