keras-team/keras · error · ValueError

Invalid value for argument `precision`. Expected one of…

Error message

Invalid value for argument `precision`. Expected one of ('16', '32', '64'). Received: precision={precision}

What it means

Guard in _resolve_weak_type: the precision used to concretize a weak dtype (e.g. 'float' -> 'float32') must be one of the bit-width strings '16', '32', '64'. Any other precision value has no corresponding concrete dtype to resolve to.

Solutions

  1. Set floatx/precision to '16', '32', or '64' (e.g. keras.config.set_floatx('float16')).
  2. Note bfloat16 has no precision tier; use the explicit 'bfloat16' dtype name instead of a precision value.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/dtypes.py:216 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/a89407b8fc507d0e. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/common/dtypes.py:216

        else:
            raise ValueError(
                "Invalid value for argument `dtype`. Expected one of "
                f"{ALLOWED_DTYPES}. Received: dtype={dtype}"
            )
    return dtype


@functools.lru_cache(maxsize=None)
def _resolve_weak_type(dtype, precision="32"):
    """Resolve weak type by the precision of `backend.floatx()`."""
    extended_allowed_dtypes = set(ALLOWED_DTYPES).union(WEAK_TYPES)
    if dtype not in extended_allowed_dtypes:
        raise ValueError(
            "Invalid value for argument `dtype`. Expected one of "
            f"{extended_allowed_dtypes}. Received: dtype={dtype}"
        )
    if precision not in ["16", "32", "64"]:
        raise ValueError(
            f"Invalid value for argument `precision`. Expected one of "
            f"('16', '32', '64'). Received: precision={precision}"
        )
    if dtype == "bfloat16":  # special case for bfloat16
        dtype_indicator = "f"
    else:
        dtype_indicator = dtype[:1]

    if dtype_indicator == "b":
        return "bool"
    elif dtype_indicator == "i":
        return f"int{precision}"
    elif dtype_indicator == "u":
        return f"uint{precision}"
    else:
        return f"float{precision}"

View on GitHub (pinned to 7a34a03db6)