-
Notifications
You must be signed in to change notification settings - Fork 569
fix(litellm): Guard against module shadowing #5249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Description
Use explicit imports to prevent
AttributeErrorwhenlitellmis shadowed.Issues
Follows up on #5140.
Reminders
tox -e linters.feat:,fix:,ref:,meta:)