Skip to content
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

Fixed issue #8246 #8250

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions litellm/litellm_core_utils/logging_callback_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ def add_litellm_async_failure_callback(
callback=callback, parent_list=litellm._async_failure_callback
)

def remove_callback_from_list_by_object(
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the existing tests, I believe a more suitable location for the unit test for remove_callback_from_list_by_object is https://github.com/BerriAI/litellm/blob/main/tests/litellm_utils_tests/test_logging_callback_manager.py

self, callback_list, obj
):
"""
Remove callbacks that are methods of a particular object (e.g., router cleanup)
"""
if not isinstance(callback_list, list): # Not list -> do nothing
return

remove_list=[c for c in callback_list if hasattr(c, '__self__') and c.__self__ == obj]

for c in remove_list:
callback_list.remove(c)


def _add_string_callback_to_list(
self, callback: str, parent_list: List[Union[CustomLogger, Callable, str]]
):
Expand Down
4 changes: 3 additions & 1 deletion litellm/proxy/route_llm_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ async def route_request(
elif "user_config" in data:
router_config = data.pop("user_config")
user_router = litellm.Router(**router_config)
return getattr(user_router, f"{route_type}")(**data)
ret_val = getattr(user_router, f"{route_type}")(**data)
user_router.discard()
return ret_val

elif (
route_type == "acompletion"
Expand Down
14 changes: 14 additions & 0 deletions litellm/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,20 @@ def __init__( # noqa: PLR0915
litellm.amoderation, call_type="moderation"
)

def discard(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

"""
Pseudo-destructor to be invoked to clean up global data structures when router is no longer used.
For now, unhook router's callbacks from all lists
"""
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm._async_success_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.success_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm._async_failure_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.failure_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.input_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.service_callback, self)
litellm.logging_callback_manager.remove_callback_from_list_by_object(litellm.callbacks, self)


def initialize_assistants_endpoint(self):
## INITIALIZE PASS THROUGH ASSISTANTS ENDPOINT ##
self.acreate_assistants = self.factory_function(litellm.acreate_assistants)
Expand Down