Skip to content

Make SafetensorError pickle-able (fixes #684)#704

Open
jayzuccarelli wants to merge 1 commit intohuggingface:mainfrom
jayzuccarelli:fix/safetensor-error-pickle-684
Open

Make SafetensorError pickle-able (fixes #684)#704
jayzuccarelli wants to merge 1 commit intohuggingface:mainfrom
jayzuccarelli:fix/safetensor-error-pickle-684

Conversation

@jayzuccarelli
Copy link

Fixes #684

Problem: SafetensorError is defined in the Rust extension (_safetensors_rust), so pickle.dumps(SafetensorError()) fails (e.g. "Can't pickle: import of module 'safetensors_rust' failed"). Many integrations (multiprocessing, joblib, distributed workers) expect exceptions to be picklable.

Solution: Introduce a Python subclass of the Rust SafetensorError that implements __reduce__, and export it as SafetensorError. Exceptions raised from Rust are still caught by except SafetensorError (same type hierarchy). Pickling/unpickling now uses the Python class, so the Rust module is not required when unpickling.

  • bindings/python/py_src/safetensors/__init__.py: Python SafetensorError subclass with __reduce__(self) -> (SafetensorError, self.args).
  • bindings/python/tests/test_safetensor_error_pickle.py: Tests for pickle round-trip (empty and with message).

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.

SafetensorError isn't pickle-able, it should be (smoke test for issue import of module 'safetensors_rust' failed)

1 participant