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): honor array-element limit and byte budget for list[File] in VariableTruncator (#39218) (#39220)

Taranum Wasuf16c249b1eac
diff --git a/api/services/variable_truncator.py b/api/services/variable_truncator.py
index 00aa31650c2..78e917b052a 100644
--- a/api/services/variable_truncator.py
+++ b/api/services/variable_truncator.py
@@ -278,14 +278,14 @@ class VariableTruncator(BaseTruncator):
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
+ # ``File`` is routed through ``_truncate_json_primitives`` (whose
+ # dedicated ``File`` branch returns the file as-is with its real
+ # serialized size). That preserves the count cap
+ # (``array_element_limit``) and the byte budget (``target_size``)
+ # for ``list[File]`` — the original "Dirty fix" branch above this
+ # loop bypassed both guarantees and reported ``used_size=2`` even
+ # when the returned array serialized to well over the budget.
+ # See https://github.com/langgenius/dify/issues/39218.
if i >= target_length:
return _PartResult(truncated_value, used_size, True)
if i > 0:
@@ -295,7 +295,7 @@ class VariableTruncator(BaseTruncator):
break
remaining_budget = target_size - used_size
- if item is None or isinstance(item, (str, list, dict, bool, int, float, UpdatedVariable)):
+ if item is None or isinstance(item, (str, list, dict, bool, int, float, File, UpdatedVariable)):
part_result = self._truncate_json_primitives(item, remaining_budget)
else:
raise UnknownTypeError(f"got unknown type {type(item)} in array truncation")
diff --git a/api/tests/unit_tests/services/test_variable_truncator.py b/api/tests/unit_tests/services/test_variable_truncator.py
index 931e96ef3a7..f2ba1784a28 100644
--- a/api/tests/unit_tests/services/test_variable_truncator.py
+++ b/api/tests/unit_tests/services/test_variable_truncator.py
@@ -673,3 +673,118 @@ def test_dummy_variable_truncator_methods():
assert isinstance(result, TruncationResult)
assert result.result == segment
assert result.truncated is False
+
+
+# ---------------------------------------------------------------------------
+# Regression tests for langgenius/dify#39218.
+#
+# Before the fix, ``_truncate_array`` had a "Dirty fix" branch that
+# unconditionally appended every ``File`` element to ``truncated_value``
+# *before* the count cap and the byte-budget check, and *before*
+# ``used_size`` was ever incremented. That made ``list[File]`` arrays:
+# 1. uncapped by ``array_element_limit``,
+# 2. uncounted against ``max_size_bytes``, and
+# 3. always reported ``truncated=False``.
+# The fix routes ``File`` through ``_truncate_json_primitives``'s dedicated
+# ``File`` branch, which returns the file as-is with its real serialized
+# size, while preserving the count cap and the byte budget.
+# ---------------------------------------------------------------------------
+
+
+class TestFileArrayTruncationRegression39218:
+ """``list[File]`` must respect ``array_element_limit`` and the byte budget."""
+
+ @pytest.fixture
+ def truncator(self) -> VariableTruncator:
+ return VariableTruncator(
+ array_element_limit=3,
+ max_size_bytes=1000,
+ string_length_limit=50,
+ )
+
+ @staticmethod
+ def _make_file(name: str = "f") -> File:
+ return File(
+ id=name,
+ type=FileType.DOCUMENT,
+ transfer_method=FileTransferMethod.REMOTE_URL,
+ remote_url=f"https://example.com/{name}.txt",
+ filename=f"{name}.txt",
+ extension=".txt",
+ mime_type="text/plain",
+ size=1024,
+ )
+
+ def test_file_array_respects_element_count_cap(self, truncator: VariableTruncator) -> None:
+ # Use a target_size larger than ``count * file_size`` so the byte
+ # budget never binds — only the count cap should fire.
+ # Each File serializes to ~237 bytes; 3 files = ~713 bytes.
+ files = [self._make_file(f"f{i}") for i in range(500)]
+
+ result = truncator._truncate_array(files, target_size=10_000_000)
+
+ # Before the fix, all 500 File entries survived (``len(value)==500``,
+ # ``truncated==False``). After the fix, the array is capped at
+ # ``array_element_limit=3`` and ``truncated`` flips to True.
+ assert len(result.value) == 3
+ assert result.truncated is True
+
+ def test_file_array_reports_real_used_size(self, truncator: VariableTruncator) -> None:
+ # Large budget so the count cap fires before the byte budget does.
+ files = [self._make_file(f"f{i}") for i in range(500)]
+
+ result = truncator._truncate_array(files, target_size=10_000_000)
+
+ # Before the fix, ``used_size`` for a File array was the empty-array
+ # baseline of 2 bytes (``[]``), regardless of how many File entries
+ # actually returned. After the fix, ``used_size`` reflects the real
+ # serialized size of the returned ``File`` payload.
+ assert result.value_size > 100
+ assert result.truncated is True
+
+ def test_file_array_respects_byte_budget(self, truncator: VariableTruncator) -> None:
+ # Use a small ``target_size`` so the byte budget is the binding
+ # constraint. Each File serializes to ~237 bytes, so even one File
+ # blows the 200-byte budget.
+ files = [self._make_file(f"f{i}") for i in range(50)]
+
+ result = truncator._truncate_array(files, target_size=200)
+
+ # Before the fix, all 50 File entries survived and ``used_size``
+ # reported ``2`` (the empty-array baseline). After the fix, the
+ # loop sees the File payload: ``value_size`` reflects the real
+ # serialized size, and the loop stops after the first File because
+ # adding the next one would exceed ``target_size``.
+ assert len(result.value) == 1
+ assert result.value_size > 100 # the File's real serialized size
+ assert result.value_size <= 250 # in the ballpark of the budget
+
+ def test_mixed_array_counts_files_toward_cap(self, truncator: VariableTruncator) -> None:
+ mixed: list[object] = [
+ self._make_file("f0"),
+ "a",
+ self._make_file("f1"),
+ "b",
+ self._make_file("f2"),
+ "c",
+ self._make_file("f3"),
+ "d",
+ ]
+
+ result = truncator._truncate_array(mixed, target_size=10_000_000)
+
+ # 8 items, cap of 3 → exactly 3 items. Files and primitives are
+ # counted together toward the cap.
+ assert len(result.value) == 3
+ assert result.truncated is True
+
+ def test_single_file_in_array_is_preserved(self, truncator: VariableTruncator) -> None:
+ result = truncator._truncate_array([self._make_file("only")], target_size=10_000_000)
+
+ # The File itself is not truncated — the dedicated ``File`` branch
+ # in ``_truncate_json_primitives`` returns the file untouched. Only
+ # the array-shape accounting changes.
+ assert len(result.value) == 1
+ assert isinstance(result.value[0], File)
+ assert result.value[0].id == "only"
+ assert result.truncated is False