{"record":{"id":"6a3ce138b31aa1f7","repo":"keras-team/keras","slug":"when-using-output-mode-self-output-mode-and-p","errorCode":null,"errorMessage":"When using `output_mode={self.output_mode}` and `pad_to_max_tokens=False`, you must set the layer's vocabulary before calling it. Either pass a `vocabulary` argument to the layer, or call `adapt` with some sample data.","messagePattern":"When using `output_mode=(.+?)` and `pad_to_max_tokens=False`, you must set the layer's vocabulary before calling it\\. Either pass a `vocabulary` argument to the layer, or call `adapt` with some sample data\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":1033,"sourceCode":"            return tf.sparse.expand_dims(inputs, axis)\n        else:\n            return tf.expand_dims(inputs, axis)\n\n    def _oov_start_index(self):\n        return (\n            1\n            if self.mask_token is not None and self.output_mode == \"int\"\n            else 0\n        )\n\n    def _token_start_index(self):\n        return self._oov_start_index() + self.num_oov_indices\n\n    def _ensure_known_vocab_size(self):\n        if self.output_mode == \"int\" or self.pad_to_max_tokens:\n            return\n        if self._frozen_vocab_size is None:\n            raise RuntimeError(\n                f\"When using `output_mode={self.output_mode}` \"\n                \"and `pad_to_max_tokens=False`, \"\n                \"you must set the layer's vocabulary before calling it. Either \"\n                \"pass a `vocabulary` argument to the layer, or call `adapt` \"\n                \"with some sample data.\"\n            )\n\n    def _ensure_vocab_size_unchanged(self):\n        if self.output_mode == \"int\" or self.pad_to_max_tokens:\n            return\n\n        with tf.init_scope():\n            new_vocab_size = self.vocabulary_size()\n\n        if (\n            self._frozen_vocab_size is not None\n            and new_vocab_size != self._frozen_vocab_size\n        ):","sourceCodeStart":1015,"sourceCodeEnd":1051,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L1015-L1051","documentation":"For output modes other than 'int' (e.g. 'count', 'tf_idf', multi-hot) with pad_to_max_tokens=False, the layer must know the vocabulary size at call time to size the output vector. _ensure_known_vocab_size() raises when _frozen_vocab_size is None, i.e. the layer was never given a vocabulary and never adapted.","triggerScenarios":"Creating IndexLookup/TextVectorization(output_mode='multi_hot'|'count'|'tf_idf', pad_to_max_tokens=False) with no vocabulary argument and calling it before adapt(); building a model with such a layer and calling model.predict without prior vocabulary setup.","commonSituations":"Prototyping multi-hot encoders in a functional model without adapt; assuming the layer infers vocab size from the first batch like 'int' mode does.","solutions":["Call layer.adapt(sample_data) before using the layer","Pass a vocabulary at construction: TextVectorization(vocabulary=...)","Set pad_to_max_tokens=True with max_tokens so the output width is fixed without a vocabulary"],"exampleFix":"// before\nlayer = TextVectorization(output_mode='multi_hot')\nmodel(layer(inputs))\n// after\nlayer = TextVectorization(output_mode='multi_hot')\nlayer.adapt(train_texts)\nmodel(layer(inputs))","handlingStrategy":"validation","validationCode":"if layer.output_mode != 'int' and not layer.pad_to_max_tokens:\n    assert layer.vocabulary_size() > 0, 'adapt or set vocabulary first'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["adapt() on sample data as a standard construction step","Use max_tokens + pad_to_max_tokens=True for fixed-width outputs"],"tags":["keras","preprocessing","vocabulary","runtime-error"],"backgroundTag":"uninitialized-component-usage","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}