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

initializer error (#123)

Braelyn Boynton1f7979ac65f0
diff --git a/agentops/agent.py b/agentops/agent.py
index 043c81e6..2a0a005c 100644
--- a/agentops/agent.py
+++ b/agentops/agent.py
@@ -1,3 +1,4 @@
+import logging
from uuid import uuid4
from agentops import Client
from inspect import isclass, isfunction
@@ -12,17 +13,20 @@ def track_agent(name: str | None = None):
original_init = obj.__init__
def new_init(self, *args, **kwargs):
- original_init(self, *args, **kwargs)
- self.agent_ops_agent_id = uuid4()
- ao_client = Client()
- ao_client.create_agent(self.agent_ops_agent_id, self.agent_ops_agent_name)
+ try:
+ original_init(self, *args, **kwargs)
+ self.agent_ops_agent_id = uuid4()
+ Client().create_agent(self.agent_ops_agent_id, self.agent_ops_agent_name)
+ except AttributeError as e:
+ logging.error("AgentOps failed to track an agent. This often happens if agentops.init() was not "
+ "called before initializing an agent with the @track_agent decorator.")
+ raise e
obj.__init__ = new_init
elif isfunction(obj):
obj.agent_ops_agent_id = uuid4()
- ao_client = Client()
- ao_client.create_agent(obj.agent_ops_agent_id, obj.agent_ops_agent_name)
+ Client().create_agent(obj.agent_ops_agent_id, obj.agent_ops_agent_name)
else:
raise Exception("Invalid input, 'obj' must be a class or a function")
diff --git a/agentops/client.py b/agentops/client.py
index 1d1a8858..2b5005e7 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -66,7 +66,7 @@ class Client(metaclass=MetaClient):
api_key = environ.get("AGENTOPS_API_KEY")
if not api_key:
logging.warning("AgentOps: No API key provided - no data will be recorded.")
- return
+ return
if not parent_key:
parent_key = environ.get('AGENTOPS_PARENT_KEY', None)