keras-team/keras · error · ValueError

KerasTensor cannot have `sparse=True` and `ragged=True` at…

Error message

KerasTensor cannot have `sparse=True` and `ragged=True` at the same time.

What it means

Guard in KerasTensor.__init__: the constructor validated both `sparse` and `ragged` flags and both came out truthy. A tensor representation cannot simultaneously be sparse and ragged in Keras, so the combination is rejected outright.

Solutions

  1. Pass sparse=True only for sparse tensors, or ragged=True only for ragged tensors — never both.
  2. If migrating tf.keras code, check whether the source model wrapped tensors in both RaggedTensor and Sparse encodings and pick one.
  3. Leave both flags at their default False for dense tensors.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/keras_tensor.py:54 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/9bcb42e8f53cd98c. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/common/keras_tensor.py:54

        record_history=True,
        name=None,
        **kwargs,
    ):
        from keras.src import backend

        ragged_rank = kwargs.pop("ragged_rank", None)
        row_splits_dtype = kwargs.pop("row_splits_dtype", None)
        if kwargs:
            raise TypeError(
                f"Unexpected keyword arguments: {', '.join(kwargs.keys())}"
            )

        self._shape = backend.standardize_shape(shape)
        self._dtype = backend.standardize_dtype(dtype)
        self._sparse = bool(sparse)
        self._ragged = bool(ragged)
        if self._sparse and self._ragged:
            raise ValueError(
                "KerasTensor cannot have `sparse=True` and `ragged=True` at "
                "the same time."
            )
        self._ragged_rank = (
            int(ragged_rank) if ragged_rank is not None else None
        )
        self._row_splits_dtype = (
            backend.standardize_dtype(row_splits_dtype)
            if row_splits_dtype is not None
            else None
        )
        self.name = name or auto_name(self.__class__.__name__)
        self.record_history = record_history

    @property
    def shape(self):
        return self._shape

View on GitHub (pinned to 7a34a03db6)