{"record":{"id":"442f358506cac277","repo":"keras-team/keras","slug":"num-oov-indices-must-be-greater-than-or-equal-to","errorCode":null,"errorMessage":"`num_oov_indices` must be greater than or equal to 0. Received: num_oov_indices={num_oov_indices}","messagePattern":"`num_oov_indices` must be greater than or equal to 0\\. Received: num_oov_indices=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":145,"sourceCode":"        salt=None,\n        **kwargs,\n    ):\n        # If max_tokens is set, the value must be greater than 1 - otherwise we\n        # are creating a 0-element vocab, which doesn't make sense.\n        if max_tokens is not None and max_tokens <= 1:\n            raise ValueError(\n                \"If set, `max_tokens` must be greater than 1. \"\n                f\"Received: max_tokens={max_tokens}\"\n            )\n\n        if pad_to_max_tokens and max_tokens is None:\n            raise ValueError(\n                \"If pad_to_max_tokens is True, must set `max_tokens`. \"\n                f\"Received: max_tokens={max_tokens}\"\n            )\n\n        if num_oov_indices < 0:\n            raise ValueError(\n                \"`num_oov_indices` must be greater than or equal to 0. \"\n                f\"Received: num_oov_indices={num_oov_indices}\"\n            )\n\n        argument_validation.validate_string_arg(\n            oov_method,\n            allowable_strings=(\"floormod\", \"farmhash\"),\n            caller_name=self.__class__.__name__,\n            arg_name=\"oov_method\",\n        )\n\n        if salt is not None:\n            if (\n                tf.as_dtype(vocabulary_dtype).is_integer\n                and oov_method != \"farmhash\"\n            ):\n                raise ValueError(\n                    \"`salt` can only be used when `oov_method='farmhash'`. \"","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L127-L163","documentation":"Keras IndexLookup reserves `num_oov_indices` buckets at the front of its index space for out-of-vocabulary tokens. The constructor validates this count is >= 0; a negative value cannot allocate buckets and fails fast at layer creation.","triggerScenarios":"Calling `IndexLookup(max_tokens=..., num_oov_indices=-1)`, or computing the value dynamically (e.g. `num_oov_indices=max_tokens - expected_vocab_size`) so it goes negative before the constructor validates it.","commonSituations":"Scripts that derive OOV bucket counts from dataset statistics, refactored StringLookup configs, or copy-pasted parameter sets where the variable holds a negative sentinel.","solutions":["Pass an explicit non-negative integer: 0 disables OOV buckets, 1 is the typical default.","If the value is computed, clamp or assert it before constructing the layer: `assert num_oov_indices >= 0`.","Remember `max_tokens` must cover OOV buckets plus mask token when `pad_to_max_tokens=True`."],"exampleFix":"# before\nlayer = IndexLookup(max_tokens=20000, num_oov_indices=n_buckets - extra)\n\n# after\nn_buckets = max(0, n_buckets - extra)\nlayer = IndexLookup(max_tokens=20000, num_oov_indices=n_buckets)","handlingStrategy":"validation","validationCode":"if not isinstance(num_oov_indices, int) or num_oov_indices < 0:\n    raise ValueError('num_oov_indices must be a non-negative int')\nlayer = IndexLookup(num_oov_indices=num_oov_indices)","typeGuard":"def valid_num_oov(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 0","tryCatchPattern":"try:\n    layer = IndexLookup(num_oov_indices=n)\nexcept ValueError:\n    n = max(0, n)\n    layer = IndexLookup(num_oov_indices=n)","preventionTips":["Validate derived configuration values before layer construction.","Use 0 or 1 for num_oov_indices unless multiple OOV buckets are required."],"tags":["keras","preprocessing","index-lookup","argument-validation"],"backgroundTag":"constructor-argument-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}