Fixed: TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32
Dividing a tensor of pixels by 255.0 stopped with TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32. The pixels were whole numbers of type uint8 and 255.0 became a float32, and TensorFlow's / refuses to mix the two. Casting the pixels to float32 first fixed it.
What I Was Doing
I was scaling pixel values from the range 0 to 255 down to 0 to 1, a usual first step before giving images to a model. The pixels were a small uint8 tensor, the type a decoded JPEG has:
import tensorflow as tf
pixels = tf.constant([[0, 64, 128], [192, 255, 32]], dtype=tf.uint8)
scaled = pixels / 255.0
print(scaled)
I ran it in a uv project with TensorFlow 2.22.0:
uv run python scale_pixels.py
What I Expected
A tensor of decimals between 0 and 1: 0 for black, 1 for 255, and about 0.25 for 64.
What I Got
Traceback (most recent call last):
File "/Users/you/source/fx-truediv-dtype/scale_pixels.py", line 5, in <module>
scaled = pixels / 255.0
~~~~~~~^~~~~~~
File "/Users/you/source/fx-truediv-dtype/.venv/lib/python3.14/site-packages/tensorflow/python/util/traceback_utils.py", line 167, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/you/source/fx-truediv-dtype/.venv/lib/python3.14/site-packages/tensorflow/python/ops/math_ops.py", line 1505, in _truediv_python3
raise TypeError(f"`x` and `y` must have the same dtype, "
f"got {x_dtype!r} != {y_dtype!r}.")
TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32.

The script stopped at line 5, the division, and printed nothing.
What I Checked
1. What x and y are
In pixels / 255.0, x is the left side and y the right. x is pixels, created with dtype=tf.uint8. y is the Python number 255.0, which TensorFlow had turned into a tf.float32 tensor.
2. Where the error comes from
The traceback ends in _truediv_python3 in TensorFlow's math_ops.py, the function behind /. In TensorFlow's source, it turns y into a tensor with dtype_hint=x.dtype.base_dtype: it tries x's type first. 255.0 is a decimal and can't be a uint8, so it became float32. The next lines compare the two types and raise this TypeError when they differ.
3. What the documentation says
The tf.math.truediv API page states the rule: "x and y must have the same numeric type." The Raises section lists this error: "TypeError: If x and y have different dtypes."
Why It Happens
/ divides only tensors of the same type, and TensorFlow doesn't convert one side to match the other. The pixels were uint8 and 255.0 was float32, so the division stopped before it started.
The Fix
Cast the pixels to float32 first, so both sides are decimals:
import tensorflow as tf
pixels = tf.constant([[0, 64, 128], [192, 255, 32]], dtype=tf.uint8)
scaled = tf.cast(pixels, tf.float32) / 255.0
print(scaled)
tf.cast(pixels, tf.float32) makes a float32 copy of the pixels, so x and y now have the same type.
Another way: divide by the whole number 255
pixels / 255 worked and returned float32. 255 fits in a uint8, so both sides were uint8. For whole-number inputs, the tf.math.truediv page says the inputs "are cast to floating types first", and TensorFlow's source casts uint8 to float32.
Another way: let TensorFlow convert the image
tf.image.convert_image_dtype(pixels, tf.float32) returned the same values. It knows that uint8 images run from 0 to 255 and scales them to 0 to 1 in one step.
After the Fix
uv run python scale_pixels.py
tf.Tensor(
[[0. 0.2509804 0.5019608]
[0.7529412 1. 0.1254902]], shape=(2, 3), dtype=float32)

Every value is now between 0 and 1: 64 became 0.2509804 (64 ÷ 255) and 255 became 1.. The result is float32. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Casting after dividing: tf.cast(pixels / 255.0, tf.float32) | The same TypeError: the division still runs first, on uint8 | Cast pixels, then divide |
Casting to a whole-number type: tf.cast(pixels, tf.int32) / 255.0 | TypeError: `x` and `y` must have the same dtype, got tf.int32 != tf.float32. | Cast to tf.float32 |
Switching to tf.divide(pixels, 255.0) | The same TypeError | tf.divide follows the same rule: cast first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
got tf.uint8 != tf.float32 | A uint8 tensor divided by a decimal | tf.cast(tensor, tf.float32) / 255.0 |
got tf.int32 != tf.float32 | A whole-number tensor divided by a decimal | Cast the tensor to tf.float32 first |
cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor | Adding a float32 tensor to a uint8 one: the same mismatch, with + | Fixed: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor |
FAQ
Why does pixels / 255 work when pixels / 255.0 doesn't?
255 is a whole number that fits in uint8, so it becomes a uint8 tensor, the same type as pixels. 255.0 is a decimal, so it can't. With both sides uint8, / converts them to float32 before dividing.
Why scale the pixels to 0–1 at all?
TensorFlow's image loading tutorial explains: "The RGB channel values are in the [0, 255] range. This is not ideal for a neural network; in general you should seek to make your input values small."
Conclusion
TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32. meant the uint8 pixels and the float32 number 255.0 couldn't be divided. tf.cast(pixels, tf.float32) / 255.0 gave a float32 tensor with every value between 0 and 1.
Additional Resources
- tf.math.truediv — the rule for both sides' types.
- tf.cast — converting a tensor's type.
- tf.image.convert_image_dtype — converting and scaling images in one step.
