@@ -368,6 +368,30 @@ def test_save_load_state(tmp_path):
368368 np .testing .assert_array_equal (optimizer .space .target , new_optimizer .space .target )
369369
370370
371+ @pytest .mark .parametrize ("saved_points" , [False , True ])
372+ @pytest .mark .parametrize ("existing_points" , [False , True ])
373+ @pytest .mark .parametrize ("pbounds" , [{"p2" : (0 , 10 ), "p1" : (0 , 10 )}, {"width" : (0 , 10 ), "height" : (0 , 10 )}])
374+ def test_load_state_rejects_incompatible_keys (tmp_path , saved_points , existing_points , pbounds ):
375+ """Reject incompatible keys without changing results or random-state progression."""
376+ original = BayesianOptimization (target_func , PBOUNDS , random_state = 1 , verbose = 0 )
377+ if saved_points :
378+ original .probe ({"p1" : 1 , "p2" : 2 }, lazy = False )
379+ state_path = tmp_path / "optimizer_state.json"
380+ original .save_state (state_path )
381+
382+ restored = BayesianOptimization (target_func , pbounds , random_state = 7 , verbose = 0 )
383+ if existing_points :
384+ restored .probe (dict .fromkeys (pbounds , 0 ), lazy = False )
385+ before = restored .res
386+
387+ with pytest .raises (ValueError , match = "parameter keys.*same order" ):
388+ restored .load_state (state_path )
389+
390+ assert restored .res == before
391+ control = BayesianOptimization (target_func , pbounds , random_state = 7 , verbose = 0 )
392+ assert restored .random_sample (2 ) == control .random_sample (2 )
393+
394+
371395def test_save_load_w_categorical_params (tmp_path ):
372396 """Test saving and loading optimizer state with categorical parameters."""
373397
0 commit comments