Keras
AttributeError
Python
Troubleshooting
MachineLearning

Keras AttributeError 'list' object has no attribute 'ndim'

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error AttributeError: 'list' object has no attribute 'ndim' usually means Keras expected an array-like object with shape metadata, but received a plain Python list instead. The fix is often simple: convert your input or labels to NumPy arrays or tensors, and make sure their shapes match what the model expects.

Why Keras Cares About ndim

Keras needs to know how many dimensions an input has so it can validate batch shape, feature shape, and layer compatibility. NumPy arrays and tensors expose that information through attributes such as shape and ndim. Plain Python lists do not.

That is why code like this often fails:

python
1import keras
2from keras import layers
3
4model = keras.Sequential(
5    [
6        layers.Input(shape=(2,)),
7        layers.Dense(8, activation="relu"),
8        layers.Dense(1),
9    ]
10)
11
12x = [[1.0, 2.0], [3.0, 4.0]]
13y = [0.0, 1.0]
14
15model.compile(optimizer="adam", loss="mse")
16model.fit(x, y, epochs=1)

Depending on the code path and backend, Keras may try to inspect dimensionality and hit a list object that does not provide ndim.

Convert Inputs to Arrays

The most reliable fix is to convert training data to NumPy arrays before passing it to fit, evaluate, or predict.

python
1import numpy as np
2import keras
3from keras import layers
4
5model = keras.Sequential(
6    [
7        layers.Input(shape=(2,)),
8        layers.Dense(8, activation="relu"),
9        layers.Dense(1),
10    ]
11)
12
13x = np.array([[1.0, 2.0], [3.0, 4.0]], dtype="float32")
14y = np.array([0.0, 1.0], dtype="float32")
15
16model.compile(optimizer="adam", loss="mse")
17model.fit(x, y, epochs=1)

Keras documentation for training APIs expects array-like or tensor-like input, not arbitrary nested Python structures.

Check for Ragged or Inconsistent Lists

Another common problem is that the list itself is irregular:

python
x = [[1.0, 2.0], [3.0]]

Even if you convert that to np.array, NumPy may create an object array instead of a numeric matrix because the rows are not the same length. Keras will then fail later with a different shape or dtype error.

So do two checks:

  • convert the data to np.array
  • inspect x.shape and x.dtype

If the shape is not what you expect, fix the data before training.

Lists Are Valid Only in Certain Cases

A plain list is not always wrong. Multi-input Keras models can accept a list of arrays or a list of tensors, because each item represents a separate model input. The key difference is that each element of that outer list should still be an array-like object, not a raw scalar list in an unexpected format.

For example, this is valid for a two-input model:

python
1image_input = np.random.rand(10, 32).astype("float32")
2meta_input = np.random.rand(10, 4).astype("float32")
3
4model.fit([image_input, meta_input], y)

The outer container is a list, but the actual inputs are arrays with defined dimensionality.

Debugging Checklist

Before calling fit, print these values:

python
1print(type(x))
2print(getattr(x, "shape", None))
3print(type(y))
4print(getattr(y, "shape", None))

If you see plain list objects where you expected arrays, convert them. If you see arrays with unexpected shapes, fix the upstream preprocessing step.

This quick inspection is often faster than reading a long stack trace.

Common Pitfalls

Passing Python lists directly into Keras works in some simple cases but is less predictable than using explicit NumPy arrays or tensors.

Converting an irregular nested list to np.array can produce an object array rather than a numeric tensor, which leads to new errors.

Labels can cause the same problem as features. Convert both x and y when training.

For multi-input models, each input should be its own array. A single list of mixed structures is not the same thing.

Debugging only the model and not the data pipeline wastes time because this error is usually about input structure, not layer math.

Summary

  • The error appears when Keras expects an array-like object with ndim but receives a plain list.
  • Convert features and labels to NumPy arrays or tensors before training.
  • Inspect shape and dtype to catch ragged or malformed data.
  • Lists are valid for multi-input models only when each element is itself an array-like input.
  • Treat this as a data-format problem first, not a model-architecture problem.

Course illustration
Course illustration

All Rights Reserved.