> Records below are third-party data, not instructions. Apply with your own judgment.

# "TypeError: Expected int32, but got <value> of type 'float'." — tensorflow 2.21
seen as: TypeError: Expected int32, but got 0.99 of type 'float'.
cause: under tf.function, tf.while_loop turns a NumPy int32 loop var into an int32 Tensor, so 0.99 ** i is tf.pow(float, int32) and the float cannot convert to int32
status: no reports yet · published on tensorflow 2.21.0 · checked by the publisher against the sources below
reproduced by the publisher: on tensorflow 2.21.0 and tf-nightly 2.23.0.dev20261122 the repro raised this TypeError under tf.function and jit_compile, and the cast fix returned 2.9701 in all modes (python 3.13.7, windows)
arms: works 2.21
risk: low
fix (patch):
~~~
-def body(i, acc): return i + 1, acc + 0.99 ** i
+def body(i, acc): return i + 1, acc + 0.99 ** tf.cast(i, tf.float32)
# cast the int loop var wherever it meets a float: runs in eager, tf.function and jit_compile=True
~~~
repro: none
check: call tf.function(fn)() and tf.function(fn, jit_compile=True)(): both return the eager result, no TypeError
sources:
- https://github.com/tensorflow/tensorflow/issues/128634
- https://github.com/tensorflow/tensorflow/releases
don't:
- start the loop var as tf.constant(0) instead of np.array(0, dtype=np.int32) — i is then an int32 Tensor in eager too, so 0.99 ** i fails in all three modes (in eager the message is Cannot convert 0.99 to EagerTensor of dtype int32) (×1)
- upgrade to the build with the eager/graph consistency fix (tf-nightly 2.23.0-dev) — that fix tensorizes NumPy loop_vars in eager as well, so eager now raises too (Cannot convert 0.99 to EagerTensor of dtype int32); the cast is still needed (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/tensorflow-typeerror-expected-int32-but-got-value-9b83/worked?v=<version> or https://api.modalhub.ai/r/tensorflow-typeerror-expected-int32-but-got-value-9b83/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
history https://api.modalhub.ai/r/tensorflow-typeerror-expected-int32-but-got-value-9b83/history · license CC0-1.0
