Skip to content

Added serialize function for save #529

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

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion aixplain/utils/asset_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def save(self):
data_dict = {}
for asset_id, asset in self.store.data.items():
try:
data_dict[asset_id] = asset.to_dict()
data_dict[asset_id] = serialize(asset)
except Exception as e:
logger.error(f"Error serializing {asset_id}: {e}")
serializable_store = {
Expand All @@ -138,3 +138,18 @@ def get_all(self) -> List[T]:

def has_valid_cache(self) -> bool:
return self.store.expiry >= time.time() and bool(self.store.data)

def serialize(obj):
if isinstance(obj, (str, int, float, bool, type(None))):
return obj
elif isinstance(obj, (list, tuple, set)):
return [serialize(o) for o in obj]
elif isinstance(obj, dict):
return {str(k): serialize(v) for k, v in obj.items()}
elif hasattr(obj, "to_dict"):
return serialize(obj.to_dict())
elif hasattr(obj, "__dict__"):
return serialize(vars(obj))
else:
return str(obj)

2 changes: 1 addition & 1 deletion tests/functional/model/run_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_aixplain_model_cache_creation():

assert "data" in cache_data, "Cache file structure invalid - missing 'data' key."
assert any(m.get("id") == model_id for m in cache_data["data"]["items"]), "Instantiated model not found in cache."
=======

def test_index_model_air_with_image():
from aixplain.factories import IndexFactory
from aixplain.modules.model.record import Record
Expand Down