{"record":{"id":"39399f4b88db584f","repo":"keras-team/keras","slug":"all-tensors-passed-to-ops-shape-must-have-a-stat","errorCode":null,"errorMessage":"All tensors passed to `ops.shape` must have a statically known rank. Received: x={x} with unknown rank.","messagePattern":"All tensors passed to `ops\\.shape` must have a statically known rank\\. Received: x=(.+?) with unknown rank\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/tensorflow/core.py","lineNumber":194,"sourceCode":"def is_tensor(x):\n    return tf.is_tensor(x)\n\n\ndef shape(x):\n    \"\"\"Always return a tuple shape.\n\n    `tf.shape` will return a `tf.Tensor`, which differs from the tuple return\n    type on the torch and jax backends. We write our own method instead which\n    always returns a tuple, with integer values when the shape is known, and\n    tensor values when the shape is unknown (this is tf specific, as dynamic\n    shapes do not apply in other backends).\n    \"\"\"\n    if isinstance(x, KerasTensor):\n        return x.shape\n    if not tf.is_tensor(x):\n        x = tf.convert_to_tensor(x)\n    if x.shape == tf.TensorShape(None):\n        raise ValueError(\n            \"All tensors passed to `ops.shape` must have a statically known \"\n            f\"rank. Received: x={x} with unknown rank.\"\n        )\n    shape = x.shape.as_list()\n    dynamic = tf.shape(x)\n    for i in range(len(shape)):\n        if shape[i] is None:\n            try:\n                shape[i] = dynamic[i]\n            except:\n                # With RaggedTensors, accessing a ragged dimension will fail,\n                # we leave it as None.\n                pass\n    return tuple(shape)\n\n\ndef cast(x, dtype):\n    dtype = standardize_dtype(dtype)","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/tensorflow/core.py#L176-L212","documentation":"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.","triggerScenarios":"Thrown at keras/src/backend/tensorflow/core.py:194 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use tf.shape(x) (runtime shape tensor) when rank/shape are dynamic","Rebuild the graph so rank is static (e.g. avoid rank-polymorphic ops)","Call shape on a KerasTensor, whose symbolic rank is always known"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}