deeplearning4j/deeplearning4j · error · IllegalArgumentException
Data type for condition must be numerical!
Error message
Data type for condition must be numerical!
What it means
The same LoopLambdaArgs constructor validates iterCount, which drives loop iteration counts: it must be a numerical datatype (INT, LONG, FLOAT, etc.). Passing a boolean or other non-numerical variable raises IllegalArgumentException. Note the message text is misleading — it says 'condition' but the check is on iterCount.
Solutions
- Ensure iterCount is a numeric variable (INT/LONG/FLOAT); create it with parent.var(..., DataType.INT/LONG) if needed.
- Swap arguments if condIn and iterCount were accidentally interchanged in the builder call.
- Verify with v.dataType().isNumerical() before constructing LoopLambdaArgs.
Example fix
// before
LoopLambdaArgs args = LoopLambdaArgs.builder()
.iterCount(condBool) // BOOL
.condIn(condBool).build();
// after
LoopLambdaArgs args = LoopLambdaArgs.builder()
.iterCount(counter) // numeric INT
.condIn(condBool).build(); Defensive patterns
Strategy: type-guard
Validate before calling
if (iterCount == null || !iterCount.dataType().isNumerical()) {
throw new IllegalArgumentException("LoopLambdaArgs.iterCount must be numerical, got " + (iterCount == null ? "null" : iterCount.dataType()));
} Type guard
boolean isNumericVar(SDVariable v) { return v != null && v.dataType().isNumerical(); } Try / catch
try {
LoopLambdaArgs args = LoopLambdaArgs.builder().iterCount(n).condIn(c).build();
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("must be numerical")) {
n = n.getSameDiff().cast(n, DataType.INT);
LoopLambdaArgs args = LoopLambdaArgs.builder().iterCount(n).condIn(c).build();
} else throw e;
} Prevention
- Keep the builder call's iterCount and condIn arguments in a fixed, commented order to avoid swapping them.
- Create counters with an explicit numeric DataType (INT/LONG).
- Add a helper wrapper around LoopLambdaArgs.builder() that validates both datatypes.
When it happens
Trigger: LoopLambdaArgs.builder().iterCount(v)... where v.dataType().isNumerical() is false, e.g. passing a BOOL variable as the iteration counter.
Common situations: Swapping the builder's condIn and iterCount arguments; casting a boolean mask variable and reusing it as a counter; constructing args from variable arrays in the wrong order after a refactor.
Related errors
- Can not use <pred.name()> as the condition of an If…
- Data type for condition must be boolean!
- Can not use <condResult.name()> as the condition of an…
- Cannot apply operation
- Cannot apply operation
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/8b501222f19a1b99.
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:358
.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
* with the body.
*
*
*View on GitHub (pinned to 4c22ac5fe4)