Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions sentry_sdk/integrations/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

try:
import litellm # type: ignore[import-not-found]
from litellm import input_callback, success_callback, failure_callback
except ImportError:
raise DidNotEnable("LiteLLM not installed")

Expand Down Expand Up @@ -278,14 +279,14 @@ def __init__(self: "LiteLLMIntegration", include_prompts: bool = True) -> None:
@staticmethod
def setup_once() -> None:
"""Set up LiteLLM callbacks for monitoring."""
litellm.input_callback = litellm.input_callback or []
litellm.input_callback = input_callback or []
if _input_callback not in litellm.input_callback:
litellm.input_callback.append(_input_callback)

litellm.success_callback = litellm.success_callback or []
litellm.success_callback = success_callback or []
if _success_callback not in litellm.success_callback:
litellm.success_callback.append(_success_callback)

litellm.failure_callback = litellm.failure_callback or []
litellm.failure_callback = failure_callback or []
if _failure_callback not in litellm.failure_callback:
litellm.failure_callback.append(_failure_callback)
Comment on lines +282 to 292
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: User-configured LiteLLM callbacks are overwritten during Sentry initialization if they are set before sentry_sdk.init() is called, due to using stale, import-time references.
Severity: MEDIUM | Confidence: High

🔍 Detailed Analysis

The change imports input_callback, success_callback, and failure_callback from litellm at the module level, capturing their state at import time. If a user sets their own litellm callbacks after the import but before sentry_sdk.init() is called, the setup_once function will use the stale, import-time values (empty lists) to re-assign the callbacks. This overwrites and silently discards the user's pre-configured callbacks, preventing them from running. This behavior is a regression from the previous implementation which read the current callback values.

💡 Suggested Fix

In setup_once, instead of using the variables imported at the module level, access the callbacks directly from the litellm module to get their current values. For example, use litellm.input_callback = litellm.input_callback or [] to preserve any user-set callbacks.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: sentry_sdk/integrations/litellm.py#L279-L292

Potential issue: The change imports `input_callback`, `success_callback`, and
`failure_callback` from `litellm` at the module level, capturing their state at import
time. If a user sets their own `litellm` callbacks after the import but before
`sentry_sdk.init()` is called, the `setup_once` function will use the stale, import-time
values (empty lists) to re-assign the callbacks. This overwrites and silently discards
the user's pre-configured callbacks, preventing them from running. This behavior is a
regression from the previous implementation which read the current callback values.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 7692173

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair point, but by design sentry_sdk.init() should be initialized first.

1 change: 0 additions & 1 deletion tests/test_shadowed_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def pytest_generate_tests(metafunc):
- {
"clickhouse_driver",
"grpc",
"litellm",
"opentelemetry",
"pure_eval",
"ray",
Expand Down
Loading