dify icon indicating copy to clipboard operation
dify copied to clipboard

Last week, I ran a code execution node to extract fields from text, which worked fine. But this week, using the exact same workflow and code, the node only returns the default output without performing the extraction.

Open danie666-star opened this issue 8 months ago • 8 comments

Self Checks

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

Dify version

1.4.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Last week, I ran a code execution node to extract fields from text, which worked fine. But this week, using the exact same workflow and code, the node only returns the default output without performing the extraction.

import re
import json

def main(arg1: str) -> dict:
    default_output = {
        "results": "",
        "ECHarts": "0",
        "chartType": "",
        "chartTitle": "",
        "chartData": "",
        "chartXAxis": ""
    }
    
    try:
        match = re.search(r'```json\s*([\s\S]*?)\s*```', arg1)
        if not match:
            raise ValueError("输入字符串中未找到有效的 JSON 数据")
        
        json_str = match.group(1).strip()
        result_dict = json.loads(json_str)
    except Exception as e:
        print(f"解析失败: {e}")
        return default_output
    
    if "ECHarts" not in result_dict:
        result_dict["ECHarts"] = "0"
    
    if result_dict["ECHarts"] == "1":
        required_chart_fields = ["chartType", "chartTitle", "chartData", "chartXAxis"]
        for field in required_chart_fields:
            if field not in result_dict:
                result_dict[field] = ""
    
    return {
        "results": str(result_dict.get("results", "")),
        "ECHarts": str(result_dict.get("ECHarts", "0")),
        "chartType": str(result_dict.get("chartType", "")),
        "chartTitle": str(result_dict.get("chartTitle", "")),
        "chartData": str(result_dict.get("chartData", "")).replace('|', ';'),
        "chartXAxis": str(result_dict.get("chartXAxis", ""))
    }
Image Image

✔️ Expected Behavior

Fixing Bugs in Code Execution

❌ Actual Behavior

No response

danie666-star avatar Jun 06 '25 08:06 danie666-star

  • Please provide some test cases.

crazywoola avatar Jun 06 '25 08:06 crazywoola

Hi @danie666-star! I'm Dosu and I’m helping the dify team.

Since your code execution node suddenly started returning only the default output, but nothing changed in your workflow or code, the most likely causes are either a change in the input data format or an issue with the code execution environment.

First, double-check that the input string (arg1) still matches the expected format—your regex expects a code block like json ... . If the input changed (for example, missing the code block or the JSON inside is malformed), your code will hit the exception and return the default output.

Second, since you're running self-hosted with Docker, network or sandbox issues can cause silent failures. If the code execution service can't run your code or can't reach required resources, it may fail and return the default output. There are known cases where Docker network issues (especially with plugins or proxies like Clash) can break code execution—enabling global TUN mode in Clash or checking Docker's network settings can help if that's relevant to your setup (source).

Finally, Dify's error handling is robust: if the code execution service is unavailable, if there's a network issue, or if the output doesn't match the expected schema, it will raise an error and return a failed status with details (source). Check your workflow logs or node outputs for any error messages—they may contain clues about what went wrong.

If you confirm the input format is correct and the Docker environment is healthy, but the issue persists, try restarting the code execution service/container and watch for any errors in the logs. If you see a specific error, feel free to share it for more targeted help.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Jun 06 '25 08:06 dosubot[bot]

  • Please provide some test cases.请提供一些测试用例。
{
  "text": "```json\n{ \"results\": \"| 客户名称 | 认领金额合计 | 占比 |\\n| --- | --- | --- |\\n| 公司1 | 1230044.32 | 18.78% |\\n| 公司2 | 57043.9 | 0.87% |\\n| 公司3 | 88874.79 | 1.36% |\\n| 公司4 | 254029 | 3.88% |\\n|公司4 | 10075.47 | 0.15% |\\n| 公司6 | 64800 | 0.99% |\\n| 公司7| 318101 | 4.86% |\\n| 公司8 | 135250 | 2.07% |\\n| 公司9 | 49000 | 0.75% |\\n| 公司10 | 66000 | 1.01% |\",\n \"ECHarts\": \"1\",\n \"chartType\": \"饼状图\",\n \"chartTitle\": \"合计值占比\",\n \"chartData\": \"18.78;0.87;1.36;3.88;0.15;0.99;4.86;2.07;0.75;1.01\",\n \"chartXAxis\": \"公司1;公司2;公司3;公司4;公司5;公司6;公司7;公司8;公司9;公司10\"\n}\n```",
  "usage": {
    "prompt_tokens": 2345,
    "prompt_unit_price": "0.002",
    "prompt_price_unit": "0.001",
    "prompt_price": "0.00469",
    "completion_tokens": 429,
    "completion_unit_price": "0.008",
    "completion_price_unit": "0.001",
    "completion_price": "0.003432",
    "total_tokens": 2774,
    "total_price": "0.008122",
    "currency": "RMB",
    "latency": 29.645418926000275
  },
  "finish_reason": "stop"
}

danie666-star avatar Jun 06 '25 08:06 danie666-star

  • Please provide some test cases.请提供一些测试用例。

{ "text": "json\n{ \"results\": \"| 客户名称 | 认领金额合计 | 占比 |\\n| --- | --- | --- |\\n| 公司1 | 1230044.32 | 18.78% |\\n| 公司2 | 57043.9 | 0.87% |\\n| 公司3 | 88874.79 | 1.36% |\\n| 公司4 | 254029 | 3.88% |\\n|公司4 | 10075.47 | 0.15% |\\n| 公司6 | 64800 | 0.99% |\\n| 公司7| 318101 | 4.86% |\\n| 公司8 | 135250 | 2.07% |\\n| 公司9 | 49000 | 0.75% |\\n| 公司10 | 66000 | 1.01% |\",\n \"ECHarts\": \"1\",\n \"chartType\": \"饼状图\",\n \"chartTitle\": \"合计值占比\",\n \"chartData\": \"18.78;0.87;1.36;3.88;0.15;0.99;4.86;2.07;0.75;1.01\",\n \"chartXAxis\": \"公司1;公司2;公司3;公司4;公司5;公司6;公司7;公司8;公司9;公司10\"\n}\n", "usage": { "prompt_tokens": 2345, "prompt_unit_price": "0.002", "prompt_price_unit": "0.001", "prompt_price": "0.00469", "completion_tokens": 429, "completion_unit_price": "0.008", "completion_price_unit": "0.001", "completion_price": "0.003432", "total_tokens": 2774, "total_price": "0.008122", "currency": "RMB", "latency": 29.645418926000275 }, "finish_reason": "stop" }

@dosu, check this code example and the provided python code.

crazywoola avatar Jun 06 '25 09:06 crazywoola

Your test case input matches what your Python code expects: a string containing a code block with triple backticks and the json keyword. The regex in your code should extract the JSON and parse it correctly.

If you're still getting only the default output, the most likely causes are:

  • The input string format changed (for example, missing the triple backticks or the json keyword).
  • The code execution environment is failing silently, possibly due to a sandbox or dependency issue.
  • The output from your code doesn't match the expected output schema, triggering Dify's strict output validation and causing it to fall back to default output or mark the node as failed (source).

To debug:

  • Double-check the actual input string reaching your code node (print/log it if possible).
  • Check the workflow logs for any error messages or output validation errors.
  • Make sure your code node's output keys and types match the schema defined in your workflow.

If you can share the exact input string as received by the node (not just the test case), I can help check for subtle format issues.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Jun 06 '25 09:06 dosubot[bot]

Your test case input matches what your Python code expects: a string containing a code block with triple backticks and the json keyword. The regex in your code should extract the JSON and parse it correctly.

If you're still getting only the default output, the most likely causes are:

  • The input string format changed (for example, missing the triple backticks or the json keyword).
  • The code execution environment is failing silently, possibly due to a sandbox or dependency issue.
  • The output from your code doesn't match the expected output schema, triggering Dify's strict output validation and causing it to fall back to default output or mark the node as failed (source).

To debug:

  • Double-check the actual input string reaching your code node (print/log it if possible).
  • Check the workflow logs for any error messages or output validation errors.
  • Make sure your code node's output keys and types match the schema defined in your workflow.

If you can share the exact input string as received by the node (not just the test case), I can help check for subtle format issues.

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

The test cases provided above are the actual inputs from my workflow, with only partial data desensitization applied.

danie666-star avatar Jun 06 '25 09:06 danie666-star

  • Please provide some test cases.请提供一些测试用例。

{ "text": "json\n{ \"results\": \"| 客户名称 | 认领金额合计 | 占比 |\\n| --- | --- | --- |\\n| 公司1 | 1230044.32 | 18.78% |\\n| 公司2 | 57043.9 | 0.87% |\\n| 公司3 | 88874.79 | 1.36% |\\n| 公司4 | 254029 | 3.88% |\\n|公司4 | 10075.47 | 0.15% |\\n| 公司6 | 64800 | 0.99% |\\n| 公司7| 318101 | 4.86% |\\n| 公司8 | 135250 | 2.07% |\\n| 公司9 | 49000 | 0.75% |\\n| 公司10 | 66000 | 1.01% |\",\n \"ECHarts\": \"1\",\n \"chartType\": \"饼状图\",\n \"chartTitle\": \"合计值占比\",\n \"chartData\": \"18.78;0.87;1.36;3.88;0.15;0.99;4.86;2.07;0.75;1.01\",\n \"chartXAxis\": \"公司1;公司2;公司3;公司4;公司5;公司6;公司7;公司8;公司9;公司10\"\n}\n", "usage": { "prompt_tokens": 2345, "prompt_unit_price": "0.002", "prompt_price_unit": "0.001", "prompt_price": "0.00469", "completion_tokens": 429, "completion_unit_price": "0.008", "completion_price_unit": "0.001", "completion_price": "0.003432", "total_tokens": 2774, "total_price": "0.008122", "currency": "RMB", "latency": 29.645418926000275 }, "finish_reason": "stop" }

@dosu, check this code example and the provided python code.

Moreover, I have deployed Dify on both the server and local environments, and the same issue persists. The critical point is that this field extraction null-value problem did not occur previously.

danie666-star avatar Jun 06 '25 09:06 danie666-star

Can you provide your dify's DSL for reproduce.

water-in-stone avatar Jun 07 '25 09:06 water-in-stone