tensorflow/models · error · ValueError

The slicing spec must be a non-empty dictionary.

Error message

The slicing spec must be a non-empty dictionary.

What it means

Raised in SlicedMetric.__init__ when slicing_spec is empty or not a dict; at least one slice definition is required.

Solutions

  1. Provide a non-empty dict mapping slice names to int/bool/str values
  2. Skip wrapping with SlicedMetric if no slicing is needed
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/recommendation/uplift/metrics/sliced_metric.py:92 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/3724b4ec5324746b. Report an issue: GitHub.

Appendix: source

Thrown at official/recommendation/uplift/metrics/sliced_metric.py:92

    Args:
      metric: A `tf_keras.metrics.Metric` instance.
      slicing_spec: A dictionary that maps from string slice names, to one of
        integer, boolean, or string slicing values.
      slicing_feature_dtype: The expected dtype of the slicing feature. The
        values in the slicing spec are casted to this type if passed. If None,
        the dtype of the slicing feature is inferred based on the values in the
        slicing spec.
      name: The name of the wrapper metric. Defaults to `sliced_{metric.name}`.

    Raises:
      A ValueError if `slicing_spec` is empty, contains duplicate slicing
      values, or has slicing values of different types.
    """
    super().__init__(name=name or f"sliced_{metric.name}", dtype=metric.dtype)

    if not slicing_spec:
      raise ValueError("The slicing spec must be a non-empty dictionary.")

    slice_names, slicing_values = zip(*slicing_spec.items())
    if not isinstance(slicing_values[0], (int, bool, str)) or not all(
        isinstance(k, type(slicing_values[0])) for k in slicing_values
    ):
      raise ValueError(
          "All slicing values in the slicing spec must be one of `int`, "
          "`bool`, or `str`, and all values must have the same type. "
          f"Got types: {list(map(type, slicing_values))}."
      )

    if len(slicing_values) > len(set(slicing_values)):
      raise ValueError(
          "The slicing values passed to the slicing spec must be unique. Got "
          f"{slicing_values}."
      )

    # TODO(b/276811843): Look into validating whether `metric` accepts

View on GitHub (pinned to e006f5f0d5)