{"record":{"id":"0586f959fdd6ba0f","repo":"keras-team/keras","slug":"output-mode-tf-idf-does-not-support-loading-a","errorCode":null,"errorMessage":"output_mode `'tf_idf'` does not support loading a vocabulary from file.","messagePattern":"output_mode `'tf_idf'` does not support loading a vocabulary from file\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":462,"sourceCode":"        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\n            if not tf.io.gfile.exists(vocabulary):\n                raise ValueError(\n                    f\"Vocabulary file {vocabulary} does not exist.\"\n                )\n            if self.output_mode == \"tf_idf\":\n                raise ValueError(\n                    \"output_mode `'tf_idf'` does not support loading a \"\n                    \"vocabulary from file.\"\n                )\n            self.lookup_table = self._lookup_table_from_file(vocabulary)\n            self._record_vocabulary_size()\n            return\n\n        if not tf.executing_eagerly() and (\n            tf.is_tensor(vocabulary) or tf.is_tensor(idf_weights)\n        ):\n            raise RuntimeError(\n                f\"Cannot set a tensor vocabulary on layer {self.name} \"\n                \"when not executing eagerly. \"\n                \"Create this layer or call `set_vocabulary()` \"\n                \"outside of any traced function.\"\n            )\n\n        # TODO(mattdangerw): for better performance we should rewrite this","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L444-L480","documentation":"Loading a vocabulary from a text file yields only the term list, but output_mode='tf_idf' additionally requires per-term idf weights that a file cannot supply. This ValueError fires when set_vocabulary()/load_vocabulary is called with a file path while the layer's output_mode is 'tf_idf'.","triggerScenarios":"`layer.set_vocabulary('vocab.txt')` on a layer built with output_mode='tf_idf'.","commonSituations":"Reusing a text vocabulary file from a StringLookup or int-mode pipeline to build a TF-IDF layer.","solutions":["Load the file yourself and pass tokens plus weights, then `set_vocabulary(tokens, idf_weights=w)`.","Or switch output_mode to 'int' or 'multi_hot' if idf weighting is not needed."],"exampleFix":"# before\ntfidf_layer.set_vocabulary('vocab.txt')\n\n# after\ntokens = [l.strip() for l in open('vocab.txt')]\ntfidf_layer.set_vocabulary(tokens, idf_weights=idf)","handlingStrategy":"validation","validationCode":"if layer.output_mode == 'tf_idf':\n    tokens = [l.strip() for l in open(vocab_file)]\n    layer.set_vocabulary(tokens, idf_weights=idf)\nelse:\n    layer.set_vocabulary(vocab_file)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember file-path vocabularies carry no idf data by construction.","Keep a (tokens, idf) pair artifact for every tf_idf layer."],"tags":["keras","preprocessing","index-lookup","tf-idf","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}