deeplearning4j/deeplearning4j · error · IllegalStateException

Unknown array type

Error message

Unknown array type

What it means

At the deepest level of arrayShape(), the code dispatches on the runtime type of the innermost element to read its length; it recognizes float[], int[], double[], long[], char[], boolean[] and short[]. If the element is any other type (a non-array object, a String, Integer, boxed value, or exotic primitive array like byte[] depending on the chain), no branch matches and an IllegalStateException('Unknown array type') is thrown. The comment notes it 'should never happen' for well-formed arrays of supported types.

Solutions

  1. Ensure leaves are primitive arrays of the supported types (float[], int[], double[], long[], char[], boolean[], short[]).
  2. Convert unsupported leaves first, e.g. copy byte[] to int[] or unbox Float[] to float[], before computing shape.
  3. Use ArrayUtil.length(Object)/dimensions-based helpers or write a custom shape walker for custom types.
  4. Catch IllegalStateException and reject/log the malformed input.

Example fix

// before
Object[] nested = { new String[]{"a", "b"} };
long[] shape = ArrayUtil.arrayShape(nested); // throws Unknown array type
// after
Object[] nested = { new float[]{1f, 2f} };
long[] shape = ArrayUtil.arrayShape(nested); // [1, 2]
Defensive patterns

Strategy: type-guard

Validate before calling

// Java
static boolean hasSupportedLeaf(Object o) {
    if (o instanceof Object[]) {
        for (Object e : (Object[]) o) if (!hasSupportedLeaf(e)) return false;
        return true;
    }
    return o instanceof float[] || o instanceof int[] || o instanceof double[]
        || o instanceof long[] || o instanceof char[] || o instanceof boolean[] || o instanceof short[];
}

Type guard

static boolean isSupportedPrimitiveArray(Object o) { return o instanceof float[] || o instanceof int[] || o instanceof double[] || o instanceof long[] || o instanceof char[] || o instanceof boolean[] || o instanceof short[]; }

Try / catch

try {
    long[] shape = ArrayUtil.arrayShape(nested);
} catch (IllegalStateException e) {
    // leaf type unsupported; convert or use custom shape logic
}

Prevention

When it happens

Trigger: Calling ArrayUtil.arrayShape(...) on a nested Object[] whose leaf elements are not one of the supported primitive arrays - e.g. leaf is a String, boxed Float, byte[], or an arbitrary POJO.

Common situations: Deserialized or reflective data where leaves are boxed or byte[]; mixing object graphs (e.g. NDArray wrappers) into Object[] trees before shape inspection.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/72704ea5f005c3a5. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java:3980

            shape[shape.length - 1] = ((Object[]) current).length;
        } else if (current instanceof double[]) {
            shape[shape.length - 1] = ((double[]) current).length;
        } else if (current instanceof float[]) {
            shape[shape.length - 1] = ((float[]) current).length;
        } else if (current instanceof long[]) {
            shape[shape.length - 1] = ((long[]) current).length;
        } else if (current instanceof int[]) {
            shape[shape.length - 1] = ((int[]) current).length;
        } else if (current instanceof byte[]) {
            shape[shape.length - 1] = ((byte[]) current).length;
        } else if (current instanceof char[]) {
            shape[shape.length - 1] = ((char[]) current).length;
        } else if (current instanceof boolean[]) {
            shape[shape.length - 1] = ((boolean[]) current).length;
        } else if (current instanceof short[]) {
            shape[shape.length - 1] = ((short[]) current).length;
        } else
            throw new IllegalStateException("Unknown array type"); //Should never happen
        return shape;
    }


    /** Returns the maximum value in the array */
    public static int max(int[] in) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < in.length; i++)
            if (in[i] > max)
                max = in[i];
        return max;
    }

    /** Returns the minimum value in the array */
    public static int min(int[] in) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < in.length; i++)
            if (in[i] < min)

View on GitHub (pinned to 4c22ac5fe4)