langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
50.29k stars 7.2k forks source link

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

Open JokerQyou opened 1 week ago

JokerQyou commented 1 week ago

Self Checks

Dify version

0.9.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce


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.

dosubot[bot] commented 1 week ago

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.

JokerQyou commented 1 week ago

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

crazywoola commented 1 week ago

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.

dosubot[bot] commented 1 week ago

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.