dify - 💡(How to fix) Fix VariableTruncator._truncate_array under-reports the `truncated` flag after dropping/shortening array elements [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Fixed

Code Example

from services.variable_truncator import VariableTruncator

# Case 1: size-limit break drops the tail, but flag stays False
t = VariableTruncator(array_element_limit=20, max_size_bytes=1000)
r = t._truncate_array([10, 20, 30, 40, 50], 12)
print(r.value, r.truncated)        # [10, 20, 30, 40] False  <- 50 dropped, but False

# Case 2: a later element that fits resets the flag set by an earlier truncated element
t2 = VariableTruncator(array_element_limit=20, max_size_bytes=1000, string_length_limit=10)
r2 = t2._truncate_array(["x" * 60, "a"], 60)
print(r2.value, r2.truncated)      # ['xxxxxxxxxx...', 'a'] False  <- first string truncated, but False

---

def main() -> dict:
       return {"result": [10, 20, 30, 40, 50]}

---

{"node_type":"code","outputs":{"result":[10]},"outputs_truncated":false}
RAW_BUFFERClick to expand / collapse

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.14.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

The bug is in api/services/variable_truncator.pyVariableTruncator._truncate_array. The truncated flag is under-reported in two cases.

Fastest reproduction (unit level):

from services.variable_truncator import VariableTruncator

# Case 1: size-limit break drops the tail, but flag stays False
t = VariableTruncator(array_element_limit=20, max_size_bytes=1000)
r = t._truncate_array([10, 20, 30, 40, 50], 12)
print(r.value, r.truncated)        # [10, 20, 30, 40] False  <- 50 dropped, but False

# Case 2: a later element that fits resets the flag set by an earlier truncated element
t2 = VariableTruncator(array_element_limit=20, max_size_bytes=1000, string_length_limit=10)
r2 = t2._truncate_array(["x" * 60, "a"], 60)
print(r2.value, r2.truncated)      # ['xxxxxxxxxx...', 'a'] False  <- first string truncated, but False

End-to-end reproduction:

  1. Set WORKFLOW_VARIABLE_TRUNCATION_MAX_SIZE=10 (small enough to force truncation).
  2. Build a workflow with a Code node that returns a numeric array:
    def main() -> dict:
        return {"result": [10, 20, 30, 40, 50]}
  3. Run the workflow from Studio (debug run) and inspect the Code node's node_finished SSE event.

Observed node_finished data:

{"node_type":"code","outputs":{"result":[10]},"outputs_truncated":false}

The array was shortened from 5 elements to 1, but outputs_truncated is false.

The two sibling methods in the same file accumulate the flag correctly (truncate_variable_mapping, _truncate_object); only _truncate_array overwrites it instead of accumulating, and its size-limit break does not set the flag at all.

✔️ Expected Behavior

When _truncate_array drops or shortens any array element, the returned truncated flag should be True, and it should be propagated as outputs_truncated / inputs_truncated on the streaming node events — consistent with the element-count-limit path and with the sibling methods truncate_variable_mapping and _truncate_object.

❌ Actual Behavior

_truncate_array returns truncated=False even though elements were dropped or a string was shortened, so outputs_truncated/inputs_truncated is reported as false for data that was actually truncated. Impact is minor/cosmetic — the final workflow_finished output is not truncated and no data is lost — but the per-node truncation flag is wrong.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING