agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

TypeError: tool.before_execution() argument after ** must be a mapping, not str

Open KOwl64 opened this issue 8 months ago • 1 comments

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

1. Use Agent Zero with certain prompts that trigger tool calls
2. The LLM may return tool_args as a string (e.g., "code": "ls -la" instead of "tool_args": {"code":
 "ls -la"})
3. 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):
    # Try to parse if it's a string, otherwise use empty 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.

KOwl64 avatar Mar 08 '26 22:03 KOwl64

Closing due to inactivity of 90 days.

github-actions[bot] avatar Jun 07 '26 04:06 github-actions[bot]