Loop variables not accessible outside the loop

Hi community, would appreciate your help with the issue below. I am using the online version of dify.

I am experiencing a critical issue with the Loop node and Variable Assigner in Dify. Although a variable (e.g., research_history) is correctly updated and appended to during each iteration, these changes are lost when the loop terminates. The final output of the Loop node returns an empty state, even though the internal logs confirm the data was successfully processed.

Workarounds Attempted (None of which worked)

  1. Direct Variable Mapping: Mapping the downstream node directly to {{Loop.research_history}}. Result: Received the initial empty array [].

  2. Internal Template Node: Adding a Template Node inside the loop as the final step to “output” the variable. Result: The Template Node’s output was not accessible or visible outside the loop’s scope.

  3. Variable vs. Constant Initialization: Switching the loop variable between “Variable” (which caused an Invalid value error) and “Constant” (which resulted in the persistent empty array).

  4. Global Anchor Idea: Attempting to initialize the variable in a node before the loop and updating it inside. Result: The Variable Assigner inside the loop was restricted to only targeting variables defined within the loop’s own header.

  5. Rename/Reset: Renaming the variables and re-publishing the workflow to clear potential cache issues. Result: No change in behavior.

@gl287
Hi, could you share the DSL file here to help clarify the issue?

Hi, the format is not accepted here as an upload. Please use this file hosting link
thanks!

now that my post has been approved, would be great to get your thoughts @kurokobo . Thanks!

@gl287
Thank you for providing the DSL file.

I imported it into my self-hosted Dify 1.12.1, and after fixing the following two points, things seem to be working properly.

  • :white_check_mark: Loop (Schleife) node
    • For the is_saturated option in Loop Termination Condition, I set it to False once and saved the flow, then set it back to True and saved the flow again.
    • Internally, in your DSL, the value was being stored as a string instead of a boolean (Line 911). Because of this, the loop was effectively being skipped. By switching between True/False and saving, I was able to overwrite the invalid internal value.
  • :white_check_mark: The Code (Search Result Format) node following the Loop (Schleife) node
    • In Output Variables, search_results was specified, but the code block’s return was returning final_research_context. These need to match, so I replaced all three instances of final_research_context in the code with search_results.

In the end, I could not reproduce the issue you mentioned, where “the loop node always returns an empty list.”
However, a quick search turned up some similar reports. It may be a known bug, and if that’s the case, upgrading Dify might solve it.