keras-team/keras · error · ValueError
`start_index+length={self.start_index} > end_index={self.end
Error message
`start_index+length={self.start_index} > end_index={self.end_index}` is disallowed, as no part of the sequence would be left to be used as current step. What it means
TimeseriesGenerator needs at least one usable timestep at or after start_index to serve as the 'current step' target, so start_index must not exceed end_index (normally end_index = len(data) - 1). The f-string even prints 'start_index+length' because that is the typical intended meaning. Violating it leaves no valid windows.
Source
Thrown at keras/src/legacy/preprocessing/sequence.py:93
f"of same length. Data length is {len(data)} "
f"while target length is {len(targets)}"
)
self.data = data
self.targets = targets
self.length = length
self.sampling_rate = sampling_rate
self.stride = stride
self.start_index = start_index + length
if end_index is None:
end_index = len(data) - 1
self.end_index = end_index
self.shuffle = shuffle
self.reverse = reverse
self.batch_size = batch_size
if self.start_index > self.end_index:
raise ValueError(
f"`start_index+length={self.start_index} "
f"> end_index={self.end_index}` "
"is disallowed, as no part of the sequence "
"would be left to be used as current step."
)
def __len__(self):
return (
self.end_index - self.start_index + self.batch_size * self.stride
) // (self.batch_size * self.stride)
def __getitem__(self, index):
if self.shuffle:
rows = np.random.randint(
self.start_index, self.end_index + 1, size=self.batch_size
)
else:
i = self.start_index + self.batch_size * self.stride * indexView on GitHub (pinned to 7a34a03db6)
Solutions
- Fix the split so start_index <= end_index, typically end_index = len(data) - 1
- Ensure len(data) > length so at least one window fits
- Compute indices programmatically instead of hardcoding them
Example fix
# before TimeseriesGenerator(data, targets, length=10, start_index=800, end_index=750) # after TimeseriesGenerator(data, targets, length=10, start_index=0, end_index=len(data) - 1)
Defensive patterns
Strategy: validation
Validate before calling
end_index = min(end_index, len(data) - 1)
if start_index > end_index:
raise ValueError('empty window range') Prevention
- Compute end_index from len(data) instead of hardcoding
- Check len(data) > length before constructing the generator
When it happens
Trigger: Passing start_index greater than end_index, e.g. TimeseriesGenerator(data, targets, length=50, start_index=900, end_index=800), or a series so short that end_index collapses below start_index.
Common situations: Carving train/validation splits with explicit start_index/end_index and swapping the two; window length >= series length.
Related errors
- Data and targets have to be of same length. Data length is {
- `adapt()` can only be called on a tf.data.Dataset or a dict
- Invalid value for argument `output_mode`. Expected one of {a
- `sparse` may only be true if `output_mode` is `"one_hot"`, `
- The `salt` argument for `Hashing` can only be a tuple of siz
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2e7d56a02591ae8a.
Report an issue: GitHub.