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

17 params are not being sent to dashboard (#19)

Alex Reibmanb3d00d169d45
diff --git a/agentops/__init__.py b/agentops/__init__.py
index cef36373..037d98c5 100755
--- a/agentops/__init__.py
+++ b/agentops/__init__.py
@@ -1,4 +1,4 @@
from .agentops import AgentOps
-from .event import Event, EventState
+from .event import Event, EventState, SessionState
from .config import Configuration
from .logger import AgentOpsLogger
diff --git a/agentops/agentops.py b/agentops/agentops.py
index 0f3bef70..301d85eb 100644
--- a/agentops/agentops.py
+++ b/agentops/agentops.py
@@ -30,14 +30,14 @@ class AgentOps:
"""
def __init__(self, api_key: str, tags: Optional[List[str]] = None, config: Configuration = Configuration()):
- self.config: Configuration = config
- self.config.api_key = api_key
- self.start_session(tags)
-
# Store a reference to the instance
AgentOps._instance = self
atexit.register(self.cleanup)
+ self.config: Configuration = config
+ self.config.api_key = api_key
+ self.start_session(tags)
+
def record(self, event: Event):
"""
Record an event with the AgentOps service.
@@ -65,11 +65,20 @@ class AgentOps:
def wrapper(*args, **kwargs):
func_args = inspect.signature(func).parameters
arg_names = list(func_args.keys())
- arg_values = dict(zip(arg_names, args))
+ # Get default values
+ arg_values = {name: func_args[name].default
+ for name in arg_names if func_args[name].default
+ is not inspect._empty}
+ # Update with positional arguments
+ arg_values.update(dict(zip(arg_names, args)))
arg_values.update(kwargs)
try:
returns = func(*args, **kwargs)
+ # If the function returns multiple values, record them all in the same event
+ if isinstance(returns, tuple):
+ returns = list(returns)
+
# Record the event after the function call
self.record(Event(event_type=event_name,
params=arg_values,
diff --git a/agentops/worker.py b/agentops/worker.py
index f9f17b8d..8b59aa49 100644
--- a/agentops/worker.py
+++ b/agentops/worker.py
@@ -3,7 +3,7 @@ import threading
import time
from .http import HttpClient
from .config import Configuration
-from .event import Event, Session
+from .event import Session
from typing import Dict
@@ -15,8 +15,21 @@ def is_jsonable(x):
return False
-def filter_unjsonable(d):
- return {k: v for k, v in d.items() if is_jsonable(v)}
+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): return f"<<non-serializable: {type(o).__qualname__}>>"
+ return json.dumps(obj, default=default)
class Worker:
@@ -28,7 +41,7 @@ class Worker:
self.thread = threading.Thread(target=self.run)
self.thread.start()
- def add_event(self, event: str) -> None:
+ def add_event(self, event: dict) -> None:
with self.lock:
self.queue.append(event)
if len(self.queue) >= self.config.max_queue_size:
@@ -44,9 +57,10 @@ class Worker:
"events": events
}
+ serialized_payload = \
+ safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/events',
- json.dumps(filter_unjsonable(
- payload)).encode("utf-8"),
+ serialized_payload,
self.config.api_key)
def start_session(self, session: Session) -> None:
@@ -54,10 +68,11 @@ class Worker:
payload = {
"session": session.__dict__
}
-
+ serialized_payload = \
+ json.dumps(filter_unjsonable(payload)).encode("utf-8")
+ print(f"{serialized_payload=}")
HttpClient.post(f'{self.config.endpoint}/sessions',
- json.dumps(filter_unjsonable(
- payload)).encode("utf-8"),
+ serialized_payload,
self.config.api_key)
def end_session(self, session: Session) -> None:
diff --git a/tests/test_session.py b/tests/test_session.py
index d0b79714..dd539f03 100644
--- a/tests/test_session.py
+++ b/tests/test_session.py
@@ -2,8 +2,7 @@ import pytest
import requests_mock
import time
-from agentops import AgentOps, Event, Configuration
-from agentops.event import EventState
+from agentops import AgentOps, Event, Configuration, EventState
@pytest.fixture
@@ -97,31 +96,31 @@ class TestRecordAction:
def test_record_action_decorator(self, mock_req):
@self.client.record_action(event_name=self.event_type, tags=['foo', 'bar'])
- def dummy_func(x, y):
+ def add_two(x, y):
return x + y
# Act
- dummy_func(3, 4)
+ add_two(3, 4)
+ time.sleep(0.1)
# Assert
assert len(mock_req.request_history) == 1
assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key
request_json = mock_req.last_request.json()
- assert request_json['event']['event_type'] == self.event_type
- assert request_json['event']['params'] == {
- 'args': [3, 4], 'kwargs': {}}
- assert request_json['event']['returns'] == 7
- assert request_json['event']['result'] == EventState.SUCCESS
- assert request_json['event']['tags'] == ['foo', 'bar']
+ assert request_json['events'][0]['event_type'] == self.event_type
+ assert request_json['events'][0]['params'] == {'x': 3, 'y': 4}
+ assert request_json['events'][0]['returns'] == 7
+ assert request_json['events'][0]['result'] == EventState.SUCCESS
+ assert request_json['events'][0]['tags'] == ['foo', 'bar']
- def test_record_action_decorator(self, mock_req):
+ def test_record_action_decorator_multiple(self, mock_req):
# Arrange
@self.client.record_action(event_name=self.event_type, tags=['foo', 'bar'])
- def dummy_func(x, y):
- return x + y
+ def add_three(x, y, z=3):
+ return x + y + z
# Act
- dummy_func(3, 4)
+ add_three(1, 2)
time.sleep(0.1)
# Assert
@@ -129,7 +128,7 @@ class TestRecordAction:
assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key
request_json = mock_req.last_request.json()
assert request_json['events'][0]['event_type'] == self.event_type
- assert request_json['events'][0]['params'] == {'x': 3, 'y': 4}
- assert request_json['events'][0]['returns'] == 7
+ assert request_json['events'][0]['params'] == {'x': 1, 'y': 2, 'z': 3}
+ assert request_json['events'][0]['returns'] == 6
assert request_json['events'][0]['result'] == EventState.SUCCESS
assert request_json['events'][0]['tags'] == ['foo', 'bar']