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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
build: merge commit 1b15568af7f8923a014e6e49f5ec09e5abf5f981 Author: Krrish Dholakia <[email protected]> Date: Mon Feb 17 21:37:36 2025 -0800
Krrish Dholakia7bfd816d3bb2
diff --git a/docs/my-website/docs/proxy/token_auth.md b/docs/my-website/docs/proxy/token_auth.mdindex 672d62ec918..c6d280cb822 100644--- a/docs/my-website/docs/proxy/token_auth.md+++ b/docs/my-website/docs/proxy/token_auth.md@@ -207,6 +207,64 @@ OIDC Auth for API: [**See Walkthrough**](https://www.loom.com/share/00fe2deab59a- If all checks pass, allow the request+## Advanced - Custom Validate++Validate a JWT Token using custom logic, if you need an extra way to verify if tokens are valid for LiteLLM Proxy.++### 1. Setup custom validate function++```python+from typing import Literal++def my_custom_validate(token: str) -> Literal[True]:+ """+ Only allow tokens with tenant-id == "my-unique-tenant", and claims == ["proxy-admin"]+ """+ allowed_tenants = ["my-unique-tenant"]+ allowed_claims = ["proxy-admin"]++ if token["tenant_id"] not in allowed_tenants:+ raise Exception("Invalid JWT token")+ if token["claims"] not in allowed_claims:+ raise Exception("Invalid JWT token")+ return True+```++### 2. Setup config.yaml++```yaml+general_settings:+ master_key: sk-1234+ enable_jwt_auth: True+ litellm_jwtauth:+ user_id_jwt_field: "sub"+ team_id_jwt_field: "tenant_id"+ user_id_upsert: True+ custom_validate: custom_validate.my_custom_validate # 👈 custom validate function+```++### 3. Test the flow++**Expected JWT**++```+{+ "sub": "my-unique-user",+ "tenant_id": "INVALID_TENANT",+ "claims": ["proxy-admin"]+}+```++**Expected Response**++```+{+ "error": "Invalid JWT token"+}+```+++## Advanced - Allowed RoutesConfigure which routes a JWT can access via the config.diff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.htmldeleted file mode 100644index 1e070dcc422..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-524b80e1a6b8bb06.js" async=""></script><script src="/ui/_next/static/chunks/117-883150efc583d711.js" async=""></script><script src="/ui/_next/static/chunks/main-app-475d6efe4080647d.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/c43d141ac9732358.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-0b72c78cfebbd712.js\",\"42\",\"static/chunks/42-1cbed529ecb084e0.js\",\"755\",\"static/chunks/755-8a74d7ff30e7d680.js\",\"250\",\"static/chunks/250-f1e2db7aa40a2982.js\",\"461\",\"static/chunks/app/onboarding/page-0992dd0b4cd99a23.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\":\"yJuobVnL1vdr5eQA0jUcl\",\"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/c43d141ac9732358.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 filediff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex b59a0a57b50..f6c8415021c 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -1,5 +1,15 @@model_list:- model_name: azure-gpt-35-turbolitellm_params:- model: topaz/chatgpt-v-2- api_key: os.environ/AZURE_API_KEY\ No newline at end of file+ model: azure/chatgpt-v-2+ api_key: os.environ/AZURE_API_KEY+ api_base: os.environ/AZURE_API_BASE++general_settings:+ master_key: sk-1234+ enable_jwt_auth: True+ litellm_jwtauth:+ user_id_jwt_field: "sub"+ team_id_jwt_field: "client_id"+ user_id_upsert: True+ custom_validate: custom_validate.my_custom_validatediff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.pyindex dbf5270e59d..a1dc0c27c53 100644--- a/litellm/proxy/_types.py+++ b/litellm/proxy/_types.py@@ -2,7 +2,7 @@ import enumimport jsonimport uuidfrom datetime import datetime-from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Unionimport httpxfrom pydantic import (@@ -30,6 +30,8 @@ from litellm.types.utils import (TextCompletionResponse,)+from .types_utils.utils import get_instance_fn, validate_custom_validate_return_type+if TYPE_CHECKING:from opentelemetry.trace import Span as _Span@@ -2395,6 +2397,7 @@ class LiteLLM_JWTAuth(LiteLLMPydanticObjectBase):- public_key_ttl: Default - 600s. TTL for caching public JWT keys.- public_allowed_routes: list of allowed routes for authenticated but unknown litellm role jwt tokens.- enforce_rbac: If true, enforce RBAC for all routes.+ - custom_validate: A custom function to validates the JWT token.See `auth_checks.py` for the specific routes"""@@ -2439,6 +2442,7 @@ class LiteLLM_JWTAuth(LiteLLMPydanticObjectBase):scope_mappings: Optional[List[ScopeMapping]] = Noneenforce_scope_based_access: bool = Falseenforce_team_based_model_access: bool = False+ custom_validate: Optional[Callable[..., Literal[True]]] = Nonedef __init__(self, **kwargs: Any) -> None:# get the attribute names for this Pydantic model@@ -2451,6 +2455,12 @@ class LiteLLM_JWTAuth(LiteLLMPydanticObjectBase):role_mappings = kwargs.get("role_mappings")scope_mappings = kwargs.get("scope_mappings")enforce_scope_based_access = kwargs.get("enforce_scope_based_access")+ custom_validate = kwargs.get("custom_validate")++ if custom_validate is not None:+ fn = get_instance_fn(custom_validate)+ validate_custom_validate_return_type(fn)+ kwargs["custom_validate"] = fnif invalid_keys:raise ValueError(diff --git a/litellm/proxy/auth/handle_jwt.py b/litellm/proxy/auth/handle_jwt.pyindex 88a8144b554..29f4b31f9cd 100644--- a/litellm/proxy/auth/handle_jwt.py+++ b/litellm/proxy/auth/handle_jwt.py@@ -862,6 +862,14 @@ class JWTAuthManager:"""Main authentication and authorization builder"""jwt_valid_token: dict = await jwt_handler.auth_jwt(token=api_key)+ # Check custom validate+ if jwt_handler.litellm_jwtauth.custom_validate:+ if not jwt_handler.litellm_jwtauth.custom_validate(jwt_valid_token):+ raise HTTPException(+ status_code=403,+ detail="Invalid JWT token",+ )+# Check RBACrbac_role = jwt_handler.get_rbac_role(token=jwt_valid_token)await JWTAuthManager.check_rbac_role(diff --git a/litellm/proxy/common_utils/callback_utils.py b/litellm/proxy/common_utils/callback_utils.pyindex 9859b340573..2280e72e9b8 100644--- a/litellm/proxy/common_utils/callback_utils.py+++ b/litellm/proxy/common_utils/callback_utils.py@@ -4,7 +4,7 @@ import litellmfrom litellm import get_secretfrom litellm._logging import verbose_proxy_loggerfrom litellm.proxy._types import CommonProxyErrors, LiteLLMPromptInjectionParams-from litellm.proxy.utils import get_instance_fn+from litellm.proxy.types_utils.utils import get_instance_fnblue_color_code = "\033[94m"reset_color_code = "\033[0m"diff --git a/litellm/proxy/custom_validate.py b/litellm/proxy/custom_validate.pynew file mode 100644index 00000000000..c80ffa4fe95--- /dev/null+++ b/litellm/proxy/custom_validate.py@@ -0,0 +1,5 @@+from typing import Literal+++def my_custom_validate(token: str) -> Literal[True]:+ raise Exception("Custom validate failed")diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.pyindex 970af05f6db..c3257e47a1b 100644--- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py+++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py@@ -609,7 +609,7 @@ def create_pass_through_route(# check if target is an adapter.py or a urlimport uuid- from litellm.proxy.utils import get_instance_fn+ from litellm.proxy.types_utils.utils import get_instance_fntry:if isinstance(target, CustomLogger):diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 421a588e390..b5db78ef880 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -238,6 +238,7 @@ from litellm.proxy.spend_tracking.spend_management_endpoints import (router as spend_management_router,)from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload+from litellm.proxy.types_utils.utils import get_instance_fnfrom litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints import (router as ui_crud_endpoints_router,)@@ -251,7 +252,6 @@ from litellm.proxy.utils import (_is_projected_spend_over_limit,_is_valid_team_configs,get_error_message_str,- get_instance_fn,hash_token,update_spend,)diff --git a/litellm/proxy/types_utils/README.md b/litellm/proxy/types_utils/README.mdnew file mode 100644index 00000000000..1d14cf513ba--- /dev/null+++ b/litellm/proxy/types_utils/README.md@@ -0,0 +1 @@+Utility functions for proxy types.py\ No newline at end of filediff --git a/litellm/proxy/types_utils/utils.py b/litellm/proxy/types_utils/utils.pynew file mode 100644index 00000000000..788849b3d5c--- /dev/null+++ b/litellm/proxy/types_utils/utils.py@@ -0,0 +1,64 @@+import importlib+import os+from typing import Any, Callable, Literal, Optional, get_type_hints+++def get_instance_fn(value: str, config_file_path: Optional[str] = None) -> Any:+ module_name = value+ instance_name = None+ try:+ # Split the path by dots to separate module from instance+ parts = value.split(".")++ # The module path is all but the last part, and the instance_name is the last part+ module_name = ".".join(parts[:-1])+ instance_name = parts[-1]++ # If config_file_path is provided, use it to determine the module spec and load the module+ if config_file_path is not None:+ directory = os.path.dirname(config_file_path)+ module_file_path = os.path.join(directory, *module_name.split("."))+ module_file_path += ".py"++ spec = importlib.util.spec_from_file_location(module_name, module_file_path) # type: ignore+ if spec is None:+ raise ImportError(+ f"Could not find a module specification for {module_file_path}"+ )+ module = importlib.util.module_from_spec(spec) # type: ignore+ spec.loader.exec_module(module) # type: ignore+ else:+ # Dynamically import the module+ module = importlib.import_module(module_name)++ # Get the instance from the module+ instance = getattr(module, instance_name)++ return instance+ except ImportError as e:+ # Re-raise the exception with a user-friendly message+ if instance_name and module_name:+ raise ImportError(+ f"Could not import {instance_name} from {module_name}"+ ) from e+ else:+ raise e+ except Exception as e:+ raise e+++def validate_custom_validate_return_type(+ fn: Optional[Callable[..., Any]]+) -> Optional[Callable[..., Literal[True]]]:+ if fn is None:+ return None++ hints = get_type_hints(fn)+ return_type = hints.get("return")++ if return_type != Literal[True]:+ raise TypeError(+ f"Custom validator must be annotated to return Literal[True], got {return_type}"+ )++ return fndiff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.pyindex 5e11f61522c..8042a781395 100644--- a/litellm/proxy/utils.py+++ b/litellm/proxy/utils.py@@ -1,7 +1,6 @@import asyncioimport copyimport hashlib-import importlibimport jsonimport osimport smtplib@@ -2222,51 +2221,6 @@ class PrismaClient:)-### CUSTOM FILE ###-def get_instance_fn(value: str, config_file_path: Optional[str] = None) -> Any:- module_name = value- instance_name = None- try:- # Split the path by dots to separate module from instance- parts = value.split(".")-- # The module path is all but the last part, and the instance_name is the last part- module_name = ".".join(parts[:-1])- instance_name = parts[-1]-- # If config_file_path is provided, use it to determine the module spec and load the module- if config_file_path is not None:- directory = os.path.dirname(config_file_path)- module_file_path = os.path.join(directory, *module_name.split("."))- module_file_path += ".py"-- spec = importlib.util.spec_from_file_location(module_name, module_file_path) # type: ignore- if spec is None:- raise ImportError(- f"Could not find a module specification for {module_file_path}"- )- module = importlib.util.module_from_spec(spec) # type: ignore- spec.loader.exec_module(module) # type: ignore- else:- # Dynamically import the module- module = importlib.import_module(module_name)-- # Get the instance from the module- instance = getattr(module, instance_name)-- return instance- except ImportError as e:- # Re-raise the exception with a user-friendly message- if instance_name and module_name:- raise ImportError(- f"Could not import {instance_name} from {module_name}"- ) from e- else:- raise e- except Exception as e:- raise e--### HELPER FUNCTIONS ###async def _cache_user_row(user_id: str, cache: DualCache, db: PrismaClient):"""diff --git a/tests/proxy_unit_tests/test_jwt.py b/tests/proxy_unit_tests/test_jwt.pyindex a168a91c120..7a9d2f0019b 100644--- a/tests/proxy_unit_tests/test_jwt.py+++ b/tests/proxy_unit_tests/test_jwt.py@@ -26,10 +26,16 @@ from fastapi.routing import APIRoutefrom fastapi.responses import Responseimport litellmfrom litellm.caching.caching import DualCache-from litellm.proxy._types import LiteLLM_JWTAuth, LiteLLM_UserTable, LiteLLMRoutes-from litellm.proxy.auth.handle_jwt import JWTHandler+from litellm.proxy._types import (+ LiteLLM_JWTAuth,+ LiteLLM_UserTable,+ LiteLLMRoutes,+ JWTAuthBuilderResult,+)+from litellm.proxy.auth.handle_jwt import JWTHandler, JWTAuthManagerfrom litellm.proxy.management_endpoints.team_endpoints import new_teamfrom litellm.proxy.proxy_server import chat_completion+from typing import Literalpublic_key = {"kty": "RSA",@@ -1247,3 +1253,32 @@ def test_check_scope_based_access(requested_model, should_work):else:with pytest.raises(HTTPException):JWTAuthManager.check_scope_based_access(**args)+++async def test_custom_validate_called():+ # Setup+ mock_custom_validate = MagicMock(return_value=True)++ jwt_handler = MagicMock()+ jwt_handler.litellm_jwtauth = MagicMock(+ custom_validate=mock_custom_validate, allowed_routes=["/chat/completions"]+ )+ jwt_handler.auth_jwt = AsyncMock(return_value={"sub": "test_user"})++ try:+ await JWTAuthManager.auth_builder(+ api_key="test",+ jwt_handler=jwt_handler,+ request_data={},+ general_settings={},+ route="/chat/completions",+ prisma_client=None,+ user_api_key_cache=MagicMock(),+ parent_otel_span=None,+ proxy_logging_obj=MagicMock(),+ )+ except Exception:+ pass+ # Assert custom_validate was called with the jwt token+ mock_custom_validate.assert_called_once_with({"sub": "test_user"})diff --git a/tests/proxy_unit_tests/test_proxy_custom_logger.py b/tests/proxy_unit_tests/test_proxy_custom_logger.pyindex eb75c4abf79..ad60335152f 100644--- a/tests/proxy_unit_tests/test_proxy_custom_logger.py+++ b/tests/proxy_unit_tests/test_proxy_custom_logger.py@@ -51,7 +51,7 @@ print("Testing proxy custom logger")def test_embedding(client):try:litellm.set_verbose = False- from litellm.proxy.utils import get_instance_fn+ from litellm.proxy.types_utils.utils import get_instance_fnmy_custom_logger = get_instance_fn(value="custom_callbacks.my_custom_logger", config_file_path=python_file_path@@ -122,7 +122,7 @@ def test_chat_completion(client):try:# Your test datalitellm.set_verbose = False- from litellm.proxy.utils import get_instance_fn+ from litellm.proxy.types_utils.utils import get_instance_fnmy_custom_logger = get_instance_fn(value="custom_callbacks.my_custom_logger", config_file_path=python_file_path@@ -217,7 +217,7 @@ def test_chat_completion_stream(client):try:# Your test datalitellm.set_verbose = False- from litellm.proxy.utils import get_instance_fn+ from litellm.proxy.types_utils.utils import get_instance_fnmy_custom_logger = get_instance_fn(value="custom_callbacks.my_custom_logger", config_file_path=python_file_path