keras-team/keras · error · ValueError

All tensors passed to `ops.shape` must have a statically…

Error message

All tensors passed to `ops.shape` must have a statically known rank. Received: x={x} with unknown rank.

What it means

Raised by keras.ops.shape in the TensorFlow backend when the tensor's rank is not statically known. The Keras shape op returns a tuple of ints/dims, so it needs static rank; fully dynamic rank cannot be represented and triggers this guard.

Solutions

  1. Use tf.shape(x) (runtime shape tensor) when rank/shape are dynamic
  2. Rebuild the graph so rank is static (e.g. avoid rank-polymorphic ops)
  3. Call shape on a KerasTensor, whose symbolic rank is always known
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/tensorflow/core.py:194 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/39399f4b88db584f. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/tensorflow/core.py:194

def is_tensor(x):
    return tf.is_tensor(x)


def shape(x):
    """Always return a tuple shape.

    `tf.shape` will return a `tf.Tensor`, which differs from the tuple return
    type on the torch and jax backends. We write our own method instead which
    always returns a tuple, with integer values when the shape is known, and
    tensor values when the shape is unknown (this is tf specific, as dynamic
    shapes do not apply in other backends).
    """
    if isinstance(x, KerasTensor):
        return x.shape
    if not tf.is_tensor(x):
        x = tf.convert_to_tensor(x)
    if x.shape == tf.TensorShape(None):
        raise ValueError(
            "All tensors passed to `ops.shape` must have a statically known "
            f"rank. Received: x={x} with unknown rank."
        )
    shape = x.shape.as_list()
    dynamic = tf.shape(x)
    for i in range(len(shape)):
        if shape[i] is None:
            try:
                shape[i] = dynamic[i]
            except:
                # With RaggedTensors, accessing a ragged dimension will fail,
                # we leave it as None.
                pass
    return tuple(shape)


def cast(x, dtype):
    dtype = standardize_dtype(dtype)

View on GitHub (pinned to 7a34a03db6)