Skip to content

Conversation

@viniciusdsmello
Copy link
Contributor

Pull Request

Summary

Fix duplicate traces issue in the LiteLLM tracer by making trace_litellm() idempotent.

Changes

  • Added _litellm_traced module-level flag to track if patching has been applied
  • Added guard in trace_litellm() to prevent multiple patching of litellm.completion

Context

When trace_litellm() is called multiple times (e.g., accidentally placed inside a request handler instead of at initialization), each call wraps the already-wrapped litellm.completion function, creating a chain of wrappers. This causes each LLM completion to generate multiple traces on the Openlayer platform.

Root cause: trace_litellm() captures litellm.completion as original_completion and 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
  • Unit tests
  • Postman CI/CD
  • Other (please specify)

Manual testing performed:

  1. Verified trace_litellm() can be called multiple times without error
  2. Confirmed only a single trace is generated per LLM completion after the fix
  3. Verified debug logs correctly indicate when patching is applied vs skipped

@viniciusdsmello viniciusdsmello changed the title feat: prevent multiple patching in trace_litellm function to avoid duplicate traces fix(closes OPEN-8565): prevent multiple patching in trace_litellm function to avoid duplicate traces Dec 26, 2025
@viniciusdsmello viniciusdsmello merged commit a1619e7 into main Dec 26, 2025
5 checks passed
@viniciusdsmello viniciusdsmello deleted the vini/open-8565-bug-litellm-tracer-is-duplicating-requests-when branch December 26, 2025 14:13
@stainless-app stainless-app bot mentioned this pull request Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants