{"record":{"id":"78988efc851f8c42","repo":"keras-team/keras","slug":"data-and-targets-have-to-be-of-same-length-data-l","errorCode":null,"errorMessage":"Data and targets have to be of same length. Data length is {len(data)} while target length is {len(targets)}","messagePattern":"Data and targets have to be of same length\\. Data length is (.+?) while target length is (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/sequence.py","lineNumber":73,"sourceCode":"    \"\"\"\n\n    def __init__(\n        self,\n        data,\n        targets,\n        length,\n        sampling_rate=1,\n        stride=1,\n        start_index=0,\n        end_index=None,\n        shuffle=False,\n        reverse=False,\n        batch_size=128,\n        **kwargs,\n    ):\n        super().__init__(**kwargs)\n        if len(data) != len(targets):\n            raise ValueError(\n                \"Data and targets have to be \"\n                f\"of same length. Data length is {len(data)} \"\n                f\"while target length is {len(targets)}\"\n            )\n\n        self.data = data\n        self.targets = targets\n        self.length = length\n        self.sampling_rate = sampling_rate\n        self.stride = stride\n        self.start_index = start_index + length\n        if end_index is None:\n            end_index = len(data) - 1\n        self.end_index = end_index\n        self.shuffle = shuffle\n        self.reverse = reverse\n        self.batch_size = batch_size\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/sequence.py#L55-L91","documentation":"TimeseriesGenerator (legacy sequence preprocessing) pairs each input window with a target, so it requires len(data) == len(targets) exactly. Any mismatch raises this ValueError in __init__ before any windowing logic runs.","triggerScenarios":"Constructing TimeseriesGenerator(data, targets, length=...) where data and targets have different first dimensions, e.g. 1000 timesteps of data vs 999 targets from an off-by-one shift.","commonSituations":"Building targets by shifting a series (series[1:]) and forgetting the result is one shorter; slicing data and targets with different index ranges; mixing DataFrame columns of different lengths.","solutions":["Pass targets of the same length as data; with default settings the target for a window is the step right after it, so pass targets=data for next-step forecasting","Assert len(data) == len(targets) immediately before construction","Double-check any slicing used to create targets"],"exampleFix":"# before\nTimeseriesGenerator(series, series[1:], length=5)\n# after\nTimeseriesGenerator(series, series, length=5)  # next step is the target by default","handlingStrategy":"validation","validationCode":"assert len(data) == len(targets), (len(data), len(targets))","typeGuard":null,"tryCatchPattern":"try:\n    TimeseriesGenerator(data, targets, length=L)\nexcept ValueError as e:\n    if 'same length' not in str(e):\n        raise\n    targets = targets[:len(data)]\n    TimeseriesGenerator(data, targets, length=L)","preventionTips":["Build data and targets in one function so they cannot diverge in length"],"tags":["keras","timeseries","preprocessing","validation"],"backgroundTag":"length-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}