{"record":{"id":"f0ade3a5b93e52f8","repo":"keras-team/keras","slug":"vocabulary-file-vocabulary-does-not-exist","errorCode":null,"errorMessage":"Vocabulary file {vocabulary} does not exist.","messagePattern":"Vocabulary file (.+?) does not exist\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":458,"sourceCode":"                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\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()` \"","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L440-L476","documentation":"set_vocabulary treats a string argument as a filesystem path to a vocabulary file. Here `tf.io.gfile.exists` reports that path as missing, so the layer cannot load it.","triggerScenarios":"`layer.set_vocabulary('/data/vocab.txt')` where the path is wrong, lives on another machine, or is not mounted in the container; also hit when a bare token string is passed by accident, since a str is always treated as a path, never as one token.","commonSituations":"Absolute paths baked into saved models that don't exist on the loading machine; vocabulary file not packaged with the deployment.","solutions":["Check the path before calling: `tf.io.gfile.exists(path)`.","Use paths relative to the project root, or ship the vocabulary file with the deployment artifact.","If you meant to pass tokens, pass a list of strings, not a single string."],"exampleFix":"# before\nlayer.set_vocabulary('vocab.txt')  # a str is treated as a file path\n\n# after\ntokens = [line.strip() for line in open('vocab.txt')]\nlayer.set_vocabulary(tokens)","handlingStrategy":"validation","validationCode":"import tensorflow as tf\nif isinstance(vocab, str):\n    if not tf.io.gfile.exists(vocab):\n        raise FileNotFoundError(vocab)\nlayer.set_vocabulary(vocab)","typeGuard":"def is_vocab_file(p) -> bool:\n    import tensorflow as tf\n    return isinstance(p, str) and tf.io.gfile.exists(p)","tryCatchPattern":"try:\n    layer.set_vocabulary(path)\nexcept ValueError as e:\n    raise FileNotFoundError('vocabulary file missing: %s' % path) from e","preventionTips":["Never pass a single string when you mean a single token — use a list.","Ship vocabulary files inside the deployment artifact and use relative paths."],"tags":["keras","preprocessing","index-lookup","file-path","vocabulary"],"backgroundTag":"file-not-found","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}