{"record":{"id":"9eb2c6cd691a2354","repo":"keras-team/keras","slug":"sparse-true-can-only-be-used-with-the-tensorflow-9eb2c6","errorCode":null,"errorMessage":"`sparse=True` can only be used with the TensorFlow backend.","messagePattern":"`sparse=True` can only be used with the TensorFlow backend\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/integer_lookup.py","lineNumber":370,"sourceCode":"    ):\n        if not tf.available:\n            raise ImportError(\n                \"Layer IntegerLookup requires TensorFlow. \"\n                \"Install it via `pip install tensorflow`.\"\n            )\n        if max_tokens is not None and max_tokens <= 1:\n            raise ValueError(\n                \"If `max_tokens` is set for `IntegerLookup`, it must be \"\n                f\"greater than 1. Received: max_tokens={max_tokens}\"\n            )\n        if num_oov_indices < 0:\n            raise ValueError(\n                \"The value of `num_oov_indices` argument for `IntegerLookup` \"\n                \"must >= 0. Received: num_oov_indices=\"\n                f\"{num_oov_indices}\"\n            )\n        if sparse and backend.backend() != \"tensorflow\":\n            raise ValueError(\n                \"`sparse=True` can only be used with the TensorFlow backend.\"\n            )\n        if vocabulary_dtype != \"int64\":\n            raise ValueError(\n                \"Only `vocabulary_dtype='int64'` is supported \"\n                \"at this time. Received: \"\n                f\"vocabulary_dtype={vocabulary_dtype}\"\n            )\n        super().__init__(\n            max_tokens=max_tokens,\n            num_oov_indices=num_oov_indices,\n            mask_token=mask_token,\n            oov_token=oov_token,\n            vocabulary=vocabulary,\n            vocabulary_dtype=vocabulary_dtype,\n            idf_weights=idf_weights,\n            invert=invert,\n            output_mode=output_mode,","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/integer_lookup.py#L352-L388","documentation":"IntegerLookup can emit sparse tensors only through TensorFlow's SparseTensor machinery. __init__ rejects sparse=True when keras.backend is not tensorflow, regardless of whether TF is installed.","triggerScenarios":"keras.layers.IntegerLookup(sparse=True) while keras.backend is 'jax', 'torch' or 'numpy'; default environment KERAS_BACKEND=jax in a multi-backend install.","commonSituations":"Porting a TF2 preprocessing pipeline to JAX/PyTorch with Keras 3; setting the backend via env var but keeping sparse output for memory efficiency.","solutions":["Switch backend before building the model: keras.config.set_backend('tensorflow') (or KERAS_BACKEND=tensorflow)","Drop sparse=True and consume dense output","Move the lookup to a separate TF-only preprocessing stage"],"exampleFix":"// before (KERAS_BACKEND=jax)\nlayer = IntegerLookup(sparse=True)\n// after\nimport os; os.environ['KERAS_BACKEND']='tensorflow'\nimport keras\nlayer = keras.layers.IntegerLookup(sparse=True)","handlingStrategy":"validation","validationCode":"import keras\nif sparse and keras.backend.backend() != 'tensorflow':\n    sparse = False  # or fail loudly at config time","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set KERAS_BACKEND before importing keras","Gate sparse=True on backend in shared config code"],"tags":["keras","backend-mismatch","sparse","preprocessing"],"backgroundTag":"unsupported-backend-feature","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}