How to Check the Data Type of a Tensor in TensorFlow
Every value in a TensorFlow tensor has the same type, its dtype: whole numbers, decimals, true-or-false values or text. Many errors come from two tensors with different dtypes, so it's worth knowing how to check. This guide reads the dtype of four tensors in TensorFlow 2.22.0.
Syntax
<tensor>.dtype
<tensor>.dtype == tf.<data type>
<tensor>.dtype.name
| Part | What it means |
|---|---|
<tensor>.dtype | The data type of every value in the tensor, a tf.dtypes.DType |
== tf.<data type> | True when the tensor has that type, such as tf.int32 |
.dtype.name | The type's name as text, such as "int32" |
The tensor guide starts with it: "Tensors are multi-dimensional arrays with a uniform type (called a dtype)." The DType API page describes the object .dtype returns: it "Represents the type of the elements in a Tensor."
The Example
Four tensors, one of each common kind: whole numbers, decimal numbers, true-or-false values and words.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
whole = tf.constant([1, 2, 3])
decimal = tf.constant([1.5, 2.5])
flags = tf.constant([True, False])
words = tf.constant(["cat", "dog"])
print(whole.dtype)
print(decimal.dtype)
print(flags.dtype)
print(words.dtype)
print(whole.dtype == tf.int32)
print(whole.dtype.name)

No tensor names its type: TensorFlow picks one from the values. The last two lines compare a dtype with tf.int32 and read its name.
Step 2: Run It
uv run python dtypes.py
<dtype: 'int32'>
<dtype: 'float32'>
<dtype: 'bool'>
<dtype: 'string'>
True
int32

| Tensor | dtype | Holds |
|---|---|---|
whole | int32 | Whole numbers, 4 bytes each |
decimal | float32 | Decimal numbers, 4 bytes each |
flags | bool | True or False |
words | string | Text, stored as bytes |
whole.dtype == tf.int32 was True, and .name gave the plain text int32, handy in messages and logs. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Checking the type with Python's type() | type(whole) printed <class 'tensorflow.python.framework.ops.EagerTensor'>, the kind of object, which is the same for every tensor | Read whole.dtype |
FAQ
Why does TensorFlow pick int32 and float32, not 64-bit types?
That's its rule for Python numbers. The tensor guide: "TensorFlow converts Python integers to tf.int32 and Python floating point numbers to tf.float32". A 32-bit value takes 4 bytes, half the 8 bytes of a 64-bit one.
Can I compare a dtype with its name as text?
Yes. whole.dtype == "int32" was True, and whole.dtype == "float32" was False. TensorFlow's DType turns the other side into a dtype before it compares, and returns False when that isn't possible.
Can I mix dtypes in one tensor?
No. A tensor has one dtype for all its values. To combine tensors of different dtypes, convert one with tf.cast first.
Conclusion
.dtype gave int32, float32, bool and string for whole numbers, decimals, true-or-false values and text. It compares with tf.int32 and friends, .dtype.name turns it into text, and type() only says that the object is a tensor.
Additional Resources
- tf.dtypes.DType — what a dtype can tell you.
- Introduction to tensors — tensors and their dtypes.
