← Back to Pull requests

[BUG] validate `mode`/`return_info` early in `predict` and keep the caller's trainer unchanged #2502

Open
unknown opened this pull request with 422 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 09, 2026 15:59 UTC
Author

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 mode and return_info. A new helper _check_predict_args (in callbacks/predict.py) validates both arguments. It runs at the start of BaseForecaster.predict (before the data is loaded) and in PredictCallback.__init__ (so BaseModel.predict also fails before any batch). Before, an unknown return_info key only warned per batch and then crashed at the end with IndexError: list index out of range. Now:
- unknown mode -> `ValueError: mode must be one of ['prediction', 'quantiles', 'raw'], got 'bogus'.
- unknown key ->
ValueError: return_info contains unknown value(s) ['not_a_real_key']. Valid options: ['x', 'y', 'index', 'decoder_lengths'].
- wrong type (e.g. a set or a number) ->
TypeError with a hint.
- a single string such as
return_info="index" is treated as ["index"], and the callback keeps its own copy of the list.
2. The caller's trainer is left unchanged. Since #2514,
predict takes a trainer instead of trainer_kwargs (this replaces the trainer_kwargs mutation reported in #2500). It appends its PredictCallback to trainer.callbacks and removes it afterwards, but if prediction raised, the callback stayed on the trainer, and every later predict with that trainer ran it again. The removal is now in a finally block. The PredictCallback (and with it the argument check) is also created before the trainer is touched, so an invalid mode / return_info never changes the trainer.

Two small helpers, _check_choice and _check_choices, are added to utils/_validation.py next to the existing checks. Docstrings and type hints of the three predict-related signatures are updated to list the valid keys and the new Raises sections.

What should a reviewer concentrate their feedback on?

  • Raising on an unknown return_info 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.

  • Whether _check_predict_args should live in callbacks/predict.py (next to the valid options) or in utils/_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's callbacks stay unchanged over repeated calls and also when prediction raises; the trainer's own callbacks still run; repeated calls give the same result; requested return_info is returned,

  • BaseForecaster.predict: fails before loading the data.
  • Any other comments?

    Related but not overlapping: #2156 / #2401 (return_info reading y and index).

    PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG].

  • Added/modified tests

  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.

  • To run hooks independent of commit, execute
    pre-commit run --all-files`

    Diff: 422 lines (+316 / -106) across 5 files Reviewer: phoeenniixx Review duration: 30275m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45% (MEDIUM) · Higher = larger blast radius
    45%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24% · Safe to merge
    24%