keras-team/keras · error · ValueError
Invalid dtype
Error message
Invalid dtype: {dtype} What it means
Raised by standardize_dtype when the input cannot be normalized to a name in keras dtypes.ALLOWED_DTYPES. The helper maps Python/numpy/torch/jax dtype objects to strings, but if the normalized result (often a typo like 'float33' or a backend-specific name) is not an allowed Keras dtype, this generic guard fires.
Solutions
- Use a valid Keras dtype string such as 'float32', 'int32', or 'bfloat16'; check exact spelling.
- Pass numpy/jax/torch dtype objects directly instead of hand-written strings when unsure.
- For custom dtypes, ensure the name attribute maps to an entry in keras.src.backend.common.dtypes.ALLOWED_DTYPES.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/common/variables.py:587 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2ab7af097049d574.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/common/variables.py:587
@keras_export(
["keras.utils.standardize_dtype", "keras.backend.standardize_dtype"]
)
def standardize_dtype(dtype):
if dtype is None:
return config.floatx()
dtype = dtypes.PYTHON_DTYPES_MAP.get(dtype, dtype)
if hasattr(dtype, "name"):
dtype = dtype.name
elif hasattr(dtype, "__name__"):
dtype = dtype.__name__
elif hasattr(dtype, "__str__") and (
"torch" in str(dtype) or "jax.numpy" in str(dtype)
):
dtype = str(dtype).split(".")[-1]
if dtype not in dtypes.ALLOWED_DTYPES:
raise ValueError(f"Invalid dtype: {dtype}")
return dtype
def standardize_shape(shape):
if not isinstance(shape, tuple):
if shape is None:
raise ValueError("Undefined shapes are not supported.")
if not hasattr(shape, "__iter__"):
raise ValueError(f"Cannot convert '{shape}' to a shape.")
if config.backend() == "tensorflow":
if isinstance(shape, tf.TensorShape):
# `tf.TensorShape` may contain `Dimension` objects.
# We need to convert the items in it to either int or `None`
shape = shape.as_list()
if config.backend() == "jax":
# Replace `_DimExpr` (dimension expression) with None
from jax import export as jax_exportView on GitHub (pinned to 7a34a03db6)