{"record":{"id":"9ad4e44e61810df3","repo":"google-research/timesfm","slug":"targets-and-train-lens-must-have-the-same-number-o","errorCode":null,"errorMessage":"targets and train_lens must have the same number of elements.","messagePattern":"targets and train_lens must have the same number of elements\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/timesfm/utils/xreg_lib.py","lineNumber":263,"sourceCode":"        \"train_dynamic_numerical_covariates\",\n        \"test_dynamic_numerical_covariates\",\n      ),\n      (\n        self.train_dynamic_categorical_covariates,\n        self.test_dynamic_categorical_covariates,\n        \"train_dynamic_categorical_covariates\",\n        \"test_dynamic_categorical_covariates\",\n      ),\n    ):\n      if w := set(dict_a.keys()) - set(dict_b.keys()):\n        raise ValueError(f\"{dict_a_name} has keys not present in {dict_b_name}: {w}\")\n      if w := set(dict_b.keys()) - set(dict_a.keys()):\n        raise ValueError(f\"{dict_b_name} has keys not present in {dict_a_name}: {w}\")\n\n    # Check shapes.\n    if assert_covariate_shapes:\n      if len(self.targets) != len(self.train_lens):\n        raise ValueError(\n          \"targets and train_lens must have the same number of elements.\"\n        )\n\n      if len(self.train_lens) != len(self.test_lens):\n        raise ValueError(\n          \"train_lens and test_lens must have the same number of elements.\"\n        )\n\n      for i, (target, train_len) in enumerate(zip(self.targets, self.train_lens)):\n        if len(target) != train_len:\n          raise ValueError(\n            f\"targets[{i}] has length {len(target)} != expected {train_len}.\"\n          )\n\n      for key, values in self.static_numerical_covariates.items():\n        if len(values) != len(self.train_lens):\n          raise ValueError(\n            f\"static_numerical_covariates has key {key} with number of\"","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/google-research/timesfm/blob/331c6d33cb1ac2611de3056d0ac7164aab6301eb/src/timesfm/utils/xreg_lib.py#L245-L281","documentation":"_assert_covariates also validates shape consistency: the number of targets (time series) must equal the number of train_lens entries. A mismatch means the covariate-holder was constructed with inconsistent-length lists, so the raise prevents building a broken regression matrix.","triggerScenarios":"create_covariate_matrix → _assert_covariates (when assert_covariate_shapes is true) where len(self.targets) != len(self.train_lens), e.g. an XRegCovariates-like object built with per-series covariates lists of different lengths than the targets list.","commonSituations":"Constructing the covariates object with targets from one dataset and train_lens computed from another (series dropped/added); filtering series but not the lens; off-by-one or duplicated entries.","solutions":["Rebuild the covariates object ensuring targets and train_lens are derived from the same filtered list of series.","Check lengths: assert len(targets) == len(train_lens) == len(test_lens) before calling create_covariate_matrix.","If series were filtered, apply the same filter to train_lens and all covariate dicts."],"exampleFix":"// before\ncovs = make_covariates(targets=targets, train_lens=[len(t) for t in targets[:4]])  # len mismatch\n// after\ncovs = make_covariates(targets=targets, train_lens=[len(t) for t in targets])","handlingStrategy":"validation","validationCode":"assert len(targets) == len(train_lens) == len(test_lens), \\\n    f\"targets({len(targets)}), train_lens({len(train_lens)}), test_lens({len(test_lens)}) length mismatch\"","typeGuard":"def lengths_consistent(targets, train_lens, test_lens) -> bool:\n    return len(targets) == len(train_lens) == len(test_lens)","tryCatchPattern":"try:\n    covs.create_covariate_matrix()\nexcept ValueError as e:\n    if \"must have the same number of elements\" in str(e):\n        print(\"Rebuild covariates object from the same filtered series list:\", e)\n        raise","preventionTips":["Derive targets, train_lens, and test_lens from one loop over the same series list","Filter series before constructing the covariates object, not after","Add a length-consistency assert in your data-prep code"],"tags":["python","covariates","valueerror","validation"],"backgroundTag":"covariate-shape-mismatch","analyzedSha":"331c6d33cb1ac2611de3056d0ac7164aab6301eb","analyzedAt":"2026-08-29T01:04:23.138Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}