Skip to content

fix(api): accumulate truncated flag in _truncate_array - #39774

Open
agarwalpranav0711 wants to merge 1 commit into
langgenius:mainfrom
agarwalpranav0711:fix/variable-truncator-accumulate-flag
Open

fix(api): accumulate truncated flag in _truncate_array#39774
agarwalpranav0711 wants to merge 1 commit into
langgenius:mainfrom
agarwalpranav0711:fix/variable-truncator-accumulate-flag

Conversation

@agarwalpranav0711

@agarwalpranav0711 agarwalpranav0711 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #39773

_truncate_array in api/services/variable_truncator.py uses a direct assignment (truncated = part_result.truncated) at line 304, which overwrites the truncation flag on every loop iteration. If an early array element is truncated but a later one fits within budget, the method incorrectly returns truncated = False.

The other two methods in the same file accumulate the flag correctly:

  • truncate_variable_mapping (line 136): is_truncated = is_truncated or part_result.truncated
  • _truncate_object (lines 383-384): if value_result.truncated: truncated = True

This PR applies the same accumulating pattern to _truncate_array and adds a targeted unit test.

Changes

  • api/services/variable_truncator.py: Changed truncated = part_result.truncatedtruncated = truncated or part_result.truncated
  • api/tests/unit_tests/services/test_variable_truncator.py: Added test_array_truncation_flag_accumulation to verify the flag stays True when an early element is truncated but a later one is not

Screenshots

N/A — backend-only logic fix, no UI changes.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-07-29 17:25:08.033178936 +0000
+++ /tmp/pyrefly_pr.txt	2026-07-29 17:24:58.714097611 +0000
@@ -8397,11 +8397,11 @@
 ERROR Cannot set item in `dict[str, int]` [unsupported-operation]
    --> tests/unit_tests/services/test_variable_truncator.py:142:31
 ERROR Argument `list[File]` is not assignable to parameter `value` with type `list[object]` in function `services.variable_truncator.VariableTruncator._truncate_array` [bad-argument-type]
-   --> tests/unit_tests/services/test_variable_truncator.py:724:44
+   --> tests/unit_tests/services/test_variable_truncator.py:731:44
 ERROR Argument `list[File]` is not assignable to parameter `value` with type `list[object]` in function `services.variable_truncator.VariableTruncator._truncate_array` [bad-argument-type]
-   --> tests/unit_tests/services/test_variable_truncator.py:736:44
+   --> tests/unit_tests/services/test_variable_truncator.py:743:44
 ERROR Argument `list[File]` is not assignable to parameter `value` with type `list[object]` in function `services.variable_truncator.VariableTruncator._truncate_array` [bad-argument-type]
-   --> tests/unit_tests/services/test_variable_truncator.py:751:44
+   --> tests/unit_tests/services/test_variable_truncator.py:758:44
 ERROR Argument `dict[str, dict[Unknown, Unknown]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
    --> tests/unit_tests/services/test_webhook_service.py:250:79
 ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 56.46% 56.46% -0.00%
Strict coverage 55.97% 55.97% -0.00%
Typed symbols 36,871 36,872 +1
Untyped symbols 28,686 28,687 +1
Modules 3091 3091 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(api): accumulate truncated flag in _truncate_array instead of overwriting it

1 participant