{"record":{"id":"e5b9a2209308e232","repo":"deeplearning4j/deeplearning4j","slug":"base-array-is-not-float","errorCode":null,"errorMessage":"Base array is not float[]","messagePattern":"Base array is not float\\[\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java","lineNumber":3915,"sourceCode":"        stack.push(floatArray);\n\n        int[] shape = arrayShape(floatArray);\n        int length = ArrayUtil.prod(shape);\n        float[] flat = new float[length];\n        int count = 0;\n\n        while (!stack.isEmpty()) {\n            Object current = stack.pop();\n            if (current instanceof float[]) {\n                float[] arr = (float[]) current;\n                for (int i = 0; i < arr.length; i++)\n                    flat[count++] = arr[i];\n            } else if (current instanceof Object[]) {\n                Object[] o = (Object[]) current;\n                for (int i = o.length - 1; i >= 0; i--)\n                    stack.push(o[i]);\n            } else\n                throw new IllegalArgumentException(\"Base array is not float[]\");\n        }\n\n        if (count != flat.length)\n            throw new IllegalArgumentException(\"Fewer elements than expected. Array is ragged?\");\n        return flat;\n    }\n\n    /** Calculate the shape of an arbitrary multi-dimensional array. Assumes:<br>\n     * (a) array is rectangular (not ragged) and first elements (i.e., array[0][0][0]...) are non-null <br>\n     * (b) First elements have > 0 length. So array[0].length > 0, array[0][0].length > 0, etc.<br>\n     * Can pass any Java array opType: double[], Object[][][], float[][], etc.<br>\n     * Length of returned array is number of dimensions; returned[i] is size of ith dimension.\n     */\n    public static int[] arrayShape(Object array) {\n        return arrayShape(array, false);\n    }\n\n    /** Calculate the shape of an arbitrary multi-dimensional array.<br>","sourceCodeStart":3897,"sourceCodeEnd":3933,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java#L3897-L3933","documentation":"ArrayUtil.flatten(float[]) (or flatten(double[]) overloads) iteratively unwraps nested arrays via a stack, expecting every level to be either a primitive float[] or an Object[] of sub-arrays. If any element encountered is neither a float[] nor an Object[] (e.g. a boxed Float, an int[], a String), it cannot be pushed as an array and this IllegalArgumentException is thrown. It is a strict input-shape contract violation.","triggerScenarios":"Calling ArrayUtil.flatten(...) on a nested structure containing a non-float array type (e.g. int[][] or double[][]), or an Object[] leaf holding boxed Float values instead of a float[] base array, or a ragged/null element that isn't an array at all.","commonSituations":"Passing data parsed from JSON or CSV (which yields Object[]/boxed values or int[] arrays) directly into flatten; mixing array types after refactoring from double to float; deep-nested arrays where one level was built as int[].","solutions":["Ensure the base (deepest) level of the nested array is float[] and every intermediate level is Object[] (or float[]) before calling flatten.","Convert mixed-type inputs first: cast/wrap int[] or double[] leaves into float[] via ArrayUtil.toFloat or manual copy.","Use ArrayUtil.flatten(double[]) or the generic flattening matching your actual element type instead of the float variant.","Catch IllegalArgumentException and fall back to a generic reflective flatten if input shape is not guaranteed."],"exampleFix":"// before\nint[][] data = {{1, 2}, {3, 4}};\nfloat[] flat = ArrayUtil.flatten(data); // throws \"Base array is not float[]\"\n// after\nfloat[][] data = {{1f, 2f}, {3f, 4f}};\nfloat[] flat = ArrayUtil.flatten(data);","handlingStrategy":"validation","validationCode":"// Java\nstatic boolean isFlattenableAsFloat(Object arr) {\n    if (!(arr instanceof Object[])) return arr instanceof float[];\n    for (Object o : (Object[]) arr)\n        if (!isFlattenableAsFloat(o)) return false;\n    return true;\n}\n// call: if (!isFlattenableAsFloat(nested)) convert/reject before ArrayUtil.flatten(...)","typeGuard":"static boolean isFloatArray(Object o) { return o instanceof float[] || (o instanceof Object[] && Arrays.stream((Object[]) o).allMatch(ArrayUtilGuard::isFloatArray)); }","tryCatchPattern":"try {\n    float[] flat = ArrayUtil.flatten(nested);\n} catch (IllegalArgumentException e) {\n    // input not float[]-shaped; convert or handle\n}","preventionTips":["Keep nested numeric data as float[][] end-to-end","Convert int[]/double[] leaves to float[] immediately after parsing","Write a shape check helper used before flatten calls","Avoid storing boxed Float objects where primitive arrays are expected"],"tags":["java","nd4j","array-flatten","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}