{"record":{"id":"280c3460cdb7056c","repo":"keras-team/keras","slug":"feature-name-has-output-mode-one-hot-howe","errorCode":null,"errorMessage":"Feature '{name}' has `output_mode='one_hot'`. However it isn't a standard feature and the dimensionality of its output space is not known, thus it cannot be one-hot encoded. Try using `output_mode='int'`.","messagePattern":"Feature '(.+?)' has `output_mode='one_hot'`\\. However it isn't a standard feature and the dimensionality of its output space is not known, thus it cannot be one-hot encoded\\. Try using `output_mode='int'`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/feature_space.py","lineNumber":704,"sourceCode":"                        f\"Feature '{name}' has `output_mode='one_hot'`. \"\n                        \"Thus its preprocessor should return an integer dtype. \"\n                        f\"Instead it returns a {dtype} dtype.\"\n                    )\n\n                if isinstance(\n                    preprocessor, (layers.IntegerLookup, layers.StringLookup)\n                ):\n                    cardinality = preprocessor.vocabulary_size()\n                elif isinstance(preprocessor, layers.CategoryEncoding):\n                    cardinality = preprocessor.num_tokens\n                elif isinstance(preprocessor, layers.Discretization):\n                    cardinality = preprocessor.num_bins\n                elif isinstance(\n                    preprocessor, (layers.HashedCrossing, layers.Hashing)\n                ):\n                    cardinality = preprocessor.num_bins\n                else:\n                    raise ValueError(\n                        f\"Feature '{name}' has `output_mode='one_hot'`. \"\n                        \"However it isn't a standard feature and the \"\n                        \"dimensionality of its output space is not known, \"\n                        \"thus it cannot be one-hot encoded. \"\n                        \"Try using `output_mode='int'`.\"\n                    )\n                if cardinality is not None:\n                    encoder = layers.CategoryEncoding(\n                        num_tokens=cardinality, output_mode=\"multi_hot\"\n                    )\n                    self.one_hot_encoders[name] = encoder\n                    feature = encoder(feature)\n\n            if self.output_mode == \"concat\":\n                dtype = feature.dtype\n                if dtype.startswith(\"int\") or dtype == \"string\":\n                    raise ValueError(\n                        f\"Cannot concatenate features because feature '{name}' \"","sourceCodeStart":686,"sourceCodeEnd":722,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/feature_space.py#L686-L722","documentation":"To one-hot encode a feature, FeatureSpace must know the output dimensionality (cardinality). It can infer num_bins only from standard preprocessors (IntegerHashed/StringHashed/CategoryCrossing/Hashing, etc.). A custom or non-standard preprocessor gives no cardinality, so one_hot is impossible.","triggerScenarios":"FeatureSpace(output_mode='one_hot') with a custom feature spec (e.g. FeatureSpace.feature(preprocessor=CustomLayer)) or a non-hashing crossing preprocessor, where the layer has no num_bins attribute.","commonSituations":"Custom crossing implementations, features built from Lambda or non-standard preprocessing layers, or crossing features without an underlying hashing/lookup layer.","solutions":["Switch the feature (or the whole FeatureSpace) to output_mode='int'","If you need one-hot, map the feature to a standard preprocessor that exposes num_bins (Hashing/HashedCrossing/CategoryEncoding)","Add a CategoryEncoding(num_tokens=known_cardinality) layer downstream to one-hot the int output yourself"],"exampleFix":"// before\nfs = FeatureSpace(features={\"cross\": FeatureSpace.cross(feature_names=(\"a\",\"b\"), output_mode=\"one_hot\")}, output_mode=\"concat\")\n// after\nfs = FeatureSpace(features={\"cross\": FeatureSpace.cross(feature_names=(\"a\",\"b\"), output_mode=\"int\")}, output_mode=\"int\")","handlingStrategy":"validation","validationCode":"p = fs.preprocessors.get(name) or fs.crossers.get(name)\nif not hasattr(p, \"num_bins\"):\n    # cannot infer cardinality; use output_mode='int'","typeGuard":"def one_hot_supported(p):\n    return hasattr(p, \"num_bins\")","tryCatchPattern":"catch ValueError from get_encoded_features/__call__, then switch the FeatureSpace (or crossing) output_mode to 'int'","preventionTips":["Use output_mode='int' for custom or crossing features whose output cardinality is unknown","Restrict one_hot to features backed by lookup/hashing layers that expose num_bins"],"tags":["keras","feature-space","one-hot","cardinality"],"backgroundTag":"unsupported-operation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}