Can't embed multi-dimension array inside Object in result of Code execution node #6159

Closed
opened 2026-02-21 18:14:19 -05:00 by yindo · 5 comments
Owner

Originally created by @JokerQyou on GitHub (Oct 18, 2024).

Originally assigned to: @crazywoola on GitHub.

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.9.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  • Create a workflow app.
  • Add a code execution node, select Python3 as language.
  • In "return" statement, return an object with array, see the following code.
  • In "Output variables" section, select type "Object" for result variable.
  • Run this workflow.

def main() -> dict:
    return {
        "result": {
            "hey": [
                [1, 2, 3],
                [4, 5, 6],
            ]
        },
    }

✔️ Expected Behavior

Workflow should run without error.

Actual Behavior

Code execution block runs into an error saying Node 代码执行 run failed: Output .result.hey is not a valid array. make sure all elements are of the same type..

It is not clear what are the restrictions on Object type of output variables. One would assume that it's just a normal dict instance in Python or anything that can pass JSON.stringify in Javascript. But as this error indicates it is not.

Originally created by @JokerQyou on GitHub (Oct 18, 2024). Originally assigned to: @crazywoola on GitHub. ### 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.9.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce - Create a workflow app. - Add a code execution node, select Python3 as language. - In "return" statement, return an object with array, see the following code. - In "Output variables" section, select type "Object" for `result` variable. - Run this workflow. ```python def main() -> dict: return { "result": { "hey": [ [1, 2, 3], [4, 5, 6], ] }, } ``` ### ✔️ Expected Behavior Workflow should run without error. ### ❌ Actual Behavior Code execution block runs into an error saying `Node 代码执行 run failed: Output .result.hey is not a valid array. make sure all elements are of the same type.`. It is not clear what are the restrictions on `Object` type of output variables. One would assume that it's just a normal `dict` instance in Python or anything that can pass `JSON.stringify` in Javascript. But as this error indicates it is not.
yindo added the 💪 enhancement🐞 bug labels 2026-02-21 18:14:19 -05:00
yindo closed this issue 2026-02-21 18:14:19 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 18, 2024):

The error you're encountering is due to the validation logic in the _transform_result method of the Dify code execution node. This method checks if the elements of an array are of the same type. If the array contains elements of different types, it raises a ValueError. In your case, the multi-dimensional array [[1, 2, 3], [4, 5, 6]] is being checked for type consistency, and it seems to be failing this check [1].

To resolve this issue, ensure that the elements within each sub-array are of the same type, which they already are in your example. However, the error might be due to how the validation logic interprets nested arrays. You might need to adjust the structure or check if there are any additional constraints on nested arrays in the Dify documentation or codebase.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 18, 2024): <!-- Answer --> The error you're encountering is due to the validation logic in the `_transform_result` method of the Dify code execution node. This method checks if the elements of an array are of the same type. If the array contains elements of different types, it raises a `ValueError`. In your case, the multi-dimensional array `[[1, 2, 3], [4, 5, 6]]` is being checked for type consistency, and it seems to be failing this check <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/code/code_node.py)</sup>. To resolve this issue, ensure that the elements within each sub-array are of the same type, which they already are in your example. However, the error might be due to how the validation logic interprets nested arrays. You might need to adjust the structure or check if there are any additional constraints on nested arrays in the Dify documentation or codebase. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@JokerQyou commented on GitHub (Oct 18, 2024):

There is no mentioning on any specific type in Dify documentation. Also I think [[1,2,3], [4,5,6]] does not break the rule "all elements are of the same type", since all elements are arrays with 3 integers. If this code runs off Dify I would type hint like this: List[List[int]]. Or I could write [(1,2,3), (4,5,6)] which satisfies an even more strict type hint: List[Tuple[int, int, int]]. But both the array and tuple example would fail with the same error. @dosu

@JokerQyou commented on GitHub (Oct 18, 2024): There is no mentioning on any specific type in Dify documentation. Also I think `[[1,2,3], [4,5,6]]` does not break the rule "all elements are of the same type", since all elements are arrays with 3 integers. If this code runs off Dify I would type hint like this: `List[List[int]]`. Or I could write `[(1,2,3), (4,5,6)]` which satisfies an even more strict type hint: `List[Tuple[int, int, int]]`. But both the array and tuple example would fail with the same error. @dosu
Author
Owner

@crazywoola commented on GitHub (Oct 18, 2024):

https://docs.dify.ai/guides/workflow/node/code#usage-scenarios
I think implementing something that can check the type recursively could help, we do have limitations on the but it's not by purpose, just because we are lack of time, so feel free to open a pull request.

@crazywoola commented on GitHub (Oct 18, 2024): https://docs.dify.ai/guides/workflow/node/code#usage-scenarios I think implementing something that can check the type recursively could help, we do have limitations on the but it's not by purpose, just because we are lack of time, so feel free to open a pull request.
Author
Owner

@dosubot[bot] commented on GitHub (Oct 18, 2024):

The issue arises because the Dify code execution node's _transform_result method does not support nested arrays or tuples. It expects all elements in a list to be of the same type, specifically numbers, strings, or dictionaries. Since the elements in your example are lists or tuples themselves, they do not match the expected types, leading to a ValueError [1].

Unfortunately, the Dify documentation does not explicitly mention handling nested arrays or tuples in the output of a code execution node. You might need to flatten the structure or handle the data differently to conform to the current constraints.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 18, 2024): <!-- Answer --> The issue arises because the Dify code execution node's `_transform_result` method does not support nested arrays or tuples. It expects all elements in a list to be of the same type, specifically numbers, strings, or dictionaries. Since the elements in your example are lists or tuples themselves, they do not match the expected types, leading to a `ValueError` <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/code/code_node.py)</sup>. Unfortunately, the Dify documentation does not explicitly mention handling nested arrays or tuples in the output of a code execution node. You might need to flatten the structure or handle the data differently to conform to the current constraints. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@hwb96 commented on GitHub (Apr 16, 2025):

Today, I also encountered the same problem as you, and I have the same structure, but it reported an error. I am really very puzzled.

@hwb96 commented on GitHub (Apr 16, 2025): Today, I also encountered the same problem as you, and I have the same structure, but it reported an error. I am really very puzzled.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6159