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
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
feature: track developer errors if there is an api key
Shawn Qiu2fd036dd081d
diff --git a/agentops/client.py b/agentops/client.pyindex 6fc14228..8e54edbb 100644--- a/agentops/client.py+++ b/agentops/client.py@@ -22,11 +22,12 @@ import atexitimport signalimport sys+from .developer_errors import ExceptionHandlerMetafrom .config import Configurationfrom .llm_tracker import LlmTracker-class Client:+class Client(metaclass=ExceptionHandlerMeta):"""Client for AgentOps service.@@ -104,7 +105,8 @@ class Client:if self._session is not None and not self._session.has_ended:self._worker.add_event(event.__dict__)else:- logging.warning("AgentOps: Cannot record event - no current session")+ logging.warning(+ "AgentOps: Cannot record event - no current session")def record_function(self, event_name: str, tags: Optional[List[str]] = None):"""diff --git a/agentops/developer_errors.py b/agentops/developer_errors.pynew file mode 100644index 00000000..28fbe690--- /dev/null+++ b/agentops/developer_errors.py@@ -0,0 +1,53 @@+import logging+import toml+import traceback+from .http_client import HttpClient+from .helpers import safe_serialize+++class ExceptionHandlerMeta(type):+ """Metaclass to automatically decorate methods with exception handling and provide a shared exception handler."""++ def __new__(cls, name, bases, dct):+ # Wrap each method with the handle_exceptions decorator+ for method_name, method in dct.items():+ if callable(method) and not method_name.startswith("__"):+ dct[method_name] = handle_exceptions(method)++ return super().__new__(cls, name, bases, dct)++ def send_exception_to_server(cls, exception, api_key):+ """Class method to send exception to server. Assumes `api_key` is passed explicitly."""+ if api_key:+ exception_type = type(exception).__name__+ exception_message = str(exception)+ exception_traceback = traceback.format_exc() # Get the full stack trace+ developer_error = {+ "sdk_version": read_version_from_pyproject(),+ "type": exception_type,+ "message": exception_message,+ "stack_trace": exception_traceback+ }+ HttpClient.post("https://api.agentops.ai/developer_errors",+ safe_serialize(developer_error).encode("utf-8"),+ api_key=api_key)+++def handle_exceptions(method):+ """Decorator within the metaclass to wrap method execution in try-except block."""++ def wrapper(self, *args, **kwargs):+ try:+ return method(self, *args, **kwargs)+ except Exception as e:+ type(self).send_exception_to_server(e, self.config._api_key)+ logging.warning(f"AgentOps: Error: {e}")+ # raise e++ return wrapper+++def read_version_from_pyproject():+ with open("../pyproject.toml", "r") as pyproject_file:+ pyproject_contents = toml.load(pyproject_file)+ return pyproject_contents['project']['version']diff --git a/agentops/helpers.py b/agentops/helpers.pyindex 055556a6..66b87201 100644--- a/agentops/helpers.py+++ b/agentops/helpers.py@@ -1,6 +1,8 @@+from typing import Dictfrom enum import Enumimport timefrom datetime import datetime+import jsonfrom packaging.version import parse@@ -14,6 +16,37 @@ def get_ISO_time():return datetime.utcfromtimestamp(time.time()).isoformat(timespec='milliseconds') + 'Z'+def is_jsonable(x):+ try:+ json.dumps(x)+ return True+ except (TypeError, OverflowError):+ return False+++def filter_unjsonable(d: dict) -> dict:+ def filter_dict(obj):+ if isinstance(obj, dict):+ return {k: filter_dict(v) if is_jsonable(v) else "" for k, v in obj.items()}+ elif isinstance(obj, list):+ return [filter_dict(x) if isinstance(x, (dict, list)) else x for x in obj]+ else:+ return obj if is_jsonable(obj) else ""++ return filter_dict(d)+++def safe_serialize(obj):+ def default(o):+ if hasattr(o, 'model_dump_json'):+ return o.model_dump_json()+ elif hasattr(o, 'to_json'):+ return o.to_json()+ else:+ return f"<<non-serializable: {type(o).__qualname__}>>"+ return json.dumps(obj, default=default)++class Models(Enum):GPT_3_5_TURBO = "gpt-3.5-turbo"GPT_3_5_TURBO_0301 = "gpt-3.5-turbo-0301"diff --git a/agentops/http.py b/agentops/http_client.pysimilarity index 100%rename from agentops/http.pyrename to agentops/http_client.pydiff --git a/agentops/worker.py b/agentops/worker.pyindex f6928340..36834f75 100644--- a/agentops/worker.py+++ b/agentops/worker.py@@ -1,42 +1,11 @@import jsonimport threadingimport time-from .http import HttpClient+from .http_client import HttpClientfrom .config import Configurationfrom .session import Session+from .helpers import safe_serialize, filter_unjsonablefrom typing import Dict-import logging---def is_jsonable(x):- try:- json.dumps(x)- return True- except (TypeError, OverflowError):- return False---def filter_unjsonable(d: dict) -> dict:- def filter_dict(obj):- if isinstance(obj, dict):- return {k: filter_dict(v) if is_jsonable(v) else "" for k, v in obj.items()}- elif isinstance(obj, list):- return [filter_dict(x) if isinstance(x, (dict, list)) else x for x in obj]- else:- return obj if is_jsonable(obj) else ""-- return filter_dict(d)---def safe_serialize(obj):- def default(o):- if hasattr(o, 'model_dump_json'):- return o.model_dump_json()- elif hasattr(o, 'to_json'):- return o.to_json()- else:- return f"<<non-serializable: {type(o).__qualname__}>>"- return json.dumps(obj, default=default)class Worker: