Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): Fix variable truncation for `list[File]` value in output mapping (#26133)

QuantumGhost96a0b9991e27
diff --git a/api/services/variable_truncator.py b/api/services/variable_truncator.py
index 4362bb02918..d02508e4f34 100644
--- a/api/services/variable_truncator.py
+++ b/api/services/variable_truncator.py
@@ -262,6 +262,14 @@ class VariableTruncator:
target_length = self._array_element_limit
for i, item in enumerate(value):
+ # Dirty fix:
+ # The output of `Start` node may contain list of `File` elements,
+ # causing `AssertionError` while invoking `_truncate_json_primitives`.
+ #
+ # This check ensures that `list[File]` are handled separately
+ if isinstance(item, File):
+ truncated_value.append(item)
+ continue
if i >= target_length:
return _PartResult(truncated_value, used_size, True)
if i > 0:
diff --git a/api/tests/unit_tests/services/test_variable_truncator.py b/api/tests/unit_tests/services/test_variable_truncator.py
index 0ad056c9852..6761f939e31 100644
--- a/api/tests/unit_tests/services/test_variable_truncator.py
+++ b/api/tests/unit_tests/services/test_variable_truncator.py
@@ -588,3 +588,11 @@ class TestIntegrationScenarios:
if isinstance(result.result, ObjectSegment):
result_size = truncator.calculate_json_size(result.result.value)
assert result_size <= original_size
+
+ def test_file_and_array_file_variable_mapping(self, file):
+ truncator = VariableTruncator(string_length_limit=30, array_element_limit=3, max_size_bytes=300)
+
+ mapping = {"array_file": [file]}
+ truncated_mapping, truncated = truncator.truncate_variable_mapping(mapping)
+ assert truncated is False
+ assert truncated_mapping == mapping