{"record":{"id":"e93143f5b06b8055","repo":"keras-team/keras","slug":"cannot-set-a-tensor-vocabulary-on-layer-self-name","errorCode":null,"errorMessage":"Cannot set a tensor vocabulary on layer {self.name} when not executing eagerly. Create this layer or call `set_vocabulary()` outside of any traced function.","messagePattern":"Cannot set a tensor vocabulary on layer (.+?) when not executing eagerly\\. Create this layer or call `set_vocabulary\\(\\)` outside of any traced function\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":473,"sourceCode":"                )\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\n        # entire function to operate on tensors and convert vocabulary to a\n        # tensor here.\n        if tf.is_tensor(vocabulary):\n            vocabulary = self._tensor_vocab_to_numpy(vocabulary)\n        elif isinstance(vocabulary, (list, tuple)):\n            vocabulary = np.array(vocabulary)\n        if tf.is_tensor(idf_weights):\n            idf_weights = idf_weights.numpy()\n        elif isinstance(idf_weights, (list, tuple)):\n            idf_weights = np.array(idf_weights)\n","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L455-L491","documentation":"Inside a traced function (tf.function graph, tf.data map, or Keras 3 multi-backend trace) there is no eager execution context, so set_vocabulary cannot convert tensor inputs — lookup table creation requires eager side effects. The layer raises RuntimeError to fail fast rather than bake a stale table into the graph.","triggerScenarios":"Constructing `IndexLookup(vocabulary=tensor)` or calling `set_vocabulary(vocab_tensor)` inside a `@tf.function`, a tf.data `.map()` callback, or another traced function.","commonSituations":"Moving layer creation or adaptation into a compiled train step or an input pipeline; JAX and torch tracing paths in Keras 3.","solutions":["Move set_vocabulary or layer construction outside the traced function, calling it eagerly at setup time.","Convert tensors to numpy first and pass the array.","In tf.data pipelines, finish vocabulary setup before building the dataset, not inside a map function."],"exampleFix":"# before\n@tf.function\ndef setup(layer, vocab):\n    layer.set_vocabulary(vocab)\n\n# after\nlayer.set_vocabulary(vocab.numpy())  # eagerly, outside any trace","handlingStrategy":"validation","validationCode":"import tensorflow as tf\nif tf.is_tensor(vocab):\n    assert tf.executing_eagerly(), 'set_vocabulary needs eager mode for tensors'\n    vocab = vocab.numpy()\nlayer.set_vocabulary(vocab)","typeGuard":"def can_set_tensor_vocab() -> bool:\n    import tensorflow as tf\n    return tf.executing_eagerly()","tryCatchPattern":"try:\n    layer.set_vocabulary(vocab)\nexcept RuntimeError:\n    layer.set_vocabulary(vocab.numpy())  # retry eagerly outside the trace","preventionTips":["Do all vocabulary setup eagerly at pipeline-build time, never inside tf.function or tf.data map.","Convert tensors to numpy before handing them to preprocessing layers."],"tags":["keras","preprocessing","index-lookup","eager-execution","tf-function"],"backgroundTag":"eager-execution-required","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}