{"record":{"id":"139fe53fc3e4dd2b","repo":"keras-team/keras","slug":"idf-weights-must-be-set-if-output-mode-is-tf-id","errorCode":null,"errorMessage":"`idf_weights` must be set if output_mode is 'tf_idf'.","messagePattern":"`idf_weights` must be set if output_mode is 'tf_idf'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":434,"sourceCode":"        instead of analyzing a dataset through `adapt`. It should be used\n        whenever the vocab (and optionally document frequency) information is\n        already known.  If vocabulary data is already present in the layer, this\n        method will replace it.\n\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 \"","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L416-L452","documentation":"set_vocabulary() on a TF-IDF layer must receive idf_weights alongside the tokens, because the lookup table and the weight vector are installed together.","triggerScenarios":"`layer.set_vocabulary(vocab)` on a layer built with output_mode='tf_idf'. Also raised from load_assets when deserializing a model whose idf weights were not saved.","commonSituations":"Calling adapt()/set_vocabulary with a token-only dataset (adapt cannot compute idf); loading models saved by an older Keras that dropped idf weights.","solutions":["Compute idf weights (e.g. fit sklearn TfidfVectorizer on the same corpus) and pass them: `set_vocabulary(vocab, idf_weights=w)`.","If it happens on load, re-save the model with the current Keras so idf_weights serialize with the layer."],"exampleFix":"# before\ntfidf_layer.set_vocabulary(vocab)\n\n# after\ntfidf_layer.set_vocabulary(vocab, idf_weights=idf)","handlingStrategy":"validation","validationCode":"if layer.output_mode == 'tf_idf':\n    assert idf is not None, 'tf_idf layer needs idf_weights'\n    layer.set_vocabulary(vocab, idf_weights=idf)\nelse:\n    layer.set_vocabulary(vocab)","typeGuard":null,"tryCatchPattern":"try:\n    layer.set_vocabulary(vocab, idf_weights=idf)\nexcept ValueError as e:\n    raise RuntimeError('vocabulary setup failed: %s' % e) from e","preventionTips":["Never adapt a tf_idf layer on tokens alone — idf cannot be inferred.","Keep vocab and idf arrays together as one artifact (e.g. one npz) so they are never set apart."],"tags":["keras","preprocessing","index-lookup","tf-idf","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}