code run in chatflow fail #12160

Closed
opened 2026-02-21 19:06:05 -05:00 by yindo · 4 comments
Owner

Originally created by @SeesawLiu on GitHub (Mar 26, 2025).

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

1.1.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

in a chat flow ,run python code


def main(query: str) -> dict:
    data = [
     ["310100", "上海市", "上海市", "上海"],
     ["310101", "上海市", "上海市", "黄浦"],
     ["310104", "上海市", "上海市", "徐汇"],
     ["310105", "上海市", "上海市", "长宁区"],
     ["310106", "上海市", "上海市", "静安"],
     ["310107", "上海市", "上海市", "普陀区"],
     ["310109", "上海市", "上海市", "虹口"],
     ["310110", "上海市", "上海市", "杨浦"],
    ]
    
    query_cleaned = query.replace("市", "").replace("区", "")
    result = [entry for entry in data if all(part in "".join(entry[1:]) for part in query_cleaned)]

    if not result:
        return {"result": "500100"}

    return {"result": result[0][0]}

✔️ Expected Behavior

if input 静安,return 310106

Image

Actual Behavior

when i debug the flow ,it's ok

but when i call from api,it run fail

Image

Traceback (most recent call last): File "/var/sandbox/sandbox-python/tmp/9a8c6b8a_95dc_4a07_a281_bfe6539bf5a9.py", line 48, in <module> File "<string>", line 30, in <module> File "<string>", line 21, in main IndexError: list index out of range error: exit status 255
Originally created by @SeesawLiu on GitHub (Mar 26, 2025). ### 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 1.1.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce in a chat flow ,run python code ``` def main(query: str) -> dict: data = [ ["310100", "上海市", "上海市", "上海"], ["310101", "上海市", "上海市", "黄浦"], ["310104", "上海市", "上海市", "徐汇"], ["310105", "上海市", "上海市", "长宁区"], ["310106", "上海市", "上海市", "静安"], ["310107", "上海市", "上海市", "普陀区"], ["310109", "上海市", "上海市", "虹口"], ["310110", "上海市", "上海市", "杨浦"], ] query_cleaned = query.replace("市", "").replace("区", "") result = [entry for entry in data if all(part in "".join(entry[1:]) for part in query_cleaned)] if not result: return {"result": "500100"} return {"result": result[0][0]} ``` ### ✔️ Expected Behavior if input 静安,return 310106 ![Image](https://github.com/user-attachments/assets/9b7b0804-cc29-4ac7-9b9b-9edd04c19c12) ### ❌ Actual Behavior when i debug the flow ,it's ok but when i call from api,it run fail ![Image](https://github.com/user-attachments/assets/d89de770-4490-428c-a2e4-1aad8b89e3ea) ``` Traceback (most recent call last): File "/var/sandbox/sandbox-python/tmp/9a8c6b8a_95dc_4a07_a281_bfe6539bf5a9.py", line 48, in <module> File "<string>", line 30, in <module> File "<string>", line 21, in main IndexError: list index out of range error: exit status 255 ```
yindo closed this issue 2026-02-21 19:06:05 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 26, 2025):

I found a similar open issue where the IndexError: list index out of range error was discussed. The suggested solution is to ensure that the list you're trying to access is not empty before accessing its elements. Here's an updated version of your code that includes a check for an empty result:

def main(query: str) -> dict:
    data = [
        ["310100", "上海市", "上海市", "上海"],
        ["310101", "上海市", "上海市", "黄浦"],
        ["310104", "上海市", "上海市", "徐汇"],
        ["310105", "上海市", "上海市", "长宁区"],
        ["310106", "上海市", "上海市", "静安"],
        ["310107", "上海市", "上海市", "普陀区"],
        ["310109", "上海市", "上海市", "虹口"],
        ["310110", "上海市", "上海市", "杨浦"],
    ]
    
    query_cleaned = query.replace("市", "").replace("区", "")
    result = [entry for entry in data if all(part in "".join(entry[1:]) for part in query_cleaned)]

    if not result:
        return {"result": "500100"}

    return {"result": result[0][0]}

This code checks if result is empty before attempting to access result[0][0], which should prevent the IndexError from occurring [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Mar 26, 2025): <!-- Answer --> I found a similar open issue where the `IndexError: list index out of range` error was discussed. The suggested solution is to ensure that the list you're trying to access is not empty before accessing its elements. Here's an updated version of your code that includes a check for an empty result: ```python def main(query: str) -> dict: data = [ ["310100", "上海市", "上海市", "上海"], ["310101", "上海市", "上海市", "黄浦"], ["310104", "上海市", "上海市", "徐汇"], ["310105", "上海市", "上海市", "长宁区"], ["310106", "上海市", "上海市", "静安"], ["310107", "上海市", "上海市", "普陀区"], ["310109", "上海市", "上海市", "虹口"], ["310110", "上海市", "上海市", "杨浦"], ] query_cleaned = query.replace("市", "").replace("区", "") result = [entry for entry in data if all(part in "".join(entry[1:]) for part in query_cleaned)] if not result: return {"result": "500100"} return {"result": result[0][0]} ``` This code checks if `result` is empty before attempting to access `result[0][0]`, which should prevent the `IndexError` from occurring <sup>[[1]](https://github.com/langgenius/dify/issues/7649)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4f6b884e-11fc-43eb-ba3a-bce7096bac11?feedback_type=other)</sup>
Author
Owner

@SeesawLiu commented on GitHub (Mar 26, 2025):

the key point is: when i debug the flow ,it's ok,but when i call from api,it run fail @sodu

@SeesawLiu commented on GitHub (Mar 26, 2025): the key point is: when i debug the flow ,it's ok,but when i call from api,it run fail @sodu
Author
Owner

@crazywoola commented on GitHub (Mar 26, 2025):

Image

Can not reproduce.

And this is a general programming issue, please resolve it by yourself.

@crazywoola commented on GitHub (Mar 26, 2025): <img width="1122" alt="Image" src="https://github.com/user-attachments/assets/2a8a6387-b455-4490-a0e2-628b09362abd" /> Can not reproduce. And this is a general programming issue, please resolve it by yourself.
Author
Owner

@SeesawLiu commented on GitHub (Mar 26, 2025):

do you saw the words,call from api,it will run fail.and if you modify the code,the error is the same. i publish the flow as a tool.
maybe the code run in the api is cache
@crazywoola @dosu

@SeesawLiu commented on GitHub (Mar 26, 2025): do you saw the words,call from api,it will run fail.and if you modify the code,the error is the same. i publish the flow as a tool. maybe the code run in the api is cache @crazywoola @dosu
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#12160