keras-team/keras · error · ValueError

Invalid `threshold` argument value. It should be a Python fl

Error message

Invalid `threshold` argument value. It should be a Python float. Received: threshold={threshold} of type '{type(threshold)}'

What it means

Raised by FBetaScore's __init__ when threshold is not None and not a Python float. The optional binarizing threshold must be an explicit float; ints like 0 or 1 are rejected.

Source

Thrown at keras/src/metrics/f_score_metrics.py:100

                f"Received: average={average}"
            )

        if not isinstance(beta, float):
            raise ValueError(
                "Invalid `beta` argument value. "
                "It should be a Python float. "
                f"Received: beta={beta} of type '{type(beta)}'"
            )
        if beta <= 0.0:
            raise ValueError(
                "Invalid `beta` argument value. "
                "It should be > 0. "
                f"Received: beta={beta}"
            )

        if threshold is not None:
            if not isinstance(threshold, float):
                raise ValueError(
                    "Invalid `threshold` argument value. "
                    "It should be a Python float. "
                    f"Received: threshold={threshold} "
                    f"of type '{type(threshold)}'"
                )
            if threshold > 1.0 or threshold <= 0.0:
                raise ValueError(
                    "Invalid `threshold` argument value. "
                    "It should verify 0 < threshold <= 1. "
                    f"Received: threshold={threshold}"
                )

        self.average = average
        self.beta = beta
        self.threshold = threshold
        self.axis = None
        self._built = False

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a float: threshold=0.5.
  2. Coerce: threshold=float(cfg['threshold']).
  3. Leave threshold as None (default) when no binarization is wanted.

Example fix

# before
m = keras.metrics.FBetaScore(beta=1.0, threshold=1)

# after
m = keras.metrics.FBetaScore(beta=1.0, threshold=0.5)  # or omit threshold
Defensive patterns

Strategy: type-guard

Validate before calling

threshold = None if threshold is None else float(threshold)

Type guard

def is_float_or_none(v) -> bool:
    return v is None or isinstance(v, float)

Prevention

When it happens

Trigger: keras.metrics.FBetaScore(beta=1.0, threshold=1); threshold loaded from config as int; numpy float64 values.

Common situations: Setting threshold=0 or 1 as ints; JSON configs parsing to int; forgetting the decimal point.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/3039b1eac9f44c8f. Report an issue: GitHub.