Skip to content

fix(cache): hash numpy scalars in cache keys - #272

Merged
donghaoren merged 1 commit into
apple:mainfrom
Yigtwxx:fix/cache-key-numpy-scalars
Oct 2, 2026
Merged

donghaoren merged 1 commit into
apple:mainfrom
Yigtwxx:fix/cache-key-numpy-scalars

Conversation

@Yigtwxx

@Yigtwxx Yigtwxx commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Problem

sha256_hexdigest falls back to json.dumps for anything that isnt bytes/str/ndarray/list/dict. numpy scalars (np.int64, np.float32, np.bool_) arent json serializable so the cache key raises. easy to hit with a param sweep:

for n in np.arange(5, 50, 5):
    compute_projection(df, inputs="v", umap_args={"n_neighbors": n})
# TypeError: Object of type int64 is not JSON serializable

np.float64 works only because it subclasses float.

Fix

convert np.generic with .item() before hashing. np.int64(15) and 15 now give the same key. keys that already worked (incl np.float64) dont change so existing caches stay valid. added a test in test_cache.py.

Keys like umap_args={"n_neighbors": np.int64(15)} fell through to json.dumps and raised TypeError. Convert np.generic with .item() so they hash like the equivalent Python value; keys that already worked are unchanged.
@donghaoren
donghaoren enabled auto-merge (squash) October 2, 2026 23:14
@donghaoren
donghaoren merged commit 2f750ac into apple:main Oct 2, 2026
8 checks passed
@Yigtwxx
Yigtwxx deleted the fix/cache-key-numpy-scalars branch October 3, 2026 08:34
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.

2 participants