You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeError: switch argument was for hp.choice nodes with numpy >= 1.20 #949
Running hyperopt with hp.choice raises a TypeError in pyll/base.py when the sampled index is returned as a numpy.int64 scalar rather than a Python int:
TypeError: ('switch argument was', 0)
File "hyperopt/pyll/base.py", line 863, in rec_eval
raise TypeError("switch argument was", switch_i)
Root Cause
In hyperopt/pyll/base.py around line 859, the switch node handler calls .item() on the sampled value only when it is an np.ndarray:
if isinstance(switch_i, np.ndarray):
switch_i = switch_i.item()
if isinstance(switch_i, int): # fails for np.int64 bare scalars
...
else:
raise TypeError("switch argument was", switch_i)
Prior to numpy 1.20, numpy.int64 inherited from Python's int, so the isinstance(switch_i, int) check passed silently. As of numpy 1.20, that inheritance was removed (numpy 1.20 release notes), and bare numpy.int64 scalars now fall through to the TypeError.
Reproduction
import numpy as np
print(np.__version__) # e.g. 1.26.4
print(isinstance(np.int64(0), int)) # False — this is the core issue
from hyperopt import fmin, tpe, hp, STATUS_OK
space = {'layers': hp.choice('layers', [1, 2, 3])}
fmin(fn=lambda p: {'loss': p['layers'], 'status': STATUS_OK},
space=space, algo=tpe.suggest, max_evals=5)
# Raises: TypeError: ('switch argument was', 0)
Fix
Extend the existing ndarray guard to also handle bare np.integer scalars, which have the same .item() method:
# Before:
if isinstance(switch_i, np.ndarray):
switch_i = switch_i.item()
if isinstance(switch_i, int):
# After:
if isinstance(switch_i, np.ndarray):
switch_i = switch_i.item()
if isinstance(switch_i, np.integer):
switch_i = int(switch_i)
if isinstance(switch_i, int):
np.integer is the abstract base class for all numpy integer scalar types (np.int64, np.int32, etc.) and has been stable across numpy versions. This is a one-line change with no behavioral impact for environments where numpy.int64 still behaves as a Python int.
Activity
fix: handle numpy.int64 scalar in switch node eval (fixes hyperopt#949)
fix: handle numpy.int64 scalar in switch node eval (fixes #949)
jaberg commentedon Jul 24, 2026
Hopefully addressed via #959, reply there if there are further issues?