{"record":{"id":"2152a41fb04d179c","repo":"keras-team/keras","slug":"sparse-may-only-be-true-if-output-mode-is-on","errorCode":null,"errorMessage":"`sparse` may only be true if `output_mode` is `\"one_hot\"`, `\"multi_hot\"`, or `\"count\"`. Received: sparse={sparse} and output_mode={output_mode}","messagePattern":"`sparse` may only be true if `output_mode` is `\"one_hot\"`, `\"multi_hot\"`, or `\"count\"`\\. Received: sparse=(.+?) and output_mode=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/hashing.py","lineNumber":184,"sourceCode":"        if output_mode == \"int\" and (\n            self.dtype_policy.name not in (\"int32\", \"int64\")\n        ):\n            raise ValueError(\n                'When `output_mode=\"int\"`, `dtype` should be an integer '\n                f\"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}\"\n            )\n\n        # 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)\n        accepted_output_modes = (\"int\", \"one_hot\", \"multi_hot\", \"count\")\n        if output_mode not in accepted_output_modes:\n            raise ValueError(\n                \"Invalid value for argument `output_mode`. \"\n                f\"Expected one of {accepted_output_modes}. \"\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\"`, or `\"count\"`. '\n                f\"Received: sparse={sparse} and \"\n                f\"output_mode={output_mode}\"\n            )\n\n        self.num_bins = num_bins\n        self.mask_value = mask_value\n        self.strong_hash = True if salt is not None else False\n        self.output_mode = output_mode\n        self.sparse = sparse\n        self.salt = None\n        if salt is not None:\n            if isinstance(salt, (tuple, list)) and len(salt) == 2:\n                self.salt = list(salt)\n            elif isinstance(salt, int):\n                self.salt = [salt, salt]\n            else:","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/hashing.py#L166-L202","documentation":"Hashing can only return a sparse tensor for modes that produce vector outputs per sample ('one_hot', 'multi_hot', 'count'). With output_mode='int' each input maps to a scalar bucket index, so a sparse representation is meaningless and the layer rejects sparse=True in that combination.","triggerScenarios":"keras.layers.Hashing(num_bins=N, output_mode='int', sparse=True).","commonSituations":"Copy-pasting sparse=True from a one_hot/multi_hot pipeline into a Hashing layer configured for integer output; enabling sparse globally for memory savings without checking mode compatibility.","solutions":["Drop sparse=True (or set sparse=False) when output_mode='int'.","If you wanted sparse output, switch output_mode to 'one_hot', 'multi_hot', or 'count'."],"exampleFix":"# before\nlayer = keras.layers.Hashing(num_bins=64, output_mode=\"int\", sparse=True)\n# after\nlayer = keras.layers.Hashing(num_bins=64, output_mode=\"int\")","handlingStrategy":"validation","validationCode":"sparse_ok = sparse and output_mode != \"int\"\nlayer = Hashing(num_bins=n, output_mode=output_mode, sparse=sparse_ok)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralize sparse=True in one config flag and assert output_mode != 'int' when it is set."],"tags":["keras","preprocessing","hashing","sparse","invalid-argument"],"backgroundTag":"incompatible-argument-combination","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}