Loop Controller Block
Create loops in your flow and control the iteration of values
Overview
The Loop Controller Block is a powerful tool that allows you to create loops in your flow. It’s the only block in Lawme that can contain cycles of blocks, including itself. This block maintains the state of the loop and controls the flow of values through it.
Key Features
- Maintains loop state
- Controls value flow through the loop
- Dynamic number of input-default pairs
- Dynamic number of output ports
- Conditional loop continuation
Inputs
If truthy, the loop continues executing. If falsy, the loop stops. Coerced to boolean. Optional, defaults to true if not provided or if the value is “Not Ran”.
The value that changes in each iteration of the loop. The number of inputs is dynamic based on connections. Each input has a corresponding default input.
The initial value for the corresponding Input X. Required for each Input X. If any default input is control-flow-excluded, all outputs will be control-flow-excluded.
Outputs
If the Continue
input is falsy, this output runs with an array of all input values. Does not run if Continue
is truthy, instead outputting a control-flow-excluded value.
The current iteration number, starting at 1 for the first iteration.
The value of the corresponding input or default input. On the first iteration, it’s the value from the default input. On subsequent iterations, it’s the value from the input on the previous iteration. The number of outputs matches the number of inputs.
Editor Settings
The maximum number of iterations the loop can run.
What should happen when max iterations is reached:
error
: Throw an error (default)break
: Break the loop as if Continue was false
Example: AI Chatting To Itself
This example demonstrates a loop that continues indefinitely:
- Add a Loop Controller block to your flow.
- Connect a Chat block to the Loop Controller’s input.
- Combine the existing message history with the AI’s response.
- Feed the combined history back into the Loop Controller.
This loop will never break on its own. It will fail after 100 iterations by default unless “At Max Iterations” is set to “break”.
Example: Iterating Over an Array
This example shows how to iterate over an array and append ” Mapped” to each element:
- Use a Pop Block to remove the first value from the array.
- Append ” Mapped” to the popped value.
- Push the new value into an output array using an Array Block.
- Use a Compare Block to check if the input array is empty and break the loop.
- Connect an Output block to the
Break
output of the Loop Controller.
Error Handling
The Loop Controller Block will error if:
- The loop exceeds the maximum number of iterations and “At Max Iterations” is set to “error”
- Any required default inputs are not connected