Help :How to implement nested loops #30457
Replies: 3 comments 2 replies
-
|
Hi @AnZheng333! I'm Dosu and I’m helping the dify team. You can implement nested loops in Dify workflows by placing a Loop node or an Iteration node inside the sub-workflow of another Loop or Iteration node. This allows you to create complex, multi-level repetition structures—useful for tasks like processing multi-dimensional data or performing repeated refinements.
Example: Here’s a Mermaid diagram to illustrate: graph TD
A["Start"] --> B["Iteration: For each chapter"]
B --> C["Loop: Refine chapter content"]
C --> D["LLM: Generate/Refine"]
D --> E["Exit Loop (if done)"]
E --> F["End Iteration"]
F --> G["End"]
For more details and examples, see the Dify docs on Loop nodes and Iteration nodes. If you’re looking for a general programming example, a nested loop in Python looks like this: for i in range(3):
for j in range(2):
print(f"Outer: {i}, Inner: {j}")Let me know if you need a step-by-step guide for a specific use case! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
try workflow_as_tool |
Beta Was this translation helpful? Give feedback.
-
|
For nested loops, use the Iteration node with a nested structure:
Alternatively, you can flatten nested data in a Code node first: def main(outer_list: list) -> dict:
flattened = []
for outer in outer_list:
for inner in outer["items"]:
flattened.append({"outer": outer, "inner": inner})
return {"result": flattened}Then iterate over the flattened list once. Which approach fits your use case better? |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.
How to implement nested loops
2. Additional context or comments
No response
Beta Was this translation helpful? Give feedback.
All reactions