agent-zero
agent-zero copied to clipboard
TypeError: tool.before_execution() argument after ** must be a mapping, not str
Bug Description
When using Agent Zero, certain tool calls fail with the error:
TypeError: python.helpers.tool.Tool.before_execution() argument after ** must be a mapping, not str
Root Cause
In agent.py, line 860:
tool_args = tool_request.get("tool_args", tool_request.get("args", {}))
Sometimes the LLM returns tool_args as a string instead of a dictionary. When this string is passed as **tool_args on line 906:
await tool.before_execution(**tool_args)
Python throws the error because only dictionaries can be unpacked with **.
Steps to Reproduce
- Use Agent Zero with certain prompts that trigger tool calls
- The LLM may return
tool_argsas a string (e.g.,"code": "ls -la"instead of"tool_args": {"code": "ls -la"}) - Error occurs when trying to execute the tool
Suggested Fix
Add validation in process_tools() to ensure tool_args is always a dictionary:
tool_args = tool_request.get("tool_args", tool_request.get("args", {}))
# Ensure tool_args is a dictionary
if not isinstance(tool_args, dict):
if isinstance(tool_args, str):
try:
import json
tool_args = json.loads(tool_args)
except:
tool_args = {}
else:
tool_args = {}
Environment
- Agent Zero version: latest (Docker image agent0ai/agent-zero:latest)
- Python version: 3.12
Additional Context
This error occurs intermittently when the LLM response format varies.
Closing due to inactivity of 90 days.