google-research/timesfm · error · ValueError
train_dynamic_numerical_covariates and test_dynamic_numerica
Error message
train_dynamic_numerical_covariates and test_dynamic_numerical_covariates must be both present or both absent.
What it means
_assert_covariates enforces that dynamic numerical covariates are provided symmetrically: train_dynamic_numerical_covariates and test_dynamic_numerical_covariates must both be set or both be None. Supplying only one side raises this ValueError in create_covariate_matrix.
Source
Thrown at src/timesfm/utils/xreg_lib.py:221
train_dynamic_categorical_covariates or {}
)
self.test_dynamic_numerical_covariates = test_dynamic_numerical_covariates or {}
self.test_dynamic_categorical_covariates = test_dynamic_categorical_covariates or {}
self.static_numerical_covariates = static_numerical_covariates or {}
self.static_categorical_covariates = static_categorical_covariates or {}
def _assert_covariates(self, assert_covariate_shapes: bool = False) -> None:
"""Verifies the validity of the covariate inputs."""
# Check presence.
if (
self.train_dynamic_numerical_covariates
and not self.test_dynamic_numerical_covariates
) or (
not self.train_dynamic_numerical_covariates
and self.test_dynamic_numerical_covariates
):
raise ValueError(
"train_dynamic_numerical_covariates and"
" test_dynamic_numerical_covariates must be both present or both"
" absent."
)
if (
self.train_dynamic_categorical_covariates
and not self.test_dynamic_categorical_covariates
) or (
not self.train_dynamic_categorical_covariates
and self.test_dynamic_categorical_covariates
):
raise ValueError(
"train_dynamic_categorical_covariates and"
" test_dynamic_categorical_covariates must be both present or both"
" absent."
)
View on GitHub (pinned to 331c6d33cb)
Solutions
- Provide both train_dynamic_numerical_covariates and test_dynamic_numerical_covariates with matching keys.
- If no dynamic numerical covariates are intended, pass None (or omit) for both sides.
- For future unknown values, supply placeholder/forecasted covariate values in the test dict.
Example fix
// before forecast_with_covariates(inputs, dynamic_numerical_covariates=train_num, dynamic_categorical_covariates=None) // after forecast_with_covariates(inputs, dynamic_numerical_covariates=train_num, dynamic_categorical_covariates=None) # ensure the API receives BOTH train_num and test_num dicts
Defensive patterns
Strategy: validation
Validate before calling
assert (train_num is None) == (test_num is None), \
"train and test dynamic numerical covariates must be both present or both absent" Type guard
def numerics_symmetric(train_num, test_num) -> bool:
return (train_num is None) == (test_num is None) Try / catch
try:
forecaster.forecast_with_covariates(...)
except ValueError as e:
if "dynamic_numerical_covariates must be both present" in str(e):
test_num = {k: forecast_values for k in train_num}
forecaster.forecast_with_covariates(...)
else:
raise Prevention
- Build train/test covariate dicts together in one helper function
- Pass None for BOTH sides when a covariate type is unused
- Remember future covariates must be supplied even if values are forecasts/placeholders
When it happens
Trigger: Calling forecast_with_covariates / create_covariate_matrix passing train_dynamic_numerical_covariates without test_dynamic_numerical_covariates (or vice versa).
Common situations: Forecasting future points where users assume test covariates can be omitted; providing historical-only covariates for model fitting; copying examples that only show one dict.
Related errors
- 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}
- targets and train_lens must have the same number of elements
- 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/2cef47b2b607590f.
Report an issue: GitHub.