jd-opensource/joyagent-jdgenie · error · ValueError

工具参数必须是字典类型

Error message

工具参数必须是字典类型

What it means

A ValueError raised in `call_tool` when `arguments` is provided but is not a dict (e.g. a list, string, or JSON text). Passing None is fine (it defaults to {}); only a wrong non-dict type triggers this.

Solutions

  1. Convert the value to a dict: `json.loads(...)` for JSON strings or `dict(pairs)` for pair lists
  2. Check the type before calling: `isinstance(arguments, dict)`
  3. Pass None or omit the argument if there are no parameters

Example fix

// before
await client.call_tool("search", '{"query": "x"}')
// after
await client.call_tool("search", {"query": "x"})
Defensive patterns

Strategy: validation

Validate before calling

if arguments is not None and not isinstance(arguments, dict):
    arguments = json.loads(arguments) if isinstance(arguments, str) else dict(arguments)

Type guard

def is_arguments_dict(args) -> bool:
    return args is None or isinstance(args, dict)

Try / catch

try:
    result = await client.call_tool(name, args)
except ValueError as e:
    logger.error(f"arguments must be a dict: {e}")
    raise

Prevention

When it happens

Trigger: `client.call_tool("tool", ["a"])`, a JSON-encoded string, or another mapping-like object that is not a Python dict.

Common situations: Passing raw JSON text instead of a parsed dict; passing a list of (key, value) pairs; framework-provided params object that isn't a plain dict.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jd-opensource/joyagent-jdgenie@2417e0b8b6 (2026-09-08). Data as JSON: /api/errors/7957e87c0d0c4a74. Report an issue: GitHub.

Appendix: source

Thrown at genie-client/app/client.py:318

        Args:
            name: 工具名称
            arguments: 工具参数字典,默认为空字典

        Returns:
            Any: 工具执行结果

        Raises:
            ValueError: 当工具名称无效时抛出
            Exception: 当工具调用失败时抛出异常
        """
        # 参数验证
        if not name or not isinstance(name, str):
            raise ValueError("工具名称不能为空且必须是字符串类型")

        if arguments is None:
            arguments = {}
        elif not isinstance(arguments, dict):
            raise ValueError("工具参数必须是字典类型")

        try:
            async with self._sse_connection() as session:
                logger.info(f"正在调用工具 '{name}',参数: {arguments}")

                # 调用工具
                response = await session.call_tool(name=name, arguments=arguments)

                logger.info(f"工具 '{name}' 执行成功")
                logger.debug(f"工具 '{name}' 返回结果类型: {type(response).__name__}")

                return response

        except Exception as e:
            error_msg = f"调用工具 '{name}' 失败: {str(e)}"
            logger.error(error_msg)
            raise Exception(error_msg) from e

View on GitHub (pinned to 2417e0b8b6)