Fixed: 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 tensor of pixels stopped with InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:AddV2]. TensorFlow's + only adds tensors of the same type. Casting the pixels to float32 first fixed it.
What I Was Doing
I was brightening three pixel values by adding 5.5 to each. The pixels were uint8, the type a decoded photo has, and the amount to add was a tensor of decimals:
import tensorflow as tf
pixels = tf.constant([10, 120, 240], dtype=tf.uint8)
boost = tf.constant([5.5, 5.5, 5.5])
brighter = pixels + boost
print(brighter)
I ran it in a uv project with TensorFlow 2.22.0:
uv run python brighten.py
What I Expected
[15.5 125.5 245.5]: each pixel plus 5.5.
What I Got
Traceback (most recent call last):
File "/Users/you/source/fx-addv2-dtype/brighten.py", line 6, in <module>
brighter = pixels + boost
~~~~~~~^~~~~~~
File "/Users/you/source/fx-addv2-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-addv2-dtype/.venv/lib/python3.14/site-packages/tensorflow/python/framework/ops.py", line 6084, in raise_from_not_ok_status
raise core._status_to_exception(e) from None # pylint: disable=protected-access
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:AddV2] name:

The script stopped at line 6, the addition, and printed nothing.
What I Checked
1. What each part of the message means
AddV2 is the TensorFlow operation behind +: the traceback points at pixels + boost. Its inputs are counted from 0 ("zero-based"), so input #0 is pixels and input #1 is boost. Input #0 is uint8, so TensorFlow expected input #1 to be uint8 too, and it was a float tensor: tf.constant([5.5, 5.5, 5.5]) makes float32.
2. What the documentation says
The tf.math.add API page lists what its two inputs may be. For the second: "A tf.Tensor. Must have the same type as x."
3. Whether a plain number does the same
pixels + 5.5, with a Python number instead of a tensor, stopped with exactly the same error. 5.5 became a float tensor, and the types still didn't match.
Why It Happens
+ adds only tensors of the same type, and TensorFlow doesn't convert either side to match the other. A uint8 tensor holds whole numbers from 0 to 255, so it can't take part in a sum with decimals until it's converted.
The Fix
Cast the pixels to float32 before adding:
import tensorflow as tf
pixels = tf.constant([10, 120, 240], dtype=tf.uint8)
boost = tf.constant([5.5, 5.5, 5.5])
brighter = tf.cast(pixels, tf.float32) + boost
print(brighter)
tf.cast(pixels, tf.float32) makes a float32 copy of the pixels, so both inputs of + are float32.
After the Fix
uv run python brighten.py
tf.Tensor([ 15.5 125.5 245.5], shape=(3,), dtype=float32)

Each pixel went up by 5.5, and the result is float32. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Casting boost to uint8 instead | pixels + tf.cast(boost, tf.uint8) returned [ 15 125 245]: the cast dropped the .5 before adding | Cast the whole numbers to decimals, not the other way round |
Adding a plain 5.5 | The same error: 5.5 becomes a float tensor | Cast the pixels first |
Swapping the sides: boost + pixels | The message turns round: cannot compute AddV2 as input #1(zero-based) was expected to be a float tensor but is a uint8 tensor [Op:AddV2] | The same fix: cast the uint8 tensor |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot compute AddV2 … expected to be a uint8 tensor but is a float tensor | A float32 tensor added to a uint8 one | tf.cast(pixels, tf.float32) + boost |
cannot compute Mul … or cannot compute Sub … with the same words | * or - with the same mix: pixels * boost gave [Op:Mul], pixels - boost gave [Op:Sub] | Cast first, as for + |
x` and `y` must have the same dtype, got tf.uint8 != tf.float32 | / with the same mix | Fixed: TypeError: x and y must have the same dtype, got tf.uint8 != tf.float32 |
FAQ
What does "zero-based" mean?
That counting starts at 0. An operation's first input is #0 and its second is #1, so in pixels + boost, input #1 is boost.
How do I turn the result back into whole pixel values?
Round, keep it between 0 and 255, and cast: tf.cast(tf.clip_by_value(tf.round(brighter), 0, 255), tf.uint8) returned [ 16 126 246]. tf.round rounds a half to the nearest even number, so 15.5 became 16 and 125.5 became 126.
Conclusion
cannot compute AddV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor meant + got a uint8 tensor and a float32 one. tf.cast(pixels, tf.float32) + boost made both float32, and returned [ 15.5 125.5 245.5].
Additional Resources
- tf.math.add — the rule for both inputs' types.
- tf.cast — converting a tensor's type.
