{"record":{"id":"397b511bb1ad7c0f","repo":"keras-team/keras","slug":"argument-n-should-be-a-positive-integer-receive","errorCode":null,"errorMessage":"Argument `n` should be a positive integer. Received: n={n}","messagePattern":"Argument `n` should be a positive integer\\. Received: n=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/reshaping/repeat_vector.py","lineNumber":35,"sourceCode":"\n    Args:\n        n: Integer, repetition factor.\n\n    Input shape:\n        2D tensor with shape `(batch_size, features)`.\n\n    Output shape:\n        3D tensor with shape `(batch_size, n, features)`.\n    \"\"\"\n\n    def __init__(self, n, **kwargs):\n        super().__init__(**kwargs)\n        if not isinstance(n, int) or isinstance(n, bool):\n            raise TypeError(\n                f\"Expected an integer value for `n`, got {type(n)}.\"\n            )\n        if n <= 0:\n            raise ValueError(\n                f\"Argument `n` should be a positive integer. Received: n={n}\"\n            )\n        self.n = n\n        self.input_spec = InputSpec(ndim=2)\n\n    def compute_output_shape(self, input_shape):\n        return (input_shape[0], self.n, input_shape[1])\n\n    def call(self, inputs):\n        input_shape = ops.shape(inputs)\n        reshaped = ops.reshape(inputs, (input_shape[0], 1, input_shape[1]))\n        return ops.repeat(reshaped, self.n, axis=1)\n\n    def get_config(self):\n        config = {\"n\": self.n}\n        base_config = super().get_config()\n        return {**base_config, **config}\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/reshaping/repeat_vector.py#L17-L53","documentation":"RepeatVector.__init__ rejects n <= 0 after the int check: repeating a vector zero or a negative number of times would produce an empty or invalid axis, so Keras fails fast at construction time.","triggerScenarios":"RepeatVector(n=0) or RepeatVector(n=-3). Typically the value comes from a computation or config that accidentally evaluates to zero or negative.","commonSituations":"Hyperparameter search proposing 0; deriving n from a length or batch-size expression that can be 0 for empty inputs; default/placeholder config values that were never replaced.","solutions":["Ensure the value feeding n is >= 1; add a max(1, ...) guard only if a 1-repeat is acceptable semantics","Trace where n comes from — usually an upstream calculation returning 0 (empty list length, division rounding down)","Validate configs at load time before layer construction"],"exampleFix":"# before\nn = len(seq) - 1  # 0 when len(seq)==1\nlayer = RepeatVector(n=n)\n\n# after\nn = max(len(seq), 1)\nlayer = RepeatVector(n=n)","handlingStrategy":"validation","validationCode":"def validated_n(n):\n    if not (isinstance(n, int) and not isinstance(n, bool)):\n        raise TypeError('n must be int')\n    if n <= 0:\n        raise ValueError(f'n must be >= 1, got {n}')\n    return n","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":null,"preventionTips":["Guard hyperparameter-search values with max(1, ...) when semantics allow","Check upstream length/ratio computations that can evaluate to 0","Fail fast on config values before model build"],"tags":["keras","repeat-vector","argument-validation","value-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}