Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/openlayer/lib/integrations/litellm_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

logger = logging.getLogger(__name__)

# Flag to prevent multiple patching
_litellm_traced = False


def trace_litellm() -> None:
"""Patch the litellm.completion function to trace completions.
Expand Down Expand Up @@ -57,11 +60,18 @@ def trace_litellm() -> None:
... inference_id="custom-id-123" # Optional Openlayer parameter
... )
"""
global _litellm_traced

if not HAVE_LITELLM:
raise ImportError(
"LiteLLM library is not installed. Please install it with: pip install litellm"
)

# Prevent multiple patching - this avoids duplicate traces
if _litellm_traced:
logger.debug("trace_litellm() already called - skipping to prevent duplicate traces")
return

original_completion = litellm.completion

@wraps(original_completion)
Expand All @@ -84,6 +94,8 @@ def traced_completion(*args, **kwargs):
)

litellm.completion = traced_completion
_litellm_traced = True
logger.debug("litellm.completion has been patched for Openlayer tracing")


def handle_streaming_completion(
Expand Down