{"record":{"id":"1e892db3803582e8","repo":"keras-team/keras","slug":"sparse-may-only-be-true-if-output-mode-is-on-1e892d","errorCode":null,"errorMessage":"`sparse` may only be true if `output_mode` is `'one_hot'`, `'multi_hot'`, `'count'` or `'tf_idf'`. Received: sparse={sparse} and output_mode={output_mode}","messagePattern":"`sparse` may only be true if `output_mode` is `'one_hot'`, `'multi_hot'`, `'count'` or `'tf_idf'`\\. Received: sparse=(.+?) and output_mode=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":202,"sourceCode":"            allowable_strings=(\n                \"int\",\n                \"one_hot\",\n                \"multi_hot\",\n                \"count\",\n                \"tf_idf\",\n            ),\n            caller_name=self.__class__.__name__,\n            arg_name=\"output_mode\",\n        )\n\n        if invert and output_mode != \"int\":\n            raise ValueError(\n                \"`output_mode` must be `'int'` when `invert` is true. \"\n                f\"Received: output_mode={output_mode}\"\n            )\n\n        if sparse and output_mode == \"int\":\n            raise ValueError(\n                \"`sparse` may only be true if `output_mode` is \"\n                \"`'one_hot'`, `'multi_hot'`, `'count'` or `'tf_idf'`. \"\n                f\"Received: sparse={sparse} and \"\n                f\"output_mode={output_mode}\"\n            )\n\n        if idf_weights is not None and output_mode != \"tf_idf\":\n            raise ValueError(\n                \"`idf_weights` should only be set if `output_mode` is \"\n                f\"`'tf_idf'`. Received: idf_weights={idf_weights} and \"\n                f\"output_mode={output_mode}\"\n            )\n\n        super().__init__(name=name)\n        self._convert_input_args = False\n        self._allow_non_tensor_positional_args = True\n        self.supports_jit = False\n","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L184-L220","documentation":"Integer output mode produces a dense tensor of indices, so `sparse=True` is meaningless there. Sparse output is only supported for the wide modes: one_hot, multi_hot, count and tf_idf.","triggerScenarios":"`IndexLookup(sparse=True, output_mode='int')` — including the default output_mode='int' when only `sparse` is set.","commonSituations":"Enabling sparse output to save memory on large vocabularies while leaving output_mode at its default.","solutions":["Drop `sparse=True` when you need integer indices.","Or switch output_mode to 'multi_hot', 'count' or 'tf_idf' to get sparse output.","For memory-efficient pipelines, keep int output and let the Embedding layer handle large vocabs."],"exampleFix":"# before\nlayer = IndexLookup(sparse=True)  # output_mode defaults to 'int'\n\n# after\nlayer = IndexLookup(output_mode='multi_hot', sparse=True)","handlingStrategy":"validation","validationCode":"if sparse and output_mode == 'int':\n    raise ValueError('sparse requires one_hot/multi_hot/count/tf_idf output')\nlayer = IndexLookup(output_mode=output_mode, sparse=sparse)","typeGuard":"def sparse_mode_ok(mode) -> bool:\n    return mode in ('one_hot', 'multi_hot', 'count', 'tf_idf')","tryCatchPattern":null,"preventionTips":["Remember output_mode defaults to 'int'; setting sparse alone always conflicts.","Encode the sparse/mode compatibility rule in shared config validators."],"tags":["keras","preprocessing","index-lookup","argument-validation","sparse"],"backgroundTag":"invalid-argument-combination","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}