{"record":{"id":"b0a06f8c6bc1842b","repo":"google-research/timesfm","slug":"dict-b-name-has-keys-not-present-in-dict-a-name","errorCode":null,"errorMessage":"{dict_b_name} has keys not present in {dict_a_name}: {w}","messagePattern":"(.+?) has keys not present in (.+?): (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/timesfm/utils/xreg_lib.py","lineNumber":258,"sourceCode":"    # Check keys.\n    for dict_a, dict_b, dict_a_name, dict_b_name in (\n      (\n        self.train_dynamic_numerical_covariates,\n        self.test_dynamic_numerical_covariates,\n        \"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          )","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/google-research/timesfm/blob/331c6d33cb1ac2611de3056d0ac7164aab6301eb/src/timesfm/utils/xreg_lib.py#L240-L276","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Remove the extra keys from the test-side dict.","Add matching keys to the train-side dict (with arrays of each series' training length).","Validate key symmetry: assert set(test.keys()) <= set(train.keys()) before calling."],"exampleFix":"// before\ntest = {\"promo\": c, \"temperature\": t}; train = {\"promo\": a}\n// after\ntest = {\"promo\": c}; train = {\"promo\": a}","handlingStrategy":"validation","validationCode":"extra = set(test_cat.keys()) - set(train_cat.keys())\nif extra:\n    raise ValueError(f\"Test-side has covariate keys absent from train: {extra}\")","typeGuard":"def test_keys_subset(train_dict, test_dict) -> bool:\n    return set(test_dict.keys()).issubset(train_dict.keys())","tryCatchPattern":"try:\n    covs.create_covariate_matrix()\nexcept ValueError as e:\n    if \"has keys not present in\" in str(e):\n        print(\"Remove or add matching train-side keys:\", e)\n        raise","preventionTips":["Never add forecast-only covariates to the test dict without training data for them","Build both dicts from the same schema/column list","Compare key sets in a unit test for your covariate pipeline"],"tags":["python","covariates","valueerror","validation"],"backgroundTag":"covariate-key-mismatch","analyzedSha":"331c6d33cb1ac2611de3056d0ac7164aab6301eb","analyzedAt":"2026-08-29T01:04:23.138Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}