工具入参为何会被解析一遍呢,导致非json数据无法入参? #5130

Closed
opened 2026-02-21 18:09:30 -05:00 by yindo · 1 comment
Owner

Originally created by @haike-1213 on GitHub (Aug 20, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.16

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

"Node run failed: Failed to invoke tool: Node 代码执行 run failed: Traceback (most recent call last):\n File "/var/sandbox/sandbox-python/tmp/7d0385bb_2708_49db_a9b6_ba19597cdeeb.py", line 48, in \n File "", line 20, in \n File "", line 5, in main\n File "/usr/local/lib/python3.10/json/init.py", line 346, in loads\n return _default_decoder.decode(s)\n File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n File "/usr/local/lib/python3.10/json/decoder.py", line 355, in raw_decode\n raise JSONDecodeError("Expecting value", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\nerror: exit status 255\n",

✔️ Expected Behavior

工具入参为何会被解析一遍呢,导致非json数据无法入参?

Actual Behavior

工具入参为何会被解析一遍呢,导致非json数据无法入参?

Originally created by @haike-1213 on GitHub (Aug 20, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.6.16 ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce "Node run failed: Failed to invoke tool: Node 代码执行 run failed: Traceback (most recent call last):\n File "/var/sandbox/sandbox-python/tmp/7d0385bb_2708_49db_a9b6_ba19597cdeeb.py", line 48, in <module>\n File "<string>", line 20, in <module>\n File "<string>", line 5, in main\n File "/usr/local/lib/python3.10/json/init.py", line 346, in loads\n return _default_decoder.decode(s)\n File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n File "/usr/local/lib/python3.10/json/decoder.py", line 355, in raw_decode\n raise JSONDecodeError("Expecting value", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\nerror: exit status 255\n", ### ✔️ Expected Behavior 工具入参为何会被解析一遍呢,导致非json数据无法入参? ### ❌ Actual Behavior 工具入参为何会被解析一遍呢,导致非json数据无法入参?
yindo closed this issue 2026-02-21 18:09:31 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 20, 2024):

工具入参会被解析是为了从 JSON 内容中提取数据。这个过程涉及将输入内容转换为 JSON 对象,因此非 JSON 数据会被视为无效。解析是为了应用 JSON 过滤器并提取所需信息。

以下是相关代码片段,展示了这个过程:

def _extract(self, content: str, json_filter: str, ensure_ascii: bool) -> str:
    try:
        input_data = json.loads(content)  # 将输入内容解析为 JSON
        expr = parse(json_filter)
        result = [match.value for match in expr.find(input_data)]

        if len(result) == 1:
            result = result[0]

        if isinstance(result, dict | list):
            return json.dumps(result, ensure_ascii=ensure_ascii)
        elif isinstance(result, str | int | float | bool) or result is None:
            return str(result)
        else:
            return repr(result)
    except Exception as e:
        return str(e)

json.loads(content) 这一行是将输入内容解析为 JSON,如果内容不是有效的 JSON,它将引发异常,导致工具返回错误消息[1][2][3][4]

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 20, 2024): <!-- Answer --> 工具入参会被解析是为了从 JSON 内容中提取数据。这个过程涉及将输入内容转换为 JSON 对象,因此非 JSON 数据会被视为无效。解析是为了应用 JSON 过滤器并提取所需信息。 以下是相关代码片段,展示了这个过程: ```python def _extract(self, content: str, json_filter: str, ensure_ascii: bool) -> str: try: input_data = json.loads(content) # 将输入内容解析为 JSON expr = parse(json_filter) result = [match.value for match in expr.find(input_data)] if len(result) == 1: result = result[0] if isinstance(result, dict | list): return json.dumps(result, ensure_ascii=ensure_ascii) elif isinstance(result, str | int | float | bool) or result is None: return str(result) else: return repr(result) except Exception as e: return str(e) ``` `json.loads(content)` 这一行是将输入内容解析为 JSON,如果内容不是有效的 JSON,它将引发异常,导致工具返回错误消息<sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/json_process/tools/parse.py)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/json_process/json_process.py)</sup><sup>[[3]](https://github.com/langgenius/dify/blob/main/api/core/tools/utils/parser.py)</sup><sup>[[4]](https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/tool/entities.py)</sup>。 <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5130