Skip to content

Conversation

@alexander-alderman-webb
Copy link
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Dec 17, 2025

Description

Use explicit imports to prevent AttributeError when litellm is shadowed.

Issues

Follows up on #5140.

Reminders

@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner December 17, 2025 13:18
@alexander-alderman-webb alexander-alderman-webb changed the title fix(litellm): Prevent module shadowing fix(litellm): Guard against module shadowing Dec 17, 2025
@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft December 17, 2025 13:41
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review December 18, 2025 08:52
@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft December 18, 2025 08:55
Comment on lines +282 to 292
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)
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.

@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review December 18, 2025 09:48
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.

2 participants