[BUG] validate `mode`/`return_info` early in `predict` and keep the caller's trainer unchanged #2502
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 422 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.362
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2502"
general
weight: 0.35
phoeenniixx
Reference Issues/PRs
Fixes #2500.
What does this implement/fix? Explain your changes.
Two fixes in the v2 predict path, as described in #2500:
1. Early, clear errors for
modeandreturn_info. A new helper_check_predict_args(incallbacks/predict.py) validates both arguments. It runs at the start ofBaseForecaster.predict(before the data is loaded) and inPredictCallback.__init__(soBaseModel.predictalso fails before any batch). Before, an unknownreturn_infokey only warned per batch and then crashed at the end withIndexError: list index out of range. Now:- unknown
mode-> `ValueError:modemust be one of ['prediction', 'quantiles', 'raw'], got 'bogus'.- unknown key ->
ValueError:return_infocontains unknown value(s) ['not_a_real_key']. Valid options: ['x', 'y', 'index', 'decoder_lengths'].TypeError- wrong type (e.g. a set or a number) ->
with a hint.return_info="index"- a single string such as
is treated as["index"], and the callback keeps its own copy of the list.predict2. The caller's trainer is left unchanged. Since #2514,
takes atrainerinstead oftrainer_kwargs(this replaces thetrainer_kwargsmutation reported in #2500). It appends itsPredictCallbacktotrainer.callbacksand removes it afterwards, but if prediction raised, the callback stayed on the trainer, and every laterpredictwith that trainer ran it again. The removal is now in afinallyblock. ThePredictCallback(and with it the argument check) is also created before the trainer is touched, so an invalidmode/return_infonever changes the trainer.Two small helpers, _check_choice
and_check_choices, are added toutils/_validation.pynext to the existing checks. Docstrings and type hints of the threepredict-related signatures are updated to list the valid keys and the newRaisessections.What should a reviewer concentrate their feedback on?
key instead of warning: it was a crash at the end of the run before, so no working code should depend on the old behavior.should live incallbacks/predict.py(next to the valid options) or inutils/_validation.py.Did you add any tests for the change?
Yes, tests/test_predict_validation.py
(31 test cases), covering:
the two new helpers,
PredictCallback: every valid mode and key accepted; invalid modes (including a list), unknown keys and wrong types rejected; single-string and copy behavior,
BaseModel.predict: fails before the first batch without touching the trainer; the trainer'scallbacksstay unchanged over repeated calls and also when prediction raises; the trainer's own callbacks still run; repeated calls give the same result; requestedreturn_infois returned,
BaseForecaster.predict: fails before loading the data.Any other comments?
Related but not overlapping: #2156 / #2401 (return_info
readingyandindex).PR checklist
.To run hooks independent of commit, execute pre-commit run --all-files`