I have defined and used the following tool
class JokerToolInput(BaseModel):
"""Topic to get jokes about."""
argument: str = Field(..., description="Topic to get jokes about, e.g. 'dogs'")
count: int = Field(
1, description="Number of jokes to return, default is 1", ge=2, le=10
)
class JokerResponse(BaseModel):
"""Response from the Joker API."""
response: list[str] = Field(..., description="JSON string with the joke data")
class JokerTool(BaseTool):
name: str = "JokerTool"
description: str = (
"Use this tool to get a list of jokes from the Joker API given a topic."
)
args_schema: Type[BaseModel] = JokerToolInput
def _run(self, argument: str, count: int = 1) -> JokerResponse:
try:
# JokesAPI endpoint - filters out explicit content
url = "https://v2.jokeapi.dev/joke/Any"
params = {
"blacklistFlags": ("nsfw,religious,political,racist,"
"sexist,explicit"),
"type": "single", # Get single-part jokes for simplicity
"amount": min(count, 10), # API limits to 10 jokes max
"contains": argument # Search for jokes containing the topic
}
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
joke_data = response.json()
joke_list = [joke['joke'] for joke in joke_data['jokes']]
except Exception as e:
return f"Error fetching joke: {e}"
#return json.dumps({"response": json.dumps(joke_data)})
return joke_list
This is already working when passed as a tool to the agent in Python code. However I want to reference it in the task definition YAML and no matter what I do it does not find the key.
Here is the YAML. I have tried JokerTool and “JokerTool”
joke_task:
description: >
Fetch a list of jokes related to the topic from the previous task using the Joker API.
Make sure the jokes are appropriate and relevant to the topic.
expected_output: JokerResponse
tools:
- "JokerTool"
agent: clown
I even tried adding a @tool definition function in python but that did not solve the problem so I removed it.
The consistent error I get it given below.
So far in general anything defined in Python, I cannot access in the YAML
Running the Crew
Traceback (most recent call last):
File "/home/mlokhandwala/linkedin-poster/poster/src/poster/main.py", line 25, in run
Poster().crew().kickoff(inputs=inputs)
^^^^^^^^
File "/home/mlokhandwala/linkedin-poster/poster/.venv/lib/python3.11/site-packages/crewai/project/crew_base.py", line 37, in __init__
self.map_all_task_variables()
File "/home/mlokhandwala/linkedin-poster/poster/.venv/lib/python3.11/site-packages/crewai/project/crew_base.py", line 234, in map_all_task_variables
self._map_task_variables(
File "/home/mlokhandwala/linkedin-poster/poster/.venv/lib/python3.11/site-packages/crewai/project/crew_base.py", line 262, in _map_task_variables
self.tasks_config[task_name]["tools"] = [
^
File "/home/mlokhandwala/linkedin-poster/poster/.venv/lib/python3.11/site-packages/crewai/project/crew_base.py", line 263, in <listcomp>
tool_functions[tool]() for tool in tools
~~~~~~~~~~~~~~^^^^^^
KeyError: 'JokerTool'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/mlokhandwala/linkedin-poster/poster/.venv/bin/run_crew", line 10, in <module>
sys.exit(run())
^^^^^
File "/home/mlokhandwala/linkedin-poster/poster/src/poster/main.py", line 27, in run
raise Exception(f"An error occurred while running the crew: {e}")
Exception: An error occurred while running the crew: 'JokerTool'

