Skip to content
58 changes: 45 additions & 13 deletions agentops/llms/tracker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import sys
from importlib import import_module
from importlib.metadata import version
Expand Down Expand Up @@ -99,39 +100,70 @@

def __init__(self, client):
self.client = client
self.litellm_initialized = False

def _is_litellm_call(self):
"""
Detects if the API call originated from LiteLLM.
Returns True if LiteLLM appears in the call stack **before** OpenAI.
"""
stack = inspect.stack()

Check warning on line 110 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L110

Added line #L110 was not covered by tests

litellm_seen = False # Track if LiteLLM was encountered
openai_seen = False # Track if OpenAI was encountered

Check warning on line 113 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L112-L113

Added lines #L112 - L113 were not covered by tests

for frame in stack:
module = inspect.getmodule(frame.frame)

Check warning on line 116 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L115-L116

Added lines #L115 - L116 were not covered by tests

module_name = module.__name__ if module else None

Check warning on line 118 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L118

Added line #L118 was not covered by tests

filename = frame.filename.lower()

Check warning on line 120 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L120

Added line #L120 was not covered by tests

if module_name and "litellm" in module_name or "litellm" in filename:
litellm_seen = True

Check warning on line 123 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L122-L123

Added lines #L122 - L123 were not covered by tests

if module_name and "openai" in module_name or "openai" in filename:
openai_seen = True

Check warning on line 126 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L125-L126

Added lines #L125 - L126 were not covered by tests

if not litellm_seen:
return False

Check warning on line 129 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L128-L129

Added lines #L128 - L129 were not covered by tests

return litellm_seen

Check warning on line 131 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L131

Added line #L131 was not covered by tests

def override_api(self):
"""
Overrides key methods of the specified API to record events.
"""

for api in self.SUPPORTED_APIS:
if api in sys.modules:
module = import_module(api)

if api == "litellm":
module_version = version(api)
if module_version is None:
logger.warning("Cannot determine LiteLLM version. Only LiteLLM>=1.3.1 supported.")

if Version(module_version) >= parse("1.3.1"):
provider = LiteLLMProvider(self.client)
provider.override()
self.litellm_initialized = True

Check warning on line 149 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L149

Added line #L149 was not covered by tests
else:
logger.warning(f"Only LiteLLM>=1.3.1 supported. v{module_version} found.")
return # If using an abstraction like litellm, do not patch the underlying LLM APIs

if api == "openai":
# Patch openai v1.0.0+ methods
if hasattr(module, "__version__"):
module_version = parse(module.__version__)
if module_version >= parse("1.0.0"):
provider = OpenAiProvider(self.client)
provider.override()
else:
raise DeprecationWarning(
"OpenAI versions < 0.1 are no longer supported by AgentOps. Please upgrade OpenAI or "
"downgrade AgentOps to <=0.3.8."
)
# Ensure OpenAI is only initialized if it was NOT called inside LiteLLM
if not self._is_litellm_call():
if hasattr(module, "__version__"):
module_version = parse(module.__version__)
if module_version >= parse("1.0.0"):
provider = OpenAiProvider(self.client)
provider.override()

Check warning on line 161 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L156-L161

Added lines #L156 - L161 were not covered by tests
else:
raise DeprecationWarning(

Check warning on line 163 in agentops/llms/tracker.py

View check run for this annotation

Codecov / codecov/patch

agentops/llms/tracker.py#L163

Added line #L163 was not covered by tests
"OpenAI versions < 0.1 are no longer supported by AgentOps. Please upgrade OpenAI or "
"downgrade AgentOps to <=0.3.8."
)

if api == "cohere":
# Patch cohere v5.4.0+ methods
Expand Down
Loading