Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Litellm dev 02 10 2025 p2 (#8443)

Krish Dholakiae26d7df91b14
diff --git a/litellm/litellm_core_utils/logging_callback_manager.py b/litellm/litellm_core_utils/logging_callback_manager.py
index 860a57c5f62..e55df444746 100644
--- a/litellm/litellm_core_utils/logging_callback_manager.py
+++ b/litellm/litellm_core_utils/logging_callback_manager.py
@@ -85,6 +85,21 @@ class LoggingCallbackManager:
callback=callback, parent_list=litellm._async_failure_callback
)
+ def remove_callback_from_list_by_object(
+ 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]]
):
diff --git a/litellm/llms/openai/openai.py b/litellm/llms/openai/openai.py
index 82b9c9ba384..5465a249458 100644
--- a/litellm/llms/openai/openai.py
+++ b/litellm/llms/openai/openai.py
@@ -27,6 +27,7 @@ from typing_extensions import overload
import litellm
from litellm import LlmProviders
from litellm._logging import verbose_logger
+from litellm.constants import DEFAULT_MAX_RETRIES
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.litellm_core_utils.logging_utils import track_llm_api_timing
from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator
@@ -320,6 +321,17 @@ class OpenAIChatCompletion(BaseLLM):
def __init__(self) -> None:
super().__init__()
+ def _set_dynamic_params_on_client(
+ self,
+ client: Union[OpenAI, AsyncOpenAI],
+ organization: Optional[str] = None,
+ max_retries: Optional[int] = None,
+ ):
+ if organization is not None:
+ client.organization = organization
+ if max_retries is not None:
+ client.max_retries = max_retries
+
def _get_openai_client(
self,
is_async: bool,
@@ -327,11 +339,10 @@ class OpenAIChatCompletion(BaseLLM):
api_base: Optional[str] = None,
api_version: Optional[str] = None,
timeout: Union[float, httpx.Timeout] = httpx.Timeout(None),
- max_retries: Optional[int] = 2,
+ max_retries: Optional[int] = DEFAULT_MAX_RETRIES,
organization: Optional[str] = None,
client: Optional[Union[OpenAI, AsyncOpenAI]] = None,
):
- args = locals()
if client is None:
if not isinstance(max_retries, int):
raise OpenAIError(
@@ -364,7 +375,6 @@ class OpenAIChatCompletion(BaseLLM):
organization=organization,
)
else:
-
_new_client = OpenAI(
api_key=api_key,
base_url=api_base,
@@ -383,6 +393,11 @@ class OpenAIChatCompletion(BaseLLM):
return _new_client
else:
+ self._set_dynamic_params_on_client(
+ client=client,
+ organization=organization,
+ max_retries=max_retries,
+ )
return client
@track_llm_api_timing()
diff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.html
deleted file mode 100644
index 94a8d6d50cd..00000000000
--- a/litellm/proxy/_experimental/out/onboarding.html
+++ /dev/null
@@ -1 +0,0 @@
-<!DOCTYPE html><html id="__next_error__"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="preload" as="script" fetchPriority="low" href="/ui/_next/static/chunks/webpack-75a5453f51d60261.js"/><script src="/ui/_next/static/chunks/fd9d1056-f8f374bd771def60.js" async=""></script><script src="/ui/_next/static/chunks/117-2d8e84979f319d39.js" async=""></script><script src="/ui/_next/static/chunks/main-app-4f7318ae681a6d94.js" async=""></script><title>LiteLLM Dashboard</title><meta name="description" content="LiteLLM Proxy Admin UI"/><link rel="icon" href="/ui/favicon.ico" type="image/x-icon" sizes="16x16"/><meta name="next-size-adjust"/><script src="/ui/_next/static/chunks/polyfills-42372ed130431b0a.js" noModule=""></script></head><body><script src="/ui/_next/static/chunks/webpack-75a5453f51d60261.js" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0]);self.__next_f.push([2,null])</script><script>self.__next_f.push([1,"1:HL[\"/ui/_next/static/media/a34f9d1faa5f3315-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n2:HL[\"/ui/_next/static/css/86f6cc749f6b8493.css\",\"style\"]\n3:HL[\"/ui/_next/static/css/17e707d690fab559.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-6bdd9c4659caabcd.js\",\"42\",\"static/chunks/42-85111ce0453ddfcd.js\",\"755\",\"static/chunks/755-288e79da1358e7df.js\",\"250\",\"static/chunks/250-27e715296ed15b72.js\",\"461\",\"static/chunks/app/onboarding/page-75a34f61b763c6f1.js\"],\"default\",1]\n8:I[4707,[],\"\"]\n9:I[36423,[],\"\"]\nb:I[61060,[],\"\"]\nc:[]\n"])</script><script>self.__next_f.push([1,"0:[\"$\",\"$L4\",null,{\"buildId\":\"lRS2K8eFHFy8OBiXYJUp-\",\"assetPrefix\":\"/ui\",\"urlParts\":[\"\",\"onboarding\"],\"initialTree\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{}]}]},\"$undefined\",\"$undefined\",true],\"initialSeedData\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{},[[\"$L5\",[\"$\",\"$L6\",null,{\"props\":{\"params\":{},\"searchParams\":{}},\"Component\":\"$7\"}],null],null],null]},[null,[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"onboarding\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\"}]],null]},[[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/ui/_next/static/css/86f6cc749f6b8493.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/ui/_next/static/css/17e707d690fab559.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[\"$\",\"body\",null,{\"className\":\"__className_cf7686\",\"children\":[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}],[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"system-ui,\\\"Segoe UI\\\",Roboto,Helvetica,Arial,sans-serif,\\\"Apple Color Emoji\\\",\\\"Segoe UI Emoji\\\"\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}\"}}],[\"$\",\"h1\",null,{\"className\":\"next-error-h1\",\"style\":{\"display\":\"inline-block\",\"margin\":\"0 20px 0 0\",\"padding\":\"0 23px 0 0\",\"fontSize\":24,\"fontWeight\":500,\"verticalAlign\":\"top\",\"lineHeight\":\"49px\"},\"children\":\"404\"}],[\"$\",\"div\",null,{\"style\":{\"display\":\"inline-block\"},\"children\":[\"$\",\"h2\",null,{\"style\":{\"fontSize\":14,\"fontWeight\":400,\"lineHeight\":\"49px\",\"margin\":0},\"children\":\"This page could not be found.\"}]}]]}]}]],\"notFoundStyles\":[]}]}]}]],null],null],\"couldBeIntercepted\":false,\"initialHead\":[null,\"$La\"],\"globalErrorComponent\":\"$b\",\"missingSlots\":\"$Wc\"}]\n"])</script><script>self.__next_f.push([1,"a:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}],[\"$\",\"meta\",\"1\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"2\",{\"children\":\"LiteLLM Dashboard\"}],[\"$\",\"meta\",\"3\",{\"name\":\"description\",\"content\":\"LiteLLM Proxy Admin UI\"}],[\"$\",\"link\",\"4\",{\"rel\":\"icon\",\"href\":\"/ui/favicon.ico\",\"type\":\"image/x-icon\",\"sizes\":\"16x16\"}],[\"$\",\"meta\",\"5\",{\"name\":\"next-size-adjust\"}]]\n5:null\n"])</script></body></html>
\ No newline at end of file
diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yaml
index 19cc8cdd070..5347d4a791b 100644
--- a/litellm/proxy/_new_secret_config.yaml
+++ b/litellm/proxy/_new_secret_config.yaml
@@ -39,6 +39,7 @@ litellm_settings:
general_settings:
enable_jwt_auth: True
+ forward_openai_org_id: True
litellm_jwtauth:
user_id_jwt_field: "sub"
team_ids_jwt_field: "groups"
diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py
index b913c238dbb..5892b7afc62 100644
--- a/litellm/proxy/litellm_pre_call_utils.py
+++ b/litellm/proxy/litellm_pre_call_utils.py
@@ -238,6 +238,7 @@ class LiteLLMProxyRequestSetup:
return None
for header, value in headers.items():
if header.lower() == "openai-organization":
+ verbose_logger.info(f"found openai org id: {value}, sending to llm")
return value
return None
diff --git a/litellm/proxy/route_llm_request.py b/litellm/proxy/route_llm_request.py
index 1ff211c20cc..6102a26b232 100644
--- a/litellm/proxy/route_llm_request.py
+++ b/litellm/proxy/route_llm_request.py
@@ -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"
diff --git a/litellm/router.py b/litellm/router.py
index 597ba9fd06a..bdac540f1ab 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -573,6 +573,21 @@ class Router:
litellm.amoderation, call_type="moderation"
)
+
+ def discard(self):
+ """
+ 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 _update_redis_cache(self, cache: RedisCache):
"""
Update the redis cache for the router, if none set.
@@ -587,6 +602,7 @@ class Router:
if self.cache.redis_cache is None:
self.cache.redis_cache = cache
+
def initialize_assistants_endpoint(self):
## INITIALIZE PASS THROUGH ASSISTANTS ENDPOINT ##
self.acreate_assistants = self.factory_function(litellm.acreate_assistants)
diff --git a/tests/litellm_utils_tests/test_logging_callback_manager.py b/tests/litellm_utils_tests/test_logging_callback_manager.py
index 71ffb186781..1b70631e4d1 100644
--- a/tests/litellm_utils_tests/test_logging_callback_manager.py
+++ b/tests/litellm_utils_tests/test_logging_callback_manager.py
@@ -160,6 +160,39 @@ def test_async_callbacks():
assert async_failure in litellm._async_failure_callback
+def test_remove_callback_from_list_by_object():
+ manager = LoggingCallbackManager()
+ # Reset all callbacks
+ manager._reset_all_callbacks()
+
+ def TestObject():
+ def __init__(self):
+ manager.add_litellm_callback(self.callback)
+ manager.add_litellm_success_callback(self.callback)
+ manager.add_litellm_failure_callback(self.callback)
+ manager.add_litellm_async_success_callback(self.callback)
+ manager.add_litellm_async_failure_callback(self.callback)
+
+ def callback(self):
+ pass
+
+ obj = TestObject()
+
+ manager.remove_callback_from_list_by_object(litellm.callbacks, obj)
+ manager.remove_callback_from_list_by_object(litellm.success_callback, obj)
+ manager.remove_callback_from_list_by_object(litellm.failure_callback, obj)
+ manager.remove_callback_from_list_by_object(litellm._async_success_callback, obj)
+ manager.remove_callback_from_list_by_object(litellm._async_failure_callback, obj)
+
+ # Verify all callback lists are empty
+ assert len(litellm.callbacks) == 0
+ assert len(litellm.success_callback) == 0
+ assert len(litellm.failure_callback) == 0
+ assert len(litellm._async_success_callback) == 0
+ assert len(litellm._async_failure_callback) == 0
+
+
+
def test_reset_callbacks(callback_manager):
# Add various callbacks
callback_manager.add_litellm_callback("test")
diff --git a/tests/router_unit_tests/test_router_helper_utils.py b/tests/router_unit_tests/test_router_helper_utils.py
index e02b47ec365..f12371baebc 100644
--- a/tests/router_unit_tests/test_router_helper_utils.py
+++ b/tests/router_unit_tests/test_router_helper_utils.py
@@ -918,6 +918,31 @@ def test_flush_cache(model_list):
assert router.cache.get_cache("test") is None
+def test_discard(model_list):
+ """
+ Test that discard properly removes a Router from the callback lists
+ """
+ litellm.callbacks = []
+ litellm.success_callback = []
+ litellm._async_success_callback = []
+ litellm.failure_callback = []
+ litellm._async_failure_callback = []
+ litellm.input_callback = []
+ litellm.service_callback = []
+
+ router = Router(model_list=model_list)
+ router.discard()
+
+ # Verify all callback lists are empty
+ assert len(litellm.callbacks) == 0
+ assert len(litellm.success_callback) == 0
+ assert len(litellm.failure_callback) == 0
+ assert len(litellm._async_success_callback) == 0
+ assert len(litellm._async_failure_callback) == 0
+ assert len(litellm.input_callback) == 0
+ assert len(litellm.service_callback) == 0
+
+
def test_initialize_assistants_endpoint(model_list):
"""Test if the 'initialize_assistants_endpoint' function is working correctly"""
router = Router(model_list=model_list)