{"record":{"id":"adf33b10c00eae52","repo":"deeplearning4j/deeplearning4j","slug":"data-type-for-condition-must-be-boolean","errorCode":null,"errorMessage":"Data type for condition must be boolean!","messagePattern":"Data type for condition must be boolean!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/ControlFlow.java","lineNumber":354,"sourceCode":"                .iterCount(inputs[1])\n                .iterStart(inputs[0])\n                .condIn(inputs[2])\n                .extraArgs(extraArgs)\n                .build();\n    }\n\n    @Data\n    public static class LoopLambdaArgs {\n\n        private SDVariable iterStart;\n        private SDVariable iterCount;\n        private SDVariable condIn;\n        private SDVariable[] extraArgs;\n\n        @Builder\n        public LoopLambdaArgs(SDVariable iterStart,SDVariable iterCount,SDVariable[] extraArgs,SDVariable condIn) {\n            if(condIn.dataType() != DataType.BOOL) {\n                throw new IllegalArgumentException(\"Data type for condition must be boolean!\");\n            }\n\n            if(!iterCount.dataType().isNumerical()) {\n                throw new IllegalArgumentException(\"Data type for condition must be numerical!\");\n            }\n\n            this.iterCount = iterCount;\n            this.extraArgs = extraArgs;\n            this.condIn = condIn;\n            this.iterStart = iterStart;\n        }\n\n        /**\n         * Construct {@link org.nd4j.linalg.api.ops.custom.Invoke.InvokeParams}\n         * for usage with {@link SameDiff#invoke(Invoke.InvokeParams)}\n         * the variables here reflect what is used in the loop.\n         * A user can use {@link LoopLambdaArgs} to create an appropriately configured\n         * {@link org.nd4j.linalg.api.ops.custom.Invoke.InvokeParams} to be used","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/ControlFlow.java#L336-L372","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a boolean variable: use eq/gt/lt/and/or ops to produce the condition before constructing LoopLambdaArgs.","Cast explicitly: sameDiff.cast(v, DataType.BOOL) when a boolean interpretation is intended.","Check the variable's datatype (v.dataType()) when constructing args to fail fast in user code."],"exampleFix":"// before\nLoopLambdaArgs args = LoopLambdaArgs.builder().condIn(iterCount).build(); // INT\n// after\nLoopLambdaArgs args = LoopLambdaArgs.builder()\n        .condIn(iterCount.lt(maxIters)) // BOOL\n        .iterCount(iterCount).build();","handlingStrategy":"type-guard","validationCode":"if (condIn.dataType() != DataType.BOOL) {\n    throw new IllegalArgumentException(\"LoopLambdaArgs.condIn must be BOOL, got \" + condIn.dataType());\n}","typeGuard":"boolean isBoolVar(SDVariable v) { return v != null && v.dataType() == DataType.BOOL; }","tryCatchPattern":"try {\n    LoopLambdaArgs args = LoopLambdaArgs.builder().condIn(c).iterCount(n).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"condition must be boolean\")) {\n        c = c.getSameDiff().cast(c, DataType.BOOL);\n        LoopLambdaArgs args = LoopLambdaArgs.builder().condIn(c).iterCount(n).build();\n    } else throw e;\n}","preventionTips":["Verify condIn.dataType() == DataType.BOOL before building LoopLambdaArgs.","Produce conditions only from boolean-producing ops.","Name condition variables with a 'cond'/'mask' prefix to make misuse obvious."],"tags":["nd4j","samediff","control-flow","dtype"],"backgroundTag":"dtype-mismatch","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}