google-research/timesfm · error · ValueError

{dict_b_name} has keys not present in {dict_a_name}: {w}

Error message

{dict_b_name} has keys not present in {dict_a_name}: {w}

What it means

The mirror of error 37: the test-side covariate dict has keys not present in the train-side dict. _assert_covariates raises this ValueError naming the offending keys so the key sets of each train/test pair stay identical.

Source

Thrown at src/timesfm/utils/xreg_lib.py:258

    # Check keys.
    for dict_a, dict_b, dict_a_name, dict_b_name in (
      (
        self.train_dynamic_numerical_covariates,
        self.test_dynamic_numerical_covariates,
        "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}."
          )

View on GitHub (pinned to 331c6d33cb)

Solutions

  1. Remove the extra keys from the test-side dict.
  2. Add matching keys to the train-side dict (with arrays of each series' training length).
  3. Validate key symmetry: assert set(test.keys()) <= set(train.keys()) before calling.

Example fix

// before
test = {"promo": c, "temperature": t}; train = {"promo": a}
// after
test = {"promo": c}; train = {"promo": a}
Defensive patterns

Strategy: validation

Validate before calling

extra = set(test_cat.keys()) - set(train_cat.keys())
if extra:
    raise ValueError(f"Test-side has covariate keys absent from train: {extra}")

Type guard

def test_keys_subset(train_dict, test_dict) -> bool:
    return set(test_dict.keys()).issubset(train_dict.keys())

Try / catch

try:
    covs.create_covariate_matrix()
except ValueError as e:
    if "has keys not present in" in str(e):
        print("Remove or add matching train-side keys:", e)
        raise

Prevention

When it happens

Trigger: create_covariate_matrix → _assert_covariates when test_dynamic_*_covariates contains keys absent from the corresponding train dict (e.g. test dict includes "temperature" but train dict does not).

Common situations: Building future covariates from a richer dataset than the training data; key typos; intentionally adding a covariate only for forecasting — which is not supported since the regressor is fitted on train covariates.

Related errors


AI-assisted analysis of google-research/timesfm@331c6d33cb (2026-08-29). Data as JSON: /api/errors/b0a06f8c6bc1842b. Report an issue: GitHub.