Reduce Block

Iterate over arrays with an accumulator pattern

The Reduce block enables powerful looping patterns in Scout workflows. It calls a sub-workflow for each item in an array, passing along an accumulator that builds up results—similar to the reduce function in JavaScript or Python.

New to iteration? The Reduce block is perfect when you need to process items sequentially and each step depends on the previous result. More iteration blocks (like Map for parallel processing) are coming soon!

How It Works

1

Provide an array of items

Either hardcode a JSON array directly (e.g., ["a", "b", "c"]) or reference data from another block using Jinja (e.g., {{ block_id.output | tojson }}).

2

Select a reducer workflow

Choose a published workflow that will process each item. This workflow receives two inputs: accumulator (the running result) and item (the current array element).

3

Set an initial value (optional)

Define the starting value for your accumulator. This could be an empty array [], an empty object {}, a number 0, or any other value.

4

Get the final result

After all items are processed, the final accumulated value is returned as the block’s output.

Configuration

Items
codeRequired

Array to iterate over. You can either hardcode an array or use a Jinja template to reference data from other blocks.

Hardcoded array:

1["apple", "banana", "cherry"]

Reference from another block (use | tojson to ensure proper formatting):

1{{ block_id.output | tojson }}

The value should be a valid JSON array. If it evaluates to a single value, it will be wrapped in an array automatically.

Reducer Workflow
selectRequired

The workflow to call for each item. This workflow must:

  • Be published (drafts won’t appear in the dropdown)
  • Handle two inputs: accumulator and item
  • Return a value that becomes the new accumulator
Initial Value
code

Starting value for the accumulator. Supports Jinja templating.

Common initial values:

  • [] — for building arrays
  • {} — for building objects
  • 0 — for counting or summing
  • "" — for string concatenation

If not specified, defaults to an empty string.

Output Block
select

Explicitly select which block’s output in the reducer workflow becomes the new accumulator.

If not specified, the workflow’s designated output block (marked with is_output) is used.

This is useful when your reducer workflow has multiple potential output blocks and you need precise control over which one feeds into the next iteration.

Creating a Reducer Workflow

Your reducer workflow needs to handle two special inputs and produce one output.

Inputs Your Reducer Receives

InputTypeDescription
accumulatoranyThe current accumulated value. On first iteration, this is your Initial Value.
itemanyThe current item from the array being processed.

Accessing Inputs

Use Jinja templates in your reducer workflow’s blocks to access these inputs:

1{# Access the current item #}
2{{ inputs.item }}
3
4{# Access the current accumulator #}
5{{ inputs.accumulator }}
6
7{# Example: Append processed item to accumulator array #}
8{{ inputs.accumulator + [inputs.item.name] }}

Outputs

output
any

The final accumulated value after processing all items.

details.iterations
integer

Number of iterations completed.

details.items_processed
integer

Number of items successfully processed.

Examples

Summarize Multiple Documents

Process a list of documents and build a combined summary.

Setup:

  • Items: {{ fetch_documents.output }}
  • Initial Value: "No documents processed yet."
  • Reducer Workflow: A workflow with an LLM block

Reducer Workflow Logic:

1System: You are a summarization assistant.
2
3User: Here is the summary so far:
4{{ inputs.accumulator }}
5
6Add this new document to the summary:
7{{ inputs.item.content }}
8
9Provide an updated combined summary.

Result: A single summary that incorporates all documents.

Collect Results into an Array

Process items and collect the results into an array.

Setup:

  • Items: {{ input.items }}
  • Initial Value: []
  • Reducer Workflow: Process each item and append to array

Reducer Workflow Logic (JSON block):

1{{ inputs.accumulator + [{"original": inputs.item, "processed": true}] }}

Result: An array of processed items.

Calculate a Running Total

Sum up values from an array of numbers.

Setup:

  • Items: {{ data_block.output.numbers }}
  • Initial Value: 0
  • Reducer Workflow: Simple addition

Reducer Workflow Logic (JSON block):

1{{ inputs.accumulator + inputs.item }}

Result: The sum of all numbers in the array.

Build an Object from Array

Transform an array into a keyed object.

Setup:

  • Items: {{ users_block.output }}
  • Initial Value: {}
  • Reducer Workflow: Add each item as a key-value pair

Reducer Workflow Logic (JSON block):

1{% set updated = inputs.accumulator.copy() %}
2{% set _ = updated.update({inputs.item.id: inputs.item}) %}
3{{ updated }}

Result: An object with user IDs as keys.

Best Practices

  • Keep reducer workflows simple — Each iteration adds latency, so minimize complexity in your reducer workflow.
  • Match initial value types — Your initial value should match what you’re building (array for arrays, object for objects, etc.).
  • Test with small arrays first — Debug your reducer logic with 2-3 items before processing large datasets.
  • Use the Output Block selector — When your reducer has multiple blocks, explicitly select which output to use for clarity.
  • Consider memory — If accumulating large objects, be mindful that each iteration passes the full accumulator.

Limitations

LimitationDetails
Max call depth10 levels of nested workflows to prevent infinite recursion
Sequential processingItems are processed one at a time, not in parallel
Published workflows onlyReducer workflow must be published to be selectable
No break/continueCannot exit early; all items will be processed

Common Issues

Solution: Make sure your reducer workflow is published. Draft workflows won’t appear in the selector. Go to your reducer workflow and click “Publish” to create a published revision.

Solution: Check that your reducer workflow’s output block is correctly configured. Use the Output Block selector to explicitly choose which block’s output becomes the accumulator.

Solution: You have nested Reduce blocks (or other sub-workflow blocks) that exceed 10 levels deep. Restructure your workflow to reduce nesting, or combine logic into fewer workflows.

See Workflow Logic & State for details on using dynamic variables, and Jinja Templates for advanced templating features.