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 * index

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Fix the split so start_index <= end_index, typically end_index = len(data) - 1
  2. Ensure len(data) > length so at least one window fits
  3. 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

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


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/2e7d56a02591ae8a. Report an issue: GitHub.