google-research/timesfm · error · ValueError
targets and train_lens must have the same number of elements
Error message
targets and train_lens must have the same number of elements.
What it means
_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.
Source
Thrown at src/timesfm/utils/xreg_lib.py:263
"train_dynamic_numerical_covariates",
"test_dynamic_numerical_covariates",
),
(
self.train_dynamic_categorical_covariates,
self.test_dynamic_categorical_covariates,
"train_dynamic_categorical_covariates",
"test_dynamic_categorical_covariates",
),
):
if w := set(dict_a.keys()) - set(dict_b.keys()):
raise ValueError(f"{dict_a_name} has keys not present in {dict_b_name}: {w}")
if w := set(dict_b.keys()) - set(dict_a.keys()):
raise ValueError(f"{dict_b_name} has keys not present in {dict_a_name}: {w}")
# Check shapes.
if assert_covariate_shapes:
if len(self.targets) != len(self.train_lens):
raise ValueError(
"targets and train_lens must have the same number of elements."
)
if len(self.train_lens) != len(self.test_lens):
raise ValueError(
"train_lens and test_lens must have the same number of elements."
)
for i, (target, train_len) in enumerate(zip(self.targets, self.train_lens)):
if len(target) != train_len:
raise ValueError(
f"targets[{i}] has length {len(target)} != expected {train_len}."
)
for key, values in self.static_numerical_covariates.items():
if len(values) != len(self.train_lens):
raise ValueError(
f"static_numerical_covariates has key {key} with number of"View on GitHub (pinned to 331c6d33cb)
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.
Example fix
// before covs = make_covariates(targets=targets, train_lens=[len(t) for t in targets[:4]]) # len mismatch // after covs = make_covariates(targets=targets, train_lens=[len(t) for t in targets])
Defensive patterns
Strategy: validation
Validate before calling
assert len(targets) == len(train_lens) == len(test_lens), \
f"targets({len(targets)}), train_lens({len(train_lens)}), test_lens({len(test_lens)}) length mismatch" Type guard
def lengths_consistent(targets, train_lens, test_lens) -> bool:
return len(targets) == len(train_lens) == len(test_lens) Try / catch
try:
covs.create_covariate_matrix()
except ValueError as e:
if "must have the same number of elements" in str(e):
print("Rebuild covariates object from the same filtered series list:", e)
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- train_dynamic_numerical_covariates and test_dynamic_numerica
- train_dynamic_categorical_covariates and test_dynamic_catego
- {dict_a_name} has keys not present in {dict_b_name}: {w}
- {dict_b_name} has keys not present in {dict_a_name}: {w}
- At least one of dynamic_numerical_covariates, dynamic_catego
AI-assisted analysis of google-research/timesfm@331c6d33cb (2026-08-29).
Data as JSON: /api/errors/9ad4e44e61810df3.
Report an issue: GitHub.