{"record":{"id":"ede6e7935b800650","repo":"keras-team/keras","slug":"if-set-max-tokens-must-be-greater-than-1-recei","errorCode":null,"errorMessage":"If set, `max_tokens` must be greater than 1. Received: max_tokens={max_tokens}","messagePattern":"If set, `max_tokens` must be greater than 1\\. Received: max_tokens=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/index_lookup.py","lineNumber":133,"sourceCode":"        num_oov_indices,\n        mask_token,\n        oov_token,\n        vocabulary_dtype,\n        vocabulary=None,\n        idf_weights=None,\n        invert=False,\n        output_mode=\"int\",\n        sparse=False,\n        pad_to_max_tokens=False,\n        oov_method=\"floormod\",\n        name=None,\n        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,","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/index_lookup.py#L115-L151","documentation":"Raised by IndexLookup's constructor when `max_tokens` is set to a value <= 1. The vocabulary table reserves slots for padding/OOV tokens, so a cap of 1 or 0 would leave a zero-element vocabulary, which is meaningless.","triggerScenarios":"Calling StringLookup(vocabulary=..., max_tokens=1) or IntegerLookup(max_tokens=0); any IndexLookup subclass (StringLookup, IntegerLookup, CategoryEncoding) with max_tokens <= 1.","commonSituations":"Computing max_tokens programmatically (e.g. len(set(labels)) minus reserved tokens) and getting 0 or 1; copying a config where max_tokens was meant to be unset (None).","solutions":["Set max_tokens=None if you don't need a cap","Use a value > 1 that accounts for OOV and padding indices, e.g. vocab_size + num_oov_indices + 1"],"exampleFix":"// before\nlayer = StringLookup(vocabulary=vocab, max_tokens=1)\n// after\nlayer = StringLookup(vocabulary=vocab, max_tokens=None)","handlingStrategy":"validation","validationCode":"assert max_tokens is None or max_tokens > 1, 'max_tokens must be greater than 1 when set'","typeGuard":"def valid_max_tokens(v):\n    return v is None or (isinstance(v, int) and v > 1)","tryCatchPattern":"try:\n    layer = StringLookup(vocabulary=vocab, max_tokens=mt)\nexcept ValueError:\n    mt = None\n    layer = StringLookup(vocabulary=vocab, max_tokens=mt)","preventionTips":["When computing max_tokens programmatically, assert it exceeds vocab + OOV + 1","Leave max_tokens unset unless you truly need a cap"],"tags":["keras","preprocessing","index-lookup","validation"],"backgroundTag":"constructor-argument-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}