deeplearning4j/deeplearning4j · error · IllegalArgumentException

Data type for condition must be boolean!

Error message

Data type for condition must be boolean!

What it means

LoopLambdaArgs' @Builder constructor validates the condition variable passed into a SameDiff loop: condIn must have DataType.BOOL. Anything else is rejected with IllegalArgumentException because the loop condition is evaluated as a boolean mask.

Solutions

  1. Pass a boolean variable: use eq/gt/lt/and/or ops to produce the condition before constructing LoopLambdaArgs.
  2. Cast explicitly: sameDiff.cast(v, DataType.BOOL) when a boolean interpretation is intended.
  3. Check the variable's datatype (v.dataType()) when constructing args to fail fast in user code.

Example fix

// before
LoopLambdaArgs args = LoopLambdaArgs.builder().condIn(iterCount).build(); // INT
// after
LoopLambdaArgs args = LoopLambdaArgs.builder()
        .condIn(iterCount.lt(maxIters)) // BOOL
        .iterCount(iterCount).build();
Defensive patterns

Strategy: type-guard

Validate before calling

if (condIn.dataType() != DataType.BOOL) {
    throw new IllegalArgumentException("LoopLambdaArgs.condIn must be BOOL, got " + condIn.dataType());
}

Type guard

boolean isBoolVar(SDVariable v) { return v != null && v.dataType() == DataType.BOOL; }

Try / catch

try {
    LoopLambdaArgs args = LoopLambdaArgs.builder().condIn(c).iterCount(n).build();
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("condition must be boolean")) {
        c = c.getSameDiff().cast(c, DataType.BOOL);
        LoopLambdaArgs args = LoopLambdaArgs.builder().condIn(c).iterCount(n).build();
    } else throw e;
}

Prevention

When it happens

Trigger: Building LoopLambdaArgs via LoopLambdaArgs.builder().condIn(v)... where v.dataType() != DataType.BOOL, typically a FLOAT/INT variable instead of the result of a boolean comparison op.

Common situations: Passing a counter or loss variable directly as condIn; forgetting that some comparison ops in older ND4J versions returned numeric types; wiring a while-loop condition from an intermediate numeric tensor.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/adf33b10c00eae52. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/ControlFlow.java:354

                .iterCount(inputs[1])
                .iterStart(inputs[0])
                .condIn(inputs[2])
                .extraArgs(extraArgs)
                .build();
    }

    @Data
    public static class LoopLambdaArgs {

        private SDVariable iterStart;
        private SDVariable iterCount;
        private SDVariable condIn;
        private SDVariable[] extraArgs;

        @Builder
        public LoopLambdaArgs(SDVariable iterStart,SDVariable iterCount,SDVariable[] extraArgs,SDVariable condIn) {
            if(condIn.dataType() != DataType.BOOL) {
                throw new IllegalArgumentException("Data type for condition must be boolean!");
            }

            if(!iterCount.dataType().isNumerical()) {
                throw new IllegalArgumentException("Data type for condition must be numerical!");
            }

            this.iterCount = iterCount;
            this.extraArgs = extraArgs;
            this.condIn = condIn;
            this.iterStart = iterStart;
        }

        /**
         * Construct {@link org.nd4j.linalg.api.ops.custom.Invoke.InvokeParams}
         * for usage with {@link SameDiff#invoke(Invoke.InvokeParams)}
         * the variables here reflect what is used in the loop.
         * A user can use {@link LoopLambdaArgs} to create an appropriately configured
         * {@link org.nd4j.linalg.api.ops.custom.Invoke.InvokeParams} to be used

View on GitHub (pinned to 4c22ac5fe4)