|
| 1 | +--- |
| 2 | +title: Loop |
| 3 | +description: Create iterative workflows with loops that execute blocks repeatedly |
| 4 | +--- |
| 5 | + |
| 6 | +import { Callout } from 'fumadocs-ui/components/callout' |
| 7 | +import { Step, Steps } from 'fumadocs-ui/components/steps' |
| 8 | +import { Tab, Tabs } from 'fumadocs-ui/components/tabs' |
| 9 | +import { ThemeImage } from '@/components/ui/theme-image' |
| 10 | + |
| 11 | +The Loop block is a container block in Sim Studio that allows you to execute a group of blocks repeatedly. Loops enable iterative processing in your workflows. |
| 12 | + |
| 13 | +<ThemeImage |
| 14 | + lightSrc="/static/light/loop-light.png" |
| 15 | + darkSrc="/static/dark/loop-dark.png" |
| 16 | + alt="Loop Block" |
| 17 | + width={300} |
| 18 | + height={175} |
| 19 | +/> |
| 20 | + |
| 21 | +<Callout type="info"> |
| 22 | + Loop blocks are container nodes that can hold other blocks inside them. The blocks inside a loop will execute multiple times based on your configuration. |
| 23 | +</Callout> |
| 24 | + |
| 25 | +## Overview |
| 26 | + |
| 27 | +The Loop block enables you to: |
| 28 | + |
| 29 | +<Steps> |
| 30 | + <Step> |
| 31 | + <strong>Iterate over collections</strong>: Process arrays or objects one item at a time |
| 32 | + </Step> |
| 33 | + <Step> |
| 34 | + <strong>Repeat operations</strong>: Execute blocks a fixed number of times |
| 35 | + </Step> |
| 36 | +</Steps> |
| 37 | + |
| 38 | +## Configuration Options |
| 39 | + |
| 40 | +### Loop Type |
| 41 | + |
| 42 | +Choose between two types of loops: |
| 43 | + |
| 44 | +<Tabs items={['For Loop', 'ForEach Loop']}> |
| 45 | + <Tab> |
| 46 | + A numeric loop that executes a fixed number of times. Use this when you need to repeat an operation a specific number of times. |
| 47 | + |
| 48 | + ``` |
| 49 | + Example: Run 5 times |
| 50 | + - Iteration 1 |
| 51 | + - Iteration 2 |
| 52 | + - Iteration 3 |
| 53 | + - Iteration 4 |
| 54 | + - Iteration 5 |
| 55 | + ``` |
| 56 | + </Tab> |
| 57 | + <Tab> |
| 58 | + A collection-based loop that iterates over each item in an array or object. Use this when you need to process a collection of items. |
| 59 | + |
| 60 | + ``` |
| 61 | + Example: Process ["apple", "banana", "orange"] |
| 62 | + - Iteration 1: Process "apple" |
| 63 | + - Iteration 2: Process "banana" |
| 64 | + - Iteration 3: Process "orange" |
| 65 | + ``` |
| 66 | + </Tab> |
| 67 | +</Tabs> |
| 68 | + |
| 69 | +## How to Use Loops |
| 70 | + |
| 71 | +### Creating a Loop |
| 72 | + |
| 73 | +1. Drag a Loop block from the toolbar onto your canvas |
| 74 | +2. Configure the loop type and parameters |
| 75 | +3. Drag other blocks inside the loop container |
| 76 | +4. Connect the blocks as needed |
| 77 | + |
| 78 | +### Accessing Results |
| 79 | + |
| 80 | +After a loop completes, you can access aggregated results: |
| 81 | + |
| 82 | +- **`<loop.results>`**: Array of results from all loop iterations |
| 83 | + |
| 84 | +## Example Use Cases |
| 85 | + |
| 86 | +### Processing API Results |
| 87 | + |
| 88 | +<div className="mb-4 rounded-md border p-4"> |
| 89 | + <h4 className="font-medium">Scenario: Process multiple customer records</h4> |
| 90 | + <ol className="list-decimal pl-5 text-sm"> |
| 91 | + <li>API block fetches customer list</li> |
| 92 | + <li>ForEach loop iterates over each customer</li> |
| 93 | + <li>Inside loop: Agent analyzes customer data</li> |
| 94 | + <li>Inside loop: Function stores analysis results</li> |
| 95 | + </ol> |
| 96 | +</div> |
| 97 | + |
| 98 | +### Iterative Content Generation |
| 99 | + |
| 100 | +<div className="mb-4 rounded-md border p-4"> |
| 101 | + <h4 className="font-medium">Scenario: Generate multiple variations</h4> |
| 102 | + <ol className="list-decimal pl-5 text-sm"> |
| 103 | + <li>Set For loop to 5 iterations</li> |
| 104 | + <li>Inside loop: Agent generates content variation</li> |
| 105 | + <li>Inside loop: Evaluator scores the content</li> |
| 106 | + <li>After loop: Function selects best variation</li> |
| 107 | + </ol> |
| 108 | +</div> |
| 109 | + |
| 110 | +## Advanced Features |
| 111 | + |
| 112 | +### Limitations |
| 113 | + |
| 114 | +<Callout type="warning"> |
| 115 | + Container blocks (Loops and Parallels) cannot be nested inside each other. This means: |
| 116 | + - You cannot place a Loop block inside another Loop block |
| 117 | + - You cannot place a Parallel block inside a Loop block |
| 118 | + - You cannot place any container block inside another container block |
| 119 | + |
| 120 | + If you need multi-dimensional iteration, consider restructuring your workflow to use sequential loops or process data in stages. |
| 121 | +</Callout> |
| 122 | + |
| 123 | +<Callout type="info"> |
| 124 | + Loops execute sequentially, not in parallel. If you need concurrent execution, use the Parallel block instead. |
| 125 | +</Callout> |
| 126 | + |
| 127 | +## Inputs and Outputs |
| 128 | + |
| 129 | +<Tabs items={['Configuration', 'Variables', 'Results']}> |
| 130 | + <Tab> |
| 131 | + <ul className="list-disc space-y-2 pl-6"> |
| 132 | + <li> |
| 133 | + <strong>Loop Type</strong>: Choose between 'for' or 'forEach' |
| 134 | + </li> |
| 135 | + <li> |
| 136 | + <strong>Iterations</strong>: Number of times to execute (for loops) |
| 137 | + </li> |
| 138 | + <li> |
| 139 | + <strong>Collection</strong>: Array or object to iterate over (forEach loops) |
| 140 | + </li> |
| 141 | + </ul> |
| 142 | + </Tab> |
| 143 | + <Tab> |
| 144 | + <ul className="list-disc space-y-2 pl-6"> |
| 145 | + <li> |
| 146 | + <strong>loop.currentItem</strong>: Current item being processed |
| 147 | + </li> |
| 148 | + <li> |
| 149 | + <strong>loop.index</strong>: Current iteration number (0-based) |
| 150 | + </li> |
| 151 | + <li> |
| 152 | + <strong>loop.items</strong>: Full collection (forEach loops) |
| 153 | + </li> |
| 154 | + </ul> |
| 155 | + </Tab> |
| 156 | + <Tab> |
| 157 | + <ul className="list-disc space-y-2 pl-6"> |
| 158 | + <li> |
| 159 | + <strong>loop.results</strong>: Array of all iteration results |
| 160 | + </li> |
| 161 | + <li> |
| 162 | + <strong>Structure</strong>: Results maintain iteration order |
| 163 | + </li> |
| 164 | + <li> |
| 165 | + <strong>Access</strong>: Available in blocks after the loop |
| 166 | + </li> |
| 167 | + </ul> |
| 168 | + </Tab> |
| 169 | +</Tabs> |
| 170 | + |
| 171 | +## Best Practices |
| 172 | + |
| 173 | +- **Set reasonable limits**: Keep iteration counts reasonable to avoid long execution times |
| 174 | +- **Use ForEach for collections**: When processing arrays or objects, use ForEach instead of For loops |
| 175 | +- **Handle errors gracefully**: Consider adding error handling inside loops for robust workflows |
0 commit comments