fix(closes OPEN-8565): prevent multiple patching in trace_litellm function to avoid duplicate traces #568
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request
Summary
Fix duplicate traces issue in the LiteLLM tracer by making
trace_litellm()idempotent.Changes
_litellm_tracedmodule-level flag to track if patching has been appliedtrace_litellm()to prevent multiple patching oflitellm.completionContext
When
trace_litellm()is called multiple times (e.g., accidentally placed inside a request handler instead of at initialization), each call wraps the already-wrappedlitellm.completionfunction, creating a chain of wrappers. This causes each LLM completion to generate multiple traces on the Openlayer platform.Root cause:
trace_litellm()captureslitellm.completionasoriginal_completionand replaces it with a traced version. On subsequent calls, the "original" is actually the previously wrapped version, leading to nested tracing.Solution: Add a module-level flag that ensures the patching only happens once, regardless of how many times
trace_litellm()is called.Testing
Manual testing performed:
trace_litellm()can be called multiple times without error