{"record":{"id":"8e9093761b55679b","repo":"keras-team/keras","slug":"input-dim-must-be-a-positive-integer-received","errorCode":null,"errorMessage":"`input_dim` must be a positive integer. Received: input_dim={input_dim} (of type {type(input_dim).__name__}).","messagePattern":"`input_dim` must be a positive integer\\. Received: input_dim=(.+?) \\(of type (.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/embedding.py","lineNumber":106,"sourceCode":"        self,\n        input_dim,\n        output_dim,\n        embeddings_initializer=\"uniform\",\n        embeddings_regularizer=None,\n        embeddings_constraint=None,\n        mask_zero=False,\n        weights=None,\n        lora_rank=None,\n        lora_alpha=None,\n        quantization_config=None,\n        **kwargs,\n    ):\n        if (\n            not isinstance(input_dim, int)\n            or isinstance(input_dim, bool)\n            or input_dim <= 0\n        ):\n            raise ValueError(\n                \"`input_dim` must be a positive integer. \"\n                f\"Received: input_dim={input_dim} \"\n                f\"(of type {type(input_dim).__name__}).\"\n            )\n        if (\n            not isinstance(output_dim, int)\n            or isinstance(output_dim, bool)\n            or output_dim <= 0\n        ):\n            raise ValueError(\n                \"`output_dim` must be a positive integer. \"\n                f\"Received: output_dim={output_dim} \"\n                f\"(of type {type(output_dim).__name__}).\"\n            )\n        input_length = kwargs.pop(\"input_length\", None)\n        if input_length is not None:\n            warnings.warn(\n                \"Argument `input_length` is deprecated. Just remove it.\"","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/embedding.py#L88-L124","documentation":"keras.layers.Embedding requires input_dim (vocabulary size) to be a Python int strictly greater than 0; bools are explicitly rejected even though bool subclasses int. Passing a float, numpy integer, 0, a negative number, or True raises this ValueError in __init__.","triggerScenarios":"Constructing Embedding with input_dim=0, input_dim=-1, input_dim=1000.0, input_dim=np.int64(5000), input_dim=True, or a value taken unconverted from a config/serialization dict.","commonSituations":"Loading configs from JSON/YAML where numbers deserialize as floats; using numpy scalars from tokenizers; off-by-one vocabulary counts yielding 0; passing True from a flag variable by mistake.","solutions":["Pass a plain Python int greater than 0, e.g. Embedding(input_dim=10000, output_dim=128)","Convert numpy scalars explicitly with int(...)","Check for an empty vocabulary / off-by-one before constructing (len(vocab), not len(vocab)-1)"],"exampleFix":"# before\nlayer = keras.layers.Embedding(input_dim=np.int64(vocab_size), output_dim=128)\n# after\nlayer = keras.layers.Embedding(input_dim=int(vocab_size), output_dim=128)","handlingStrategy":"type-guard","validationCode":"assert isinstance(input_dim, int) and not isinstance(input_dim, bool) and input_dim > 0","typeGuard":"def is_positive_int(v):\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":null,"preventionTips":["Cast tokenizer vocabulary sizes with int() at the call site","Never source constructor args straight from JSON/YAML configs without casting"],"tags":["keras","embedding","constructor-validation","input-dim"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}