keras-team/keras · error · TypeError
Expected an integer value for `n`, got {type(n)}.
Error message
Expected an integer value for `n`, got {type(n)}. What it means
RepeatVector.__init__ requires n to be a genuine Python int. The explicit bool exclusion exists because isinstance(True, int) is True in Python, so True/False would otherwise silently become n=1/n=0. Floats, strings and None all fail this TypeError at construction.
Source
Thrown at keras/src/layers/reshaping/repeat_vector.py:31
>>> x = keras.Input(shape=(32,))
>>> y = keras.layers.RepeatVector(3)(x)
>>> y.shape
(None, 3, 32)
Args:
n: Integer, repetition factor.
Input shape:
2D tensor with shape `(batch_size, features)`.
Output shape:
3D tensor with shape `(batch_size, n, features)`.
"""
def __init__(self, n, **kwargs):
super().__init__(**kwargs)
if not isinstance(n, int) or isinstance(n, bool):
raise TypeError(
f"Expected an integer value for `n`, got {type(n)}."
)
if n <= 0:
raise ValueError(
f"Argument `n` should be a positive integer. Received: n={n}"
)
self.n = n
self.input_spec = InputSpec(ndim=2)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.n, input_shape[1])
def call(self, inputs):
input_shape = ops.shape(inputs)
reshaped = ops.reshape(inputs, (input_shape[0], 1, input_shape[1]))
return ops.repeat(reshaped, self.n, axis=1)
def get_config(self):View on GitHub (pinned to 7a34a03db6)
Solutions
- Cast explicitly: RepeatVector(n=int(n)) after confirming the value is integral
- Fix the config source so n is stored and loaded as an integer (int() after JSON/YAML load, or a typed CLI flag)
- If the value arrives as a numpy integer, wrap with int()
Example fix
# before n = float(cfg['repeat']) # e.g. 4.0 layer = RepeatVector(n=n) # TypeError # after n = int(cfg['repeat']) layer = RepeatVector(n=n)
Defensive patterns
Strategy: type-guard
Validate before calling
def coerce_n(n):
if isinstance(n, bool) or not isinstance(n, int):
raise TypeError(f'n must be int, got {type(n)}')
return n Type guard
def is_strict_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) Prevention
- Cast config values with int() at load time
- Treat bool as invalid for count-like parameters
- Schema-validate config files so n is typed int
When it happens
Trigger: RepeatVector(n=3.0), RepeatVector(n='4'), RepeatVector(n=None), or RepeatVector(n=True). Any of these raises before the layer is usable.
Common situations: Reading n from JSON/YAML config where it deserializes as float or string; hyperparameter sweeps (optuna, ray) returning floats; forgetting to cast a CLI arg (always a string) to int.
Related errors
- Argument `n` should be a positive integer. Received: n={n}
- Expected an integer value for `size`, got {type(size)}.
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/0c2f196daef9658a.
Report an issue: GitHub.