{"record":{"id":"0cbe2f8d201b16e6","repo":"keras-team/keras","slug":"a-kerastensor-cannot-be-used-as-input-to-a-tensorf","errorCode":null,"errorMessage":"A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.ops`). You are likely doing something like:\n\n```\nx = Input(...)\n...\ntf_fn(x)  # Invalid.\n```\n\nWhat you should do instead is wrap `tf_fn` in a layer:\n\n```\nclass MyLayer(Layer):\n    def call(self, x):\n        return tf_fn(x)\n\nx = MyLayer()(x)\n```\n","messagePattern":"A KerasTensor cannot be used as input to a TensorFlow function\\. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions\\. You can only use it as input to a Keras layer or a Keras operation \\(from the namespaces `keras\\.layers` and `keras\\.ops`\\)\\. You are likely doing something like:\n\n```\nx = Input\\(\\.\\.\\.\\)\n\\.\\.\\.\ntf_fn\\(x\\)  # Invalid\\.\n```\n\nWhat you should do instead is wrap `tf_fn` in a layer:\n\n```\nclass MyLayer\\(Layer\\):\n    def call\\(self, x\\):\n        return tf_fn\\(x\\)\n\nx = MyLayer\\(\\)\\(x\\)\n```\n","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/keras_tensor.py","lineNumber":194,"sourceCode":"            \"or a Keras operation (from the namespaces `keras.layers` \"\n            \"and `keras.ops`). \"\n            \"You are likely doing something like:\\n\\n\"\n            \"```\\n\"\n            \"x = Input(...)\\n\"\n            \"...\\n\"\n            \"jax_fn(x)  # Invalid.\\n\"\n            \"```\\n\\n\"\n            \"What you should do instead is wrap `jax_fn` in a layer:\\n\\n\"\n            \"```\\n\"\n            \"class MyLayer(Layer):\\n\"\n            \"    def call(self, x):\\n\"\n            \"        return jax_fn(x)\\n\\n\"\n            \"x = MyLayer()(x)\\n\"\n            \"```\\n\"\n        )\n\n    def __tf_tensor__(self, dtype=None, name=None):\n        raise ValueError(\n            \"A KerasTensor cannot be used as input to a TensorFlow function. \"\n            \"A KerasTensor is a symbolic placeholder for a shape and dtype, \"\n            \"used when constructing Keras Functional models \"\n            \"or Keras Functions. You can only use it as input to a Keras layer \"\n            \"or a Keras operation (from the namespaces `keras.layers` \"\n            \"and `keras.ops`). \"\n            \"You are likely doing something like:\\n\\n\"\n            \"```\\n\"\n            \"x = Input(...)\\n\"\n            \"...\\n\"\n            \"tf_fn(x)  # Invalid.\\n\"\n            \"```\\n\\n\"\n            \"What you should do instead is wrap `tf_fn` in a layer:\\n\\n\"\n            \"```\\n\"\n            \"class MyLayer(Layer):\\n\"\n            \"    def call(self, x):\\n\"\n            \"        return tf_fn(x)\\n\\n\"\n            \"x = MyLayer()(x)\\n\"","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/keras_tensor.py#L176-L212","documentation":"Error \"A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.ops`). You are likely doing something like:\n\n```\nx = Input(...)\n...\ntf_fn(x)  # Invalid.\n```\n\nWhat you should do instead is wrap `tf_fn` in a layer:\n\n```\nclass MyLayer(Layer):\n    def call(self, x):\n        return tf_fn(x)\n\nx = MyLayer()(x)\n```\n\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/backend/common/keras_tensor.py:194 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}