Skip to content
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

Added try catch and extra cases to safe_serialize #421

Merged
merged 1 commit into from
Sep 21, 2024
Merged
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
35 changes: 21 additions & 14 deletions agentops/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@ def filter_dict(obj):

def safe_serialize(obj):
def default(o):
if isinstance(o, UUID):
return str(o)
elif hasattr(o, "model_dump_json"):
return o.model_dump_json()
elif hasattr(o, "to_json"):
return o.to_json()
elif hasattr(o, "json"):
return o.json()
elif hasattr(o, "to_dict"):
return o.to_dict()
elif hasattr(o, "dict"):
return o.dict()
else:
return f"<<non-serializable: {type(o).__qualname__}>>"
try:
if isinstance(o, UUID):
return str(o)
elif hasattr(o, "model_dump_json"):
return str(o.model_dump_json())
elif hasattr(o, "to_json"):
return str(o.to_json())
elif hasattr(o, "json"):
return str(o.json())
elif hasattr(o, "to_dict"):
return {k: str(v) for k, v in o.to_dict().items() if not callable(v)}
elif hasattr(o, "dict"):
return {k: str(v) for k, v in o.dict().items() if not callable(v)}
elif isinstance(o, dict):
return {k: str(v) for k, v in o.items()}
elif isinstance(o, list):
return [str(item) for item in o]
else:
return f"<<non-serializable: {type(o).__qualname__}>>"
except Exception as e:
return f"<<serialization-error: {str(e)}>>"

def remove_unwanted_items(value):
"""Recursively remove self key and None/... values from dictionaries so they aren't serialized"""
Expand Down
Loading