{"record":{"id":"8b501222f19a1b99","repo":"deeplearning4j/deeplearning4j","slug":"data-type-for-condition-must-be-numerical","errorCode":null,"errorMessage":"Data type for condition must be numerical!","messagePattern":"Data type for condition must be numerical!","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":358,"sourceCode":"                .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\n         * with the body.\n         *\n         *\n         *","sourceCodeStart":340,"sourceCodeEnd":376,"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#L340-L376","documentation":"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.","triggerScenarios":"LoopLambdaArgs.builder().iterCount(v)... where v.dataType().isNumerical() is false, e.g. passing a BOOL variable as the iteration counter.","commonSituations":"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.","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."],"exampleFix":"// before\nLoopLambdaArgs args = LoopLambdaArgs.builder()\n        .iterCount(condBool)   // BOOL\n        .condIn(condBool).build();\n// after\nLoopLambdaArgs args = LoopLambdaArgs.builder()\n        .iterCount(counter)    // numeric INT\n        .condIn(condBool).build();","handlingStrategy":"type-guard","validationCode":"if (iterCount == null || !iterCount.dataType().isNumerical()) {\n    throw new IllegalArgumentException(\"LoopLambdaArgs.iterCount must be numerical, got \" + (iterCount == null ? \"null\" : iterCount.dataType()));\n}","typeGuard":"boolean isNumericVar(SDVariable v) { return v != null && v.dataType().isNumerical(); }","tryCatchPattern":"try {\n    LoopLambdaArgs args = LoopLambdaArgs.builder().iterCount(n).condIn(c).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"must be numerical\")) {\n        n = n.getSameDiff().cast(n, DataType.INT);\n        LoopLambdaArgs args = LoopLambdaArgs.builder().iterCount(n).condIn(c).build();\n    } else throw e;\n}","preventionTips":["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."],"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"}