langgenius / langgenius/dify

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

Open Beginner friendly
#39,773 1 comment 1 reaction 0 assignees View on GitHub
🐞 bug
Dominant language
TypeScript
Stars
156k
Forks
24.6k
Avg merge
20h 50m
Merged PRs (30d)
586

Description

### Self Checks

- [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542).
- [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general).
- [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.

### Dify version

main (commit 72c20daa61)

### Cloud or Self Hosted

Self Hosted (Docker)

### Steps to reproduce

I was looking at the _truncate_array method in api/services/variable_truncator.py
(which was updated in #39220 for the File bypass issue) and noticed a subtle
flag-accumulation bug.

When _truncate_array iterates over array elements, it tracks whether any
element was truncated via a `truncated` boolean. But at line 304, it does:

truncated = part_result.truncated

This overwrites the flag on every iteration instead of accumulating it. So
if element 0 gets truncated (truncated=True) but element 1 doesn't need
truncation (part_result.truncated=False), the final result incorrectly
reports truncated=False.

The other two methods in the same file do this correctly:
- _truncate_object (line 383-384) uses: if value_result.truncated: truncated = True
- truncate_variable_mapping (line 136) uses: is_truncated = is_truncated or part_result.truncated

### ✔️ Expected Behavior

_truncate_array should report truncated=True whenever ANY element in the
array was truncated, matching the behavior of _truncate_object and
truncate_variable_mapping in the same file.

### ❌ Actual Behavior

The truncated flag only reflects whether the LAST processed element was
truncated. Earlier truncations are silently forgotten/overwritten.

This means WorkflowNodeExecution records may report "not truncated" even
when array data was shortened, hiding the fact that users are looking at
incomplete results.

Suggested fix (one line, matching the exact pattern already used elsewhere
in the same file):

- truncated = part_result.truncated
+ truncated = truncated or part_result.truncated

I have a fix + test ready and can submit a PR right away.

Contributor guide

Open the contributing guide

Research direction

Start in api/services/variable_truncator.py at _truncate_array, then compare its truncated-flag handling with _truncate_object and truncate_variable_mapping. Add a regression test covering an array where an earlier element is truncated and a later element is not; done means the final result still reports truncated=True.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.