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

Fixed uuid/str. Added filtering to safe_serialize just like filter_unjsonable does

Howard Gil1a98fc4278bf
diff --git a/agentops/agent.py b/agentops/agent.py
index 989f492d..44f0f9cd 100644
--- a/agentops/agent.py
+++ b/agentops/agent.py
@@ -11,7 +11,7 @@ def track_agent(name: str | None):
original_init = obj.__init__
def new_init(self, *args, **kwargs):
- self._agent_ops_agent_id = str(uuid4())
+ self._agent_ops_agent_id = uuid4()
ao_client = Client()
ao_client.create_agent(self._agent_ops_agent_id, self._agent_ops_agent_name)
original_init(self, *args, **kwargs)
@@ -19,7 +19,7 @@ def track_agent(name: str | None):
obj.__init__ = new_init
elif isfunction(obj):
- obj._agent_ops_agent_id = str(uuid4())
+ obj._agent_ops_agent_id = uuid4()
ao_client = Client()
ao_client.create_agent(obj._agent_ops_agent_id, obj._agent_ops_agent_name)
diff --git a/agentops/client.py b/agentops/client.py
index ff88ef18..700bbcda 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -226,8 +226,7 @@ class Client(metaclass=MetaClient):
if not config and not self.config:
return logging.warning("AgentOps: Cannot start session - missing configuration")
- self._session = Session(
- str(uuid4()), tags or self._tags, host_env=get_host_env())
+ self._session = Session(uuid4(), tags or self._tags, host_env=get_host_env())
self._worker = Worker(config or self.config)
self._worker.start_session(self._session)
logging.info('View info on this session at https://agentops.ai/dashboard?session_id={}'
diff --git a/agentops/event.py b/agentops/event.py
index f03fe86f..dfa35b5a 100644
--- a/agentops/event.py
+++ b/agentops/event.py
@@ -9,7 +9,7 @@ from dataclasses import dataclass, field
from typing import List, Optional
from .helpers import get_ISO_time
from .enums import EventType, Models
-from uuid import uuid4
+from uuid import UUID, uuid4
@dataclass
@@ -20,18 +20,14 @@ class Event:
returns: Optional[str] = None
init_timestamp: Optional[str] = field(default_factory=get_ISO_time)
end_timestamp: str = field(default_factory=get_ISO_time)
- # TODO: instead of making this a str we can keep it as uuid4 and just handle it on serialization
- # e.g. if isinstance(o, UUID): return str(o)
- # we might have to do this bc when they start passing uuid4 it'll error unless they do str(uuid4())
- # this is why i have to use lambda
- id: str = field(default_factory=lambda: str(uuid4()))
+ id: UUID = field(default_factory=uuid4)
@dataclass
class ActionEvent(Event):
event_type: str = EventType.ACTION.value
# TODO: Should not be optional, but non-default argument 'agent_id' follows default argument error
- agent_id: Optional[uuid4] = None
+ agent_id: Optional[UUID] = None
action_type: Optional[str] = None
logs: Optional[str] = None
screenshot: Optional[str] = None
@@ -45,8 +41,8 @@ class ActionEvent(Event):
@dataclass
class LLMEvent(Event):
event_type: str = EventType.LLM.value
- agent_id: Optional[uuid4] = None
- thread_id: Optional[uuid4] = None
+ agent_id: Optional[UUID] = None
+ thread_id: Optional[UUID] = None
prompt: Optional[str] = None
completion: Optional[str] = None
model: Optional[Models] = None
@@ -57,7 +53,7 @@ class LLMEvent(Event):
@dataclass
class ToolEvent(Event):
event_type: str = EventType.TOOL.value
- agent_id: Optional[uuid4] = None
+ agent_id: Optional[UUID] = None
name: Optional[str] = None
logs: Optional[str] = None
@@ -65,7 +61,7 @@ class ToolEvent(Event):
####################
@dataclass
class Error():
- trigger_event_id: Optional[uuid4]
+ trigger_event_id: Optional[UUID]
trigger_event_type: Optional[EventType]
error_type: Optional[str] = None
code: Optional[str] = None
diff --git a/agentops/helpers.py b/agentops/helpers.py
index 3e0ae4ca..79e7fc16 100644
--- a/agentops/helpers.py
+++ b/agentops/helpers.py
@@ -3,6 +3,7 @@ from datetime import datetime
import json
import inspect
import logging
+from uuid import UUID
def singleton(class_):
@@ -37,18 +38,21 @@ def is_jsonable(x):
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()}
+ # TODO: clean up this mess lol
+ return {k: filter_dict(v) if isinstance(v, (dict, list)) or is_jsonable(v) else str(v) if isinstance(v, UUID) 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]
+ return [filter_dict(x) if isinstance(x, (dict, list)) or is_jsonable(x) else str(x) if isinstance(x, UUID) else "" for x in obj]
else:
- return obj if is_jsonable(obj) else ""
+ return obj if is_jsonable(obj) or isinstance(obj, UUID) else ""
return filter_dict(d)
def safe_serialize(obj):
def default(o):
- if hasattr(o, 'model_dump_json'):
+ 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()
diff --git a/agentops/session.py b/agentops/session.py
index 8b1d8a11..4944059c 100644
--- a/agentops/session.py
+++ b/agentops/session.py
@@ -1,5 +1,6 @@
from .helpers import get_ISO_time
from typing import Optional, List
+from uuid import UUID
class Session:
@@ -7,7 +8,7 @@ class Session:
Represents a session of events, with a start and end state.
Args:
- session_id (str): The session id is used to record particular runs.
+ session_id (UUID): The session id is used to record particular runs.
tags (List[str], optional): Tags that can be used for grouping or sorting later. Examples could be ["GPT-4"].
Attributes:
@@ -19,7 +20,7 @@ class Session:
"""
- def __init__(self, session_id: str, tags: Optional[List[str]] = None, host_env: Optional[dict] = None):
+ def __init__(self, session_id: UUID, tags: Optional[List[str]] = None, host_env: Optional[dict] = None):
self.end_timestamp = None
self.rating = None
self.end_state = None
diff --git a/agentops/worker.py b/agentops/worker.py
index c5688701..bc930d69 100644
--- a/agentops/worker.py
+++ b/agentops/worker.py
@@ -36,8 +36,7 @@ class Worker:
"events": events
}
- serialized_payload = \
- safe_serialize(payload).encode("utf-8")
+ serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/events',
serialized_payload,
self.config.api_key)
@@ -48,8 +47,7 @@ class Worker:
payload = {
"session": session.__dict__
}
- serialized_payload = \
- json.dumps(filter_unjsonable(payload)).encode("utf-8")
+ serialized_payload = json.dumps(filter_unjsonable(payload)).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/sessions',
serialized_payload,
self.config.api_key)