API Node's JSON Body Does Not Support Array Input
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
0.7.2
Cloud or Self Hosted
Cloud
Steps to reproduce
In the current implementation of the API node configuration, specifically within the JSON body section, there is a limitation that prevents users from select array types.
- In the code execution node, configure an output variable of array type.
- In the HTTP node, select the body type as JSON.
- Choose the variable.
✔️ Expected Behavior
The JSON body configuration for API nodes should support select array types, allowing users to construct and send requests that include array structures directly within the JSON body. This enhancement would enable a wider range of API interactions, particularly with services that require array inputs for processing.
❌ Actual Behavior
In the current implementation of the API node configuration, specifically within the JSON body section, there is a limitation that prevents users from select array types. This issue significantly restricts the functionality and flexibility of API requests, as it hinders the ability to send complex data structures, including arrays, to endpoints that expect such formats.
@YidaHu Hello! I'm here to help you with bug fixes, answer questions, and guide you on how to contribute.
The API Node's JSON body configuration does support array input in version 0.7.2. The code includes validation and transformation logic for array types, as shown in the _validate_result and _transform_result methods. Here are the relevant parts of the code:
In the _validate_result method:
if parameter.type.startswith('array'):
if not isinstance(result.get(parameter.name), list):
raise ValueError(f"Invalid `array` value for parameter {parameter.name}")
nested_type = parameter.type[6:-1]
for item in result.get(parameter.name):
if nested_type == 'number' and not isinstance(item, int | float):
raise ValueError(f"Invalid `array[number]` value for parameter {parameter.name}")
if nested_type == 'string' and not isinstance(item, str):
raise ValueError(f"Invalid `array[string]` value for parameter {parameter.name}")
if nested_type == 'object' and not isinstance(item, dict):
raise ValueError(f"Invalid `array[object]` value for parameter {parameter.name}")
In the _transform_result method:
elif parameter.type.startswith('array'):
if isinstance(result[parameter.name], list):
nested_type = parameter.type[6:-1]
transformed_result[parameter.name] = []
for item in result[parameter.name]:
if nested_type == 'number':
if isinstance(item, int | float):
transformed_result[parameter.name].append(item)
elif isinstance(item, str):
try:
if '.' in item:
transformed_result[parameter.name].append(float(item))
else:
transformed_result[parameter.name].append(int(item))
except ValueError:
pass
elif nested_type == 'string':
if isinstance(item, str):
transformed_result[parameter.name].append(item)
elif nested_type == 'object':
if isinstance(item, dict):
transformed_result[parameter.name].append(item)
Therefore, the array input is supported in the JSON body configuration for version 0.7.2 [1].
Additionally, there are specific unit tests for the JSON body configuration in API nodes that include array inputs. The tests test__convert_to_http_request_node_for_chatbot and test__convert_to_http_request_node_for_workflow_app both verify the JSON body configuration, including array inputs [2].
To continue talking to Dosu, mention @dosu.