Keras ValueError Failed to convert a NumPy array to a Tensor Unsupported object type float
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This Keras error usually means your input looks numeric to a human but not to TensorFlow. The most common cause is an array or DataFrame column that has fallen back to dtype=object, often because of mixed values, missing data, or uneven row shapes.
What the message is really telling you
The wording "unsupported object type float" is confusing because normal floating-point tensors are valid. The key word is object. TensorFlow is not receiving a clean numeric array. It is receiving Python objects, and some of those objects happen to be floats.
That often happens in three situations:
- a NumPy array contains mixed types such as numbers plus strings
- a pandas column is
objectbecause one row is malformed - nested rows have different lengths, so NumPy builds an object array instead of a dense matrix
Before training, always inspect the shape and dtype of what you pass to model.fit.
Converting data to a real numeric tensor input
A reliable fix is to coerce the data to a single numeric dtype before it reaches Keras.
This works because X and y are dense arrays with a consistent numeric type. Keras can convert them to tensors without ambiguity.
How bad input sneaks in
Here is a small example of data that looks close to valid but fails because the array becomes an object array.
The presence of a string means NumPy no longer treats the matrix as a regular numeric block. The fix is not to cast blindly and hope for the best. First clean the source data, then convert it with a deliberate dtype such as np.float32.
For pandas input, df.dtypes is often the fastest diagnostic. If a feature column shows object, inspect it for missing strings, stray whitespace, or inconsistent preprocessing.
A debugging checklist
When this error appears, inspect these four things in order:
- Print
X.dtypeandy.dtype. - Print
X.shapeand verify each row has the same width. - Check pandas column dtypes before converting to NumPy.
- Coerce numeric columns with explicit conversion and handle missing values before model training.
If you have text features, do not feed raw strings into a dense numeric model. Encode them first with tokenization, one-hot encoding, embeddings, or another preprocessing step that produces numeric tensors.
Common Pitfalls
- Assuming that "float" in the error message means TensorFlow rejects floating-point numbers. The actual issue is usually
dtype=object. - Converting a malformed array with
astypewithout checking whether some values becomenan. - Building NumPy arrays from rows of different lengths and accidentally creating ragged object arrays.
- Passing pandas columns with mixed strings and numbers directly to Keras.
- Debugging the model architecture first when the problem is really in data preparation.
Summary
- This error usually means Keras received an object array instead of a dense numeric array.
- Check
dtypeandshapebefore callingmodel.fit. - Convert inputs explicitly with a numeric dtype such as
np.float32. - Clean mixed values, missing values, and uneven rows before creating tensors.
- When the input pipeline is consistent, the Keras side of the error usually disappears.

