Skip to content

Commit c75f105

Browse files
feat: prevent multiple patching in trace_litellm function to avoid duplicate traces
1 parent 43a7d2e commit c75f105

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/openlayer/lib/integrations/litellm_tracer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22+
# Flag to prevent multiple patching
23+
_litellm_traced = False
24+
2225

2326
def trace_litellm() -> None:
2427
"""Patch the litellm.completion function to trace completions.
@@ -57,11 +60,18 @@ def trace_litellm() -> None:
5760
... inference_id="custom-id-123" # Optional Openlayer parameter
5861
... )
5962
"""
63+
global _litellm_traced
64+
6065
if not HAVE_LITELLM:
6166
raise ImportError(
6267
"LiteLLM library is not installed. Please install it with: pip install litellm"
6368
)
6469

70+
# Prevent multiple patching - this avoids duplicate traces
71+
if _litellm_traced:
72+
logger.debug("trace_litellm() already called - skipping to prevent duplicate traces")
73+
return
74+
6575
original_completion = litellm.completion
6676

6777
@wraps(original_completion)
@@ -84,6 +94,8 @@ def traced_completion(*args, **kwargs):
8494
)
8595

8696
litellm.completion = traced_completion
97+
_litellm_traced = True
98+
logger.debug("litellm.completion has been patched for Openlayer tracing")
8799

88100

89101
def handle_streaming_completion(

0 commit comments

Comments
 (0)