{"record":{"id":"0ad0bb4376bc087e","repo":"keras-team/keras","slug":"a-kerastensor-cannot-be-used-as-input-to-a-jax-fun","errorCode":null,"errorMessage":"A KerasTensor cannot be used as input to a JAX 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...\njax_fn(x)  # Invalid.\n```\n\nWhat you should do instead is wrap `jax_fn` in a layer:\n\n```\nclass MyLayer(Layer):\n    def call(self, x):\n        return jax_fn(x)\n\nx = MyLayer()(x)\n```\n","messagePattern":"A KerasTensor cannot be used as input to a JAX 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\\.\\.\\.\njax_fn\\(x\\)  # Invalid\\.\n```\n\nWhat you should do instead is wrap `jax_fn` in a layer:\n\n```\nclass MyLayer\\(Layer\\):\n    def call\\(self, x\\):\n        return jax_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":171,"sourceCode":"            \"You cannot convert it to an int.\"\n        )\n\n    def __float__(self):\n        raise ValueError(\n            \"A KerasTensor is symbolic: it's a placeholder for a shape \"\n            \"an a dtype. It doesn't have any actual numerical value. \"\n            \"You cannot convert it to a float.\"\n        )\n\n    def __array__(self):\n        raise ValueError(\n            \"A KerasTensor is symbolic: it's a placeholder for a shape \"\n            \"an a dtype. It doesn't have any actual numerical value. \"\n            \"You cannot convert it to a NumPy array.\"\n        )\n\n    def __jax_array__(self):\n        raise ValueError(\n            \"A KerasTensor cannot be used as input to a JAX 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            \"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\"","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/keras_tensor.py#L153-L189","documentation":"Error \"A KerasTensor cannot be used as input to a JAX 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...\njax_fn(x)  # Invalid.\n```\n\nWhat you should do instead is wrap `jax_fn` in a layer:\n\n```\nclass MyLayer(Layer):\n    def call(self, x):\n        return jax_fn(x)\n\nx = MyLayer()(x)\n```\n\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/backend/common/keras_tensor.py:171 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Wrap the jax_fn call inside a custom keras.Layer whose call() applies jax_fn to its input, and use that layer in the Functional graph","Use ops from keras.ops, which handle symbolic KerasTensors, instead of raw jax functions","For pure functions on concrete data, run them outside the model on real tensors, not on symbolic inputs"],"exampleFix":null,"handlingStrategy":"fallback","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"}