ForEach loop
The ForEach loop executes a sub-process for each record from an upstream operator's output. It is ideal for one-to-many scenarios where an upstream operator generates multiple output records from a single input, each requiring further processing.
Prerequisites
-
To use a real-time workflow, you must purchase the Unstructured Data-Enterprise Edition for your current instance.
-
To use an online workflow, you must purchase the Unstructured Data-Enterprise Edition for your current instance.
-
You have created a real-time workflow or an online workflow. For more information, see Create a workflow.
Limitations
-
The ForEach loop is currently supported only in real-time workflows and online workflows.
-
The ForEach loop supports nesting up to three levels deep.
Permissions
In a Basic project, project administrators, developers, analysts, and custom project roles with the Dataset-Use permission can use any dataset in the project as an operator's input or output.
Background
The ForEach loop is a container-type control flow operator. It iterates over an array from an upstream operator's output, executing a sub-DAG for each item. A typical use case is batch processing PDF documents page by page or chunk by chunk after parsing for tasks such as vectorization or information extraction.
Procedure
-
On the Dataphin homepage, in the top navigation bar, choose R&D > Data Development.
-
On the Develop page, select a project from the top menu bar.
-
In the left navigation bar, select Data Processing > Workflow, click the target workflow in the Workflow list on the left to open the workflow canvas, drag the ForEach loop operator from the operator library on the left to the canvas, and configure the following parameters in the ForEach loop panel on the right.
Parameter
Description
Step name
Enter a name for the operator. The name can contain any characters and must be no more than 256 characters long.
Description
Enter a description for the ForEach loop operator.
Loop configuration
Iteration source
Select an array field from the output of an upstream operator. The loop will iterate over each element in this array. For example,
text_chunk_split.chunk(Array<Object>).Execution mode
Select the execution mode for the loop's iterations. The default is parallel.
-
Parallel: Iterations run concurrently. This mode provides higher throughput but does not guarantee the processing order of the items.
-
Serial: Iterations run sequentially, one after another. This mode is suitable for scenarios with stateful dependencies.
Maximum parallelism (Optional)
The maximum number of iterations that can run concurrently. The value must be between 1 and 128. The default is 10. We recommend setting this value to match the Queries Per Second (QPS) limit of the downstream service.
NoteThis parameter is available only when the execution mode is set to parallel.
Maximum iterations (Optional)
This parameter sets a safety limit on the number of iterations. Items that exceed this limit are skipped and recorded as dirty data. The default is 5,000.
Exception handling
Failure policy
-
Skip and record: Failed iterations are recorded as dirty data, and the loop continues to process the remaining iterations.
-
Terminate loop: If any iteration fails, the system immediately terminates the loop but retains the results of completed iterations.
-
Terminate workflow: If any iteration fails, the entire workflow fails and does not execute any downstream operators.
Item timeout (Optional)
If enabled, you can set a Timeout (seconds) for each individual iteration. The default is 300 seconds.
Output configuration
The output mode for the ForEach loop is fixed to array aggregation. It collects the outputs of all iterations into arrays based on their respective branches. After all iterations are complete, it passes these arrays to downstream operators as a whole. You can modify the declared field names or add output fields from any operator within the loop body.
When the ForEach loop contains multiple branches, the output from the final operator of each branch is independently aggregated into an array. Each output array requires a custom name that contains only English characters and punctuation. The system automatically generates an output name based on the final operator's English name in the format
operator_name_results, which you can modify.Downstream operators reference the declared fields, not the actual output of the final operators. The reference format is as follows:
-
Scalar field (expanded into multiple rows in List mode):
ForEach.{array_name}[*].{field_name} -
Entire array (mapped to an Array parameter in GET mode):
ForEach.{array_name}
Common statistics
The ForEach loop always outputs the following fields that downstream operators can use:
-
total_count: The total number of iterations. -
success_count: The number of successful iterations. -
fail_count: The number of failed iterations (skipped items). -
truncated: Indicates whether truncation occurred due to the maximum iteration limit.
-