Skip to content

Reject incompatible parameter keys when loading optimizer state - #629

Open
ktz03 wants to merge 2 commits into
bayesian-optimization:masterfrom
ktz03:fix-load-state-parameter-keys-hourly-0836
Open

ktz03 wants to merge 2 commits into
bayesian-optimization:masterfrom
ktz03:fix-load-state-parameter-keys-hourly-0836

Conversation

@ktz03

@ktz03 ktz03 commented Oct 11, 2026 •

Copy link
Copy Markdown

load_state() replays saved observation arrays using the receiving optimizer's parameter order without checking the saved keys. Saving x=1, y=2, target=21 and loading into pbounds={"y": ..., "x": ...} silently associates that target with y=1, x=2. Replacing the parameter names is also accepted when the dimensions match.

Validate the saved names and their order at the start of _load_state_dict() and raise a clear ValueError on a mismatch, before registering observations or changing the receiver's random state. Add eight regression cases covering reversed and renamed keys, empty/populated saved states, and empty/populated receiving optimizers. Same-order round trips retain the existing behavior.

Validation on Windows, Python 3.12.14, NumPy 2.5.3, SciPy 1.18.1, and scikit-learn 1.9.1:

  • Public save_state() / load_state() reproduction: four incompatible-key cases fail on untouched master, while two same-order controls pass; all six cases pass after the fix.
  • Complete pytest suite, including all eight example notebooks: 184 passed. With the new tests and untouched production code: eight failures and 176 passes, with no skips.
  • Full configured Ruff 0.12.3 pre-commit lint and format hooks and git diff --check pass.

The key-order mismatch was previously identified in the CodeRabbit review on #609. The state persistence API was introduced by @adrianmolzon in #547. This change addresses parameter names and order; it does not introduce state migration or validation of the other saved configuration fields. Documentation builds and the other platform/Python-version combinations were not run locally.

Summary by CodeRabbit

  • Bug Fixes
    • Loading saved optimizer state rejects parameter keys that differ or appear in a different order, preventing incompatible state from being restored.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ed4af42-cb9d-4c67-a8c0-9d12e4af72d0

📥 Commits

Reviewing files that changed from the base of the PR and between 3660dc9 and e47d1a3.


📒 Files selected for processing (1)
  • tests/test_bayesian_optimization.py

🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_bayesian_optimization.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.



📝 Walkthrough

Walkthrough

State loading checks that saved parameter keys match the optimizer’s keys in membership and order. It raises ValueError before restoring state when they do not match. Tests verify that rejection leaves optimizer results and subsequent random samples unchanged.

Changes

State Loading

Layer / File(s) Summary
Validate parameter keys before restoration
bayes_opt/bayesian_optimization.py, tests/test_bayesian_optimization.py
_load_state_dict rejects saved parameter keys that differ or appear in a different order. Tests cover observation variants and verify that rejection leaves results and subsequent random samples unchanged.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix


Merge Risk: ⚪ Minimal · up to e47d1

Loading saved state into an optimizer with different or reordered parameters now fails early with a clear error instead of silently misassociating observations. Same-order loads keep their existing behavior. No merge-blocking risk is identified.

Pre-merge checks | Passed 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: rejecting incompatible parameter keys when loading optimizer state.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant