Skip to content

Fix pickle #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open

Fix pickle #428

wants to merge 1 commit into from

Conversation

mschwoer
Copy link
Contributor

@mschwoer mschwoer commented Apr 17, 2025

This pull request enhances the session management functionality in alphastats by introducing support for an additional serialization library (dill) alongside the existing cloudpickle. It also updates the session saving and loading mechanisms to handle multiple file extensions and improve compatibility.

Serialization Enhancements:

  • Added dill as an alternative serialization library to handle cases where cloudpickle fails. (alphastats/gui/utils/session_manager.py) [1] [2]
  • Introduced a new file extension (dpkl) for files serialized with dill, alongside the existing cpkl for cloudpickle. (alphastats/gui/utils/session_manager.py) [1] [2]

Session Management Updates:

  • Updated get_saved_sessions to support both cpkl and dpkl file extensions when retrieving saved session files. (alphastats/gui/utils/session_manager.py)
  • Enhanced the save method to attempt serialization with cloudpickle first, falling back to dill if an exception occurs. (alphastats/gui/utils/session_manager.py)
  • Modified the load method to correctly identify and deserialize files based on their extensions (cpkl or dpkl). (alphastats/gui/utils/session_manager.py)

@mschwoer mschwoer changed the base branch from main to development April 17, 2025 06:13
@mschwoer mschwoer requested a review from Copilot April 17, 2025 06:14
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request enhances session management by adding support for an alternative serialization library (dill) and by handling multiple file extensions for session files.

  • Introduces dill as a fallback when cloudpickle fails during serialization.
  • Adds support for two file extensions (cpkl and dpkl) in both saving and loading sessions.
Comments suppressed due to low confidence (2)

alphastats/gui/utils/session_manager.py:36

  • [nitpick] Consider using more descriptive constant names (e.g., CLOUDPICKLE_EXT and DILL_EXT) to improve code readability.
_EXT_C = "cpkl"

alphastats/gui/utils/session_manager.py:125

  • If the file suffix is not '.cpkl' or '.dpkl', the code does not initialize 'loaded_data', which may lead to an undefined variable error. Consider adding an else block to handle unexpected file extensions.
if file_path.exists():

@JuliaS92
Copy link
Collaborator

Still raises with a chat session:
TypeError: cannot pickle 'SSLContext' object
Traceback:

File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
result = func()
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec
exec(code, module.dict)
File "/Users/schessner/Documents/gitrepos/alphapeptstats/alphastats/gui/pages/06_LLM.py", line 37, in
sidebar_info()
File "/Users/schessner/Documents/gitrepos/alphapeptstats/alphastats/gui/utils/ui_helper.py", line 29, in sidebar_info
saved_file_path = SessionManager().save(st.session_state, session_name)
File "/Users/schessner/Documents/gitrepos/alphapeptstats/alphastats/gui/utils/session_manager.py", line 114, in save
dillpickle.dump(data_to_dump, f)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 260, in dump
Pickler(file, protocol, **_kwds).dump(obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 428, in dump
StockPickler.dump(self, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 487, in dump
self.save(obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 1002, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 603, in save
self.save_reduce(obj=obj, *rv)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 717, in save_reduce
save(state)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 603, in save
self.save_reduce(obj=obj, *rv)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 717, in save_reduce
save(state)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 603, in save
self.save_reduce(obj=obj, *rv)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 717, in save_reduce
save(state)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 603, in save
self.save_reduce(obj=obj, *rv)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 717, in save_reduce
save(state)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 1002, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 603, in save
self.save_reduce(obj=obj, *rv)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 717, in save_reduce
save(state)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 560, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 1262, in save_module_dict
StockPickler.save_dict(pickler, obj)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 971, in save_dict
self._batch_setitems(obj.items())
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 997, in _batch_setitems
save(v)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/site-packages/dill/_dill.py", line 422, in save
StockPickler.save(self, obj, save_persistent_id)
File "/Users/schessner/anaconda3/envs/alphastats_dev/lib/python3.9/pickle.py", line 578, in save
rv = reduce(self.proto)

@JuliaS92
Copy link
Collaborator

JuliaS92 commented May 8, 2025

I think we need to skip the LLMIntegration object and instead only save the serializable attributes of it.

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