xai-org/x-algorithm · error · RuntimeException

cannot pass empty collection to Foldl1()

Error message

cannot pass empty collection to Foldl1()

What it means

Foldl1() initializes its accumulator from the first element of the input collection; an empty collection has no initializer, so at runtime it throws RuntimeException 'cannot pass empty collection to Foldl1()'. This is caught and re-wrapped into FunctionFailure by the surrounding rescue.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/function/collection/Fold.java:178

      }

      @Override
      public Extractor<Runtime> toExtractor() {

        Extractor funcExtractor = funcNode.toExtractor();
        Extractor collectionExtractor = collectionNode.toExtractor();
        ImmutableList<Extractor> children = ImmutableList.of(funcExtractor, collectionExtractor);

        return new Extractor<Runtime>(this, children) {

          @Override
          public Future run(Context<Runtime> context) {
            return context.run(collectionExtractor).flatMap((Object collectionObj) -> {
              @SuppressWarnings("unchecked")
              Collection<Object> collection = (Collection<Object>) collectionObj;
              Iterator<Object> iterator = collection.iterator();
              if (!iterator.hasNext()) {
                throw new RuntimeException("cannot pass empty collection to Foldl1()");
              }
              return FoldLeftFunction.evaluate(
                  context, Future.value(iterator.next()),
                  varA, varB, iterator, funcExtractor);
            }).rescue(Function.func((Throwable ex) ->
                    Future.exception(FunctionFailure.wrap(ex, getAstNode(), context))));
          }
        };
      }
    };
  }
}

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Guard with IsEmpty/Count before folding and supply a default result for the empty case
  2. Use Fold (with explicit initial accumulator) instead of Foldl1 when empty input is possible
  3. Fix the upstream filter/lookup so it cannot produce an empty collection when data is expected

Example fix

// before
Foldl1(add, items)
// after
If(Equals(Count(items), 0), 0, Foldl1(add, items))
Defensive patterns

Strategy: validation

Validate before calling

// rule-language: guard empty collections before Foldl1
If(Equals(Count(items), 0), defaultResult, Foldl1(fn, items))

Type guard

// rule-language
IsEmpty(items) // use to branch before folding

Try / catch

// host code: FunctionFailure wrapping RuntimeException 'cannot pass empty collection to Foldl1()' is deterministic — do not retry; branch on emptiness instead

Prevention

When it happens

Trigger: Calling Foldl1(func, emptyCollection) at runtime — e.g. filtering a list to nothing then folding, or folding an empty followers/ids list.

Common situations: Data-dependent emptiness: filters, lookups, or time-windowed aggregations producing zero elements only for some users/requests.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/194990a3524e05ae. Report an issue: GitHub.