deeplearning4j/deeplearning4j · error · IllegalArgumentException
Base array is not float[]
Error message
Base array is not float[]
What it means
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.
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.
Example fix
// before
int[][] data = {{1, 2}, {3, 4}};
float[] flat = ArrayUtil.flatten(data); // throws "Base array is not float[]"
// after
float[][] data = {{1f, 2f}, {3f, 4f}};
float[] flat = ArrayUtil.flatten(data); Defensive patterns
Strategy: validation
Validate before calling
// Java
static boolean isFlattenableAsFloat(Object arr) {
if (!(arr instanceof Object[])) return arr instanceof float[];
for (Object o : (Object[]) arr)
if (!isFlattenableAsFloat(o)) return false;
return true;
}
// call: if (!isFlattenableAsFloat(nested)) convert/reject before ArrayUtil.flatten(...) Type guard
static boolean isFloatArray(Object o) { return o instanceof float[] || (o instanceof Object[] && Arrays.stream((Object[]) o).allMatch(ArrayUtilGuard::isFloatArray)); } Try / catch
try {
float[] flat = ArrayUtil.flatten(nested);
} catch (IllegalArgumentException e) {
// input not float[]-shaped; convert or handle
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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[].
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- Can't get value for non-regression Metric
- Can't get value for non-regression Metric
- Can't get value for non-ROC Metric
- Unknown array type (or not an array):
- A columns must equal B rows. MMul attempt:
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/e5b9a2209308e232.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java:3915
stack.push(floatArray);
int[] shape = arrayShape(floatArray);
int length = ArrayUtil.prod(shape);
float[] flat = new float[length];
int count = 0;
while (!stack.isEmpty()) {
Object current = stack.pop();
if (current instanceof float[]) {
float[] arr = (float[]) current;
for (int i = 0; i < arr.length; i++)
flat[count++] = arr[i];
} else if (current instanceof Object[]) {
Object[] o = (Object[]) current;
for (int i = o.length - 1; i >= 0; i--)
stack.push(o[i]);
} else
throw new IllegalArgumentException("Base array is not float[]");
}
if (count != flat.length)
throw new IllegalArgumentException("Fewer elements than expected. Array is ragged?");
return flat;
}
/** Calculate the shape of an arbitrary multi-dimensional array. Assumes:<br>
* (a) array is rectangular (not ragged) and first elements (i.e., array[0][0][0]...) are non-null <br>
* (b) First elements have > 0 length. So array[0].length > 0, array[0][0].length > 0, etc.<br>
* Can pass any Java array opType: double[], Object[][][], float[][], etc.<br>
* Length of returned array is number of dimensions; returned[i] is size of ith dimension.
*/
public static int[] arrayShape(Object array) {
return arrayShape(array, false);
}
/** Calculate the shape of an arbitrary multi-dimensional array.<br>View on GitHub (pinned to 4c22ac5fe4)