Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

added try catch to serializer (#421)

Alex Reibman122ad1dbe23e
diff --git a/agentops/helpers.py b/agentops/helpers.py
index 2ec13264..fef08a54 100644
--- a/agentops/helpers.py
+++ b/agentops/helpers.py
@@ -59,20 +59,27 @@ def filter_unjsonable(d: dict) -> dict:
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"""