{"record":{"id":"277391e268479143","repo":"keras-team/keras","slug":"idf-weights-should-only-be-set-if-output-mode-is","errorCode":null,"errorMessage":"`idf_weights` should only be set if output_mode is `'tf_idf'`. Received: output_mode={self.output_mode} and idf_weights={idf_weights}","messagePattern":"`idf_weights` should only be set if output_mode is `'tf_idf'`\\. Received: output_mode=(.+?) and idf_weights=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":438,"sourceCode":"\n        Args:\n            vocabulary: Either an array or a string path to a text file.\n                If passing an array, can pass a tuple, list,\n                1D numpy array, or 1D tensor containing the vocbulary terms.\n                If passing a file path, the file should contain one line\n                per term in the vocabulary.\n            idf_weights: A tuple, list, 1D numpy array, or 1D tensor\n                of inverse document frequency weights with equal\n                length to vocabulary. Must be set if `output_mode`\n                is `\"tf_idf\"`. Should not be set otherwise.\n        \"\"\"\n        if self.output_mode == \"tf_idf\":\n            if idf_weights is None:\n                raise ValueError(\n                    \"`idf_weights` must be set if output_mode is 'tf_idf'.\"\n                )\n        elif idf_weights is not None:\n            raise ValueError(\n                \"`idf_weights` should only be set if output_mode is \"\n                f\"`'tf_idf'`. Received: output_mode={self.output_mode} \"\n                f\"and idf_weights={idf_weights}\"\n            )\n\n        if isinstance(vocabulary, str):\n            if serialization_lib.in_safe_mode():\n                raise ValueError(\n                    \"Requested the loading of a vocabulary file outside of the \"\n                    \"model archive. This carries a potential risk of loading \"\n                    \"arbitrary and sensitive files and thus it is disallowed \"\n                    \"by default. If you trust the source of the artifact, you \"\n                    \"can override this error by passing `safe_mode=False` to \"\n                    \"the loading function, or calling \"\n                    \"`keras.config.enable_unsafe_deserialization(). \"\n                    f\"Vocabulary file: '{vocabulary}'\"\n                )\n","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L420-L456","documentation":"The mirror of the missing-weights check: set_vocabulary() rejects `idf_weights` when the layer's output_mode is anything other than tf_idf, keeping the weight vector and the mode consistent.","triggerScenarios":"`layer.set_vocabulary(vocab, idf_weights=w)` on a layer constructed with output_mode 'int', 'multi_hot', 'count' or 'one_hot'.","commonSituations":"Shared vocabulary-setting helper code used for both a tf_idf layer and an int-index layer.","solutions":["Only pass idf_weights to the tf_idf layer; branch on `layer.output_mode`.","Or rebuild the layer with `output_mode='tf_idf'` if idf scaling is wanted."],"exampleFix":"# before\ndef set_vocab(layer, vocab, idf):\n    layer.set_vocabulary(vocab, idf_weights=idf)\n\n# after\ndef set_vocab(layer, vocab, idf):\n    if layer.output_mode == 'tf_idf':\n        layer.set_vocabulary(vocab, idf_weights=idf)\n    else:\n        layer.set_vocabulary(vocab)","handlingStrategy":"validation","validationCode":"if layer.output_mode == 'tf_idf':\n    layer.set_vocabulary(vocab, idf_weights=idf)\nelse:\n    layer.set_vocabulary(vocab)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Branch on layer.output_mode in shared vocabulary-setting helpers.","Store per-layer metadata (mode) next to vocabularies in pipelines."],"tags":["keras","preprocessing","index-lookup","tf-idf","argument-validation"],"backgroundTag":"invalid-argument-combination","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}