google-research/timesfm · error · ValueError

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

Error message

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

What it means

During covariate validation, each train/test dict pair is compared by key set. If the train-side dict contains keys missing from the test-side dict, this ValueError lists those extra keys (format: "{dict_a_name} has keys not present in {dict_b_name}: {...}").

Source

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

      )

    # 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(

View on GitHub (pinned to 331c6d33cb)

Solutions

  1. Add the missing key(s) to the test-side dict with arrays of length equal to the forecast horizon.
  2. Remove the extra key(s) from the train-side dict if the covariate is not needed.
  3. Assert set(train.keys()) == set(test.keys()) in your own code before calling the forecaster.

Example fix

// before
train = {"promo": a, "holiday": b}; test = {"promo": c}
// after
train = {"promo": a, "holiday": b}; test = {"promo": c, "holiday": d}
Defensive patterns

Strategy: validation

Validate before calling

extra = set(train_cat.keys()) - set(test_cat.keys())
if extra:
    raise ValueError(f"Missing test-side covariate keys: {extra}")

Type guard

def keys_match(train_dict, test_dict) -> bool:
    return set(train_dict.keys()) == set(test_dict.keys())

Try / catch

try:
    covs.create_covariate_matrix()
except ValueError as e:
    if "has keys not present in" in str(e):
        print("Align train/test covariate keys, then retry:", e)
        raise

Prevention

When it happens

Trigger: create_covariate_matrix → _assert_covariates with dicts whose key sets differ, e.g. train dict has covariate "holiday" but the test dict does not.

Common situations: Adding a new covariate to training data but forgetting to add it to test data; typos in dict keys; serializing covariates from different data sources with inconsistent naming.

Related errors


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