{"record":{"id":"fadbd28f239fc6dc","repo":"keras-team/keras","slug":"when-output-mode-int-dtype-should-be-an-int","errorCode":null,"errorMessage":"When `output_mode=\"int\"`, `dtype` should be an integer type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}","messagePattern":"When `output_mode=\"int\"`, `dtype` should be an integer type, 'int32' or 'in64'\\. Received: dtype=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/hashing.py","lineNumber":169,"sourceCode":"\n        # By default, output int32 when output_mode='int' and floats otherwise.\n        if \"dtype\" not in kwargs or kwargs[\"dtype\"] is None:\n            kwargs[\"dtype\"] = (\n                \"int64\" if output_mode == \"int\" else backend.floatx()\n            )\n\n        super().__init__(**kwargs)\n\n        if num_bins is None or num_bins <= 0:\n            raise ValueError(\n                \"The `num_bins` for `Hashing` cannot be `None` or \"\n                f\"non-positive values. Received: num_bins={num_bins}.\"\n            )\n\n        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 \"","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/hashing.py#L151-L187","documentation":"When output_mode='int', Hashing emits bucket indices, so its dtype policy must be int32 or int64. A float dtype (e.g. the backend default floatx) makes index output meaningless and is rejected.","triggerScenarios":"Hashing(..., output_mode='int', dtype='float32') (or any dtype whose policy name is not int32/int64) at construction.","commonSituations":"Explicitly passing dtype='float32' (or inheriting a global float dtype policy) while leaving output_mode='int'; refactoring code from one_hot/count back to int without updating dtype.","solutions":["Pass dtype='int64' (or 'int32') when output_mode='int'","Simplest: omit dtype — the layer defaults to int64 for output_mode='int'","If you wanted float output, switch output_mode to 'one_hot'/'multi_hot'/'count' instead"],"exampleFix":"// before\nlayer = Hashing(num_bins=1000, output_mode=\"int\", dtype=\"float32\")\n// after\nlayer = Hashing(num_bins=1000, output_mode=\"int\", dtype=\"int64\")\n# or simply omit dtype","handlingStrategy":"validation","validationCode":"assert kwargs.get(\"dtype\", None) in (None, \"int32\", \"int64\") or output_mode != \"int\"","typeGuard":"def valid_int_dtype(d):\n    return d in (None, \"int32\", \"int64\")","tryCatchPattern":"catch ValueError from Hashing.__init__ and pass dtype='int64' (or drop dtype) when constructing again","preventionTips":["Omit the dtype argument for Hashing; the layer picks int32/int64 automatically for output_mode='int'","Pass dtype='int32' or 'int64' explicitly only when overriding"],"tags":["keras","hashing","dtype","output-mode"],"backgroundTag":"dtype-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}