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
Add remaining org CRUD endpoints + support deleting orgs on UI (#8561)
Krish Dholakia01ace78dcc6b
diff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.htmldeleted file mode 100644index 5f99ad5db9c..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-cb7d89b8d4a64932.js" async=""></script><script src="/ui/_next/static/chunks/117-cf5aa74edfdd4793.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/deccd6025d356171.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-e49c9b842247b56a.js\",\"659\",\"static/chunks/659-1fb49caa68833930.js\",\"755\",\"static/chunks/755-248eba4167b362a7.js\",\"250\",\"static/chunks/250-7c1dd32205ba8631.js\",\"461\",\"static/chunks/app/onboarding/page-13bd3d179b7732eb.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\":\"7fZTEM-yNGaA6bxfb1bmG\",\"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/deccd6025d356171.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/_types.py b/litellm/proxy/_types.pyindex 4c296a57982..4da023681eb 100644--- a/litellm/proxy/_types.py+++ b/litellm/proxy/_types.py@@ -5,7 +5,14 @@ from datetime import datetimefrom typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Unionimport httpx-from pydantic import BaseModel, ConfigDict, Field, Json, model_validator+from pydantic import (+ BaseModel,+ ConfigDict,+ Field,+ Json,+ field_validator,+ model_validator,+)from typing_extensions import Required, TypedDictfrom litellm.types.integrations.slack_alerting import AlertType@@ -393,6 +400,7 @@ class LiteLLMRoutes(enum.Enum):"/organization/info","/organization/delete","/organization/member_add",+ "/organization/member_update",]# All routes accesible by an Org Admin@@ -1110,6 +1118,10 @@ class OrganizationRequest(LiteLLMPydanticObjectBase):organizations: List[str]+class DeleteOrganizationRequest(LiteLLMPydanticObjectBase):+ organization_ids: List[str] # required++class KeyManagementSystem(enum.Enum):GOOGLE_KMS = "google_kms"AZURE_KEY_VAULT = "azure_key_vault"@@ -1494,6 +1506,18 @@ class LiteLLM_OrganizationTable(LiteLLMPydanticObjectBase):updated_by: str+class LiteLLM_OrganizationTableUpdate(LiteLLMPydanticObjectBase):+ """Represents user-controllable params for a LiteLLM_OrganizationTable record"""++ organization_id: Optional[str] = None+ organization_alias: Optional[str] = None+ budget_id: Optional[str] = None+ spend: Optional[float] = None+ metadata: Optional[dict] = None+ models: Optional[List[str]] = None+ updated_by: Optional[str] = None++class LiteLLM_OrganizationTableWithMembers(LiteLLM_OrganizationTable):"""Returned by the /organization/info endpoint and /organization/list endpoint"""@@ -2097,8 +2121,26 @@ class OrganizationMemberDeleteRequest(MemberDeleteRequest):organization_id: str+ROLES_WITHIN_ORG = [+ LitellmUserRoles.ORG_ADMIN,+ LitellmUserRoles.INTERNAL_USER,+ LitellmUserRoles.INTERNAL_USER_VIEW_ONLY,+]++class OrganizationMemberUpdateRequest(OrganizationMemberDeleteRequest):- max_budget_in_organization: float+ max_budget_in_organization: Optional[float] = None+ role: Optional[LitellmUserRoles] = None++ @field_validator("role")+ def validate_role(+ cls, value: Optional[LitellmUserRoles]+ ) -> Optional[LitellmUserRoles]:+ if value is not None and value not in ROLES_WITHIN_ORG:+ raise ValueError(+ f"Invalid role. Must be one of: {[role.value for role in ROLES_WITHIN_ORG]}"+ )+ return valueclass OrganizationMemberUpdateResponse(MemberUpdateResponse):diff --git a/litellm/proxy/management_endpoints/organization_endpoints.py b/litellm/proxy/management_endpoints/organization_endpoints.pyindex 20ba084d307..3b6c625abe8 100644--- a/litellm/proxy/management_endpoints/organization_endpoints.py+++ b/litellm/proxy/management_endpoints/organization_endpoints.py@@ -19,6 +19,10 @@ from fastapi import APIRouter, Depends, HTTPException, Request, statusfrom litellm._logging import verbose_proxy_loggerfrom litellm.proxy._types import *from litellm.proxy.auth.user_api_key_auth import user_api_key_auth+from litellm.proxy.management_endpoints.budget_management_endpoints import (+ new_budget,+ update_budget,+)from litellm.proxy.management_helpers.utils import (get_new_internal_user_defaults,management_endpoint_wrapper,@@ -179,24 +183,105 @@ async def new_organization(return response"/organization/update",tags=["organization management"],dependencies=[Depends(user_api_key_auth)],+ response_model=LiteLLM_OrganizationTableWithMembers,)-async def update_organization():- """[TODO] Not Implemented yet. Let us know if you need this - https://github.com/BerriAI/litellm/issues"""- raise NotImplementedError("Not Implemented Yet")+async def update_organization(+ data: LiteLLM_OrganizationTableUpdate,+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),+):+ """+ Update an organization+ """+ from litellm.proxy.proxy_server import prisma_client++ if prisma_client is None:+ raise HTTPException(+ status_code=500,+ detail={"error": CommonProxyErrors.db_not_connected_error.value},+ )++ if user_api_key_dict.user_id is None:+ raise HTTPException(+ status_code=400,+ detail={+ "error": "Cannot associate a user_id to this action. Check `/key/info` to validate if 'user_id' is set."+ },+ )+ if data.updated_by is None:+ data.updated_by = user_api_key_dict.user_id+ response = await prisma_client.db.litellm_organizationtable.update(+ where={"organization_id": data.organization_id},+ data=data.model_dump(exclude_none=True),+ include={"members": True, "teams": True, "litellm_budget_table": True},+ )++ return response++"/organization/delete",tags=["organization management"],dependencies=[Depends(user_api_key_auth)],+ response_model=List[LiteLLM_OrganizationTableWithMembers],)-async def delete_organization():- """[TODO] Not Implemented yet. Let us know if you need this - https://github.com/BerriAI/litellm/issues"""- raise NotImplementedError("Not Implemented Yet")+async def delete_organization(+ data: DeleteOrganizationRequest,+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),+):+ """+ Delete an organization++ # Parameters:++ - organization_ids: List[str] - The organization ids to delete.+ """+ from litellm.proxy.proxy_server import prisma_client++ if prisma_client is None:+ raise HTTPException(+ status_code=500,+ detail={"error": CommonProxyErrors.db_not_connected_error.value},+ )++ if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN:+ raise HTTPException(+ status_code=401,+ detail={"error": "Only proxy admins can delete organizations"},+ )++ deleted_orgs = []+ for organization_id in data.organization_ids:+ # delete all teams in the organization+ await prisma_client.db.litellm_teamtable.delete_many(+ where={"organization_id": organization_id}+ )+ # delete all members in the organization+ await prisma_client.db.litellm_organizationmembership.delete_many(+ where={"organization_id": organization_id}+ )+ # delete all keys in the organization+ await prisma_client.db.litellm_verificationtoken.delete_many(+ where={"organization_id": organization_id}+ )+ # delete the organization+ deleted_org = await prisma_client.db.litellm_organizationtable.delete(+ where={"organization_id": organization_id},+ include={"members": True, "teams": True, "litellm_budget_table": True},+ )+ if deleted_org is None:+ raise HTTPException(+ status_code=404,+ detail={"error": f"Organization={organization_id} not found"},+ )+ deleted_orgs.append(deleted_org)++ return deleted_orgs@router.get(@@ -429,6 +514,213 @@ async def organization_member_add()+async def find_member_if_email(+ user_email: str, prisma_client: PrismaClient+) -> LiteLLM_UserTable:+ """+ Find a member if the user_email is in LiteLLM_UserTable+ """++ try:+ existing_user_email_row: BaseModel = (+ await prisma_client.db.litellm_usertable.find_unique(+ where={"user_email": user_email}+ )+ )+ except Exception:+ raise HTTPException(+ status_code=400,+ detail={+ "error": f"Unique user not found for user_email={user_email}. Potential duplicate OR non-existent user_email in LiteLLM_UserTable. Use 'user_id' instead."+ },+ )+ existing_user_email_row_pydantic = LiteLLM_UserTable(+ **existing_user_email_row.model_dump()+ )+ return existing_user_email_row_pydantic+++ "/organization/member_update",+ tags=["organization management"],+ dependencies=[Depends(user_api_key_auth)],+ response_model=LiteLLM_OrganizationMembershipTable,+)+@management_endpoint_wrapper+async def organization_member_update(+ data: OrganizationMemberUpdateRequest,+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),+):+ """+ Update a member's role in an organization+ """+ try:+ from litellm.proxy.proxy_server import prisma_client++ if prisma_client is None:+ raise HTTPException(+ status_code=500,+ detail={"error": CommonProxyErrors.db_not_connected_error.value},+ )++ # Check if organization exists+ existing_organization_row = (+ await prisma_client.db.litellm_organizationtable.find_unique(+ where={"organization_id": data.organization_id}+ )+ )+ if existing_organization_row is None:+ raise HTTPException(+ status_code=400,+ detail={+ "error": f"Organization not found for organization_id={getattr(data, 'organization_id', None)}"+ },+ )++ # Check if member exists in organization+ if data.user_email is not None and data.user_id is None:+ existing_user_email_row = await find_member_if_email(+ data.user_email, prisma_client+ )+ data.user_id = existing_user_email_row.user_id++ try:+ existing_organization_membership = (+ await prisma_client.db.litellm_organizationmembership.find_unique(+ where={+ "user_id_organization_id": {+ "user_id": data.user_id,+ "organization_id": data.organization_id,+ }+ }+ )+ )+ except Exception as e:+ raise HTTPException(+ status_code=400,+ detail={+ "error": f"Error finding organization membership for user_id={data.user_id} in organization={data.organization_id}: {e}"+ },+ )+ if existing_organization_membership is None:+ raise HTTPException(+ status_code=404,+ detail={+ "error": f"Member not found in organization for user_id={data.user_id}"+ },+ )++ # Update member role+ if data.role is not None:+ await prisma_client.db.litellm_organizationmembership.update(+ where={+ "user_id_organization_id": {+ "user_id": data.user_id,+ "organization_id": data.organization_id,+ }+ },+ data={"user_role": data.role},+ )+ if data.max_budget_in_organization is not None:+ # if budget_id is None, create a new budget+ budget_id = existing_organization_membership.budget_id or str(uuid.uuid4())+ if existing_organization_membership.budget_id is None:+ new_budget_obj = BudgetNewRequest(+ budget_id=budget_id, max_budget=data.max_budget_in_organization+ )+ await new_budget(+ budget_obj=new_budget_obj, user_api_key_dict=user_api_key_dict+ )+ else:+ # update budget table with new max_budget+ await update_budget(+ budget_obj=BudgetNewRequest(+ budget_id=budget_id, max_budget=data.max_budget_in_organization+ ),+ user_api_key_dict=user_api_key_dict,+ )++ # update organization membership with new budget_id+ await prisma_client.db.litellm_organizationmembership.update(+ where={+ "user_id_organization_id": {+ "user_id": data.user_id,+ "organization_id": data.organization_id,+ }+ },+ data={"budget_id": budget_id},+ )+ final_organization_membership: Optional[BaseModel] = (+ await prisma_client.db.litellm_organizationmembership.find_unique(+ where={+ "user_id_organization_id": {+ "user_id": data.user_id,+ "organization_id": data.organization_id,+ }+ },+ include={"litellm_budget_table": True},+ )+ )++ if final_organization_membership is None:+ raise HTTPException(+ status_code=400,+ detail={+ "error": f"Member not found in organization={data.organization_id} for user_id={data.user_id}"+ },+ )++ final_organization_membership_pydantic = LiteLLM_OrganizationMembershipTable(+ **final_organization_membership.model_dump(exclude_none=True)+ )+ return final_organization_membership_pydantic+ except Exception as e:+ verbose_proxy_logger.exception(f"Error updating member in organization: {e}")+ raise e+++ "/organization/member_delete",+ tags=["organization management"],+ dependencies=[Depends(user_api_key_auth)],+)+async def organization_member_delete(+ data: OrganizationMemberDeleteRequest,+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),+):+ """+ Delete a member from an organization+ """+ try:+ from litellm.proxy.proxy_server import prisma_client++ if prisma_client is None:+ raise HTTPException(+ status_code=500,+ detail={"error": CommonProxyErrors.db_not_connected_error.value},+ )++ if data.user_email is not None and data.user_id is None:+ existing_user_email_row = await find_member_if_email(+ data.user_email, prisma_client+ )+ data.user_id = existing_user_email_row.user_id++ member_to_delete = await prisma_client.db.litellm_organizationmembership.delete(+ where={+ "user_id_organization_id": {+ "user_id": data.user_id,+ "organization_id": data.organization_id,+ }+ }+ )+ return member_to_delete++ except Exception as e:+ verbose_proxy_logger.exception(f"Error deleting member from organization: {e}")+ raise e++async def add_member_to_organization(member: OrgMember,organization_id: str,diff --git a/tests/test_organizations.py b/tests/test_organizations.pyindex 588d838f293..565aba14d46 100644--- a/tests/test_organizations.py+++ b/tests/test_organizations.py@@ -7,6 +7,49 @@ import time, uuidfrom openai import AsyncOpenAI+async def new_user(+ session,+ i,+ user_id=None,+ budget=None,+ budget_duration=None,+ models=["azure-models"],+ team_id=None,+ user_email=None,+):+ url = "http://0.0.0.0:4000/user/new"+ headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}+ data = {+ "models": models,+ "aliases": {"mistral-7b": "gpt-3.5-turbo"},+ "duration": None,+ "max_budget": budget,+ "budget_duration": budget_duration,+ "user_email": user_email,+ }++ if user_id is not None:+ data["user_id"] = user_id++ if team_id is not None:+ data["team_id"] = team_id++ async with session.post(url, headers=headers, json=data) as response:+ status = response.status+ response_text = await response.text()++ print(f"Response {i} (Status code: {status}):")+ print(response_text)+ print()++ if status != 200:+ raise Exception(+ f"Request {i} did not return a 200 status code: {status}, response: {response_text}"+ )++ return await response.json()++async def new_organization(session, i, organization_alias, max_budget=None):url = "http://0.0.0.0:4000/organization/new"headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}@@ -30,6 +73,99 @@ async def new_organization(session, i, organization_alias, max_budget=None):return await response.json()+async def add_member_to_org(+ session, i, organization_id, user_id, user_role="internal_user"+):+ url = "http://0.0.0.0:4000/organization/member_add"+ headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}+ data = {+ "organization_id": organization_id,+ "member": {+ "user_id": user_id,+ "role": user_role,+ },+ }++ async with session.post(url, headers=headers, json=data) as response:+ status = response.status+ response_text = await response.text()++ print(f"Response {i} (Status code: {status}):")+ print(response_text)+ print()++ if status != 200:+ raise Exception(f"Request {i} did not return a 200 status code: {status}")++ return await response.json()+++async def update_member_role(+ session, i, organization_id, user_id, user_role="internal_user"+):+ url = "http://0.0.0.0:4000/organization/member_update"+ headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}+ data = {+ "organization_id": organization_id,+ "user_id": user_id,+ "role": user_role,+ }++ async with session.patch(url, headers=headers, json=data) as response:+ status = response.status+ response_text = await response.text()++ print(f"Response {i} (Status code: {status}):")+ print(response_text)+ print()++ if status != 200:+ raise Exception(f"Request {i} did not return a 200 status code: {status}")++ return await response.json()+++async def delete_member_from_org(session, i, organization_id, user_id):+ url = "http://0.0.0.0:4000/organization/member_delete"+ headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}+ data = {+ "organization_id": organization_id,+ "user_id": user_id,+ }++ async with session.delete(url, headers=headers, json=data) as response:+ status = response.status+ response_text = await response.text()++ print(f"Response {i} (Status code: {status}):")+ print(response_text)+ print()++ if status != 200:+ raise Exception(f"Request {i} did not return a 200 status code: {status}")++ return await response.json()+++async def delete_organization(session, i, organization_id):+ url = "http://0.0.0.0:4000/organization/delete"+ headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}+ data = {"organization_ids": [organization_id]}++ async with session.delete(url, headers=headers, json=data) as response:+ status = response.status+ response_text = await response.text()++ print(f"Response {i} (Status code: {status}):")+ print(response_text)+ print()++ if status != 200:+ raise Exception(f"Request {i} did not return a 200 status code: {status}")++ return await response.json()++async def list_organization(session, i):url = "http://0.0.0.0:4000/organization/list"headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}@@ -84,3 +220,91 @@ async def test_organization_list():if len(response_json) == 0:raise Exception("Return empty list of organization")+++async def test_organization_delete():+ """+ create a new organization+ delete the organization+ check if the Organization list is set+ """+ organization_alias = f"Organization: {uuid.uuid4()}"+ async with aiohttp.ClientSession() as session:+ tasks = [+ new_organization(+ session=session, i=0, organization_alias=organization_alias+ )+ ]+ await asyncio.gather(*tasks)++ response_json = await list_organization(session, i=0)+ print(len(response_json))++ organization_id = response_json[0]["organization_id"]+ await delete_organization(session, i=0, organization_id=organization_id)++ response_json = await list_organization(session, i=0)+ print(len(response_json))+++async def test_organization_member_flow():+ """+ create a new organization+ add a new member to the organization+ check if the member is added to the organization+ update the member's role in the organization+ delete the member from the organization+ check if the member is deleted from the organization+ """+ organization_alias = f"Organization: {uuid.uuid4()}"+ async with aiohttp.ClientSession() as session:+ response_json = await new_organization(+ session=session, i=0, organization_alias=organization_alias+ )+ organization_id = response_json["organization_id"]++ response_json = await list_organization(session, i=0)+ print(len(response_json))++ new_user_response_json = await new_user(+ session=session, i=0, user_email=f"test_user_{uuid.uuid4()}@example.com"+ )+ user_id = new_user_response_json["user_id"]++ await add_member_to_org(+ session, i=0, organization_id=organization_id, user_id=user_id+ )++ response_json = await list_organization(session, i=0)+ print(len(response_json))++ for orgs in response_json:+ tmp_organization_id = orgs["organization_id"]+ if (+ tmp_organization_id is not None+ and tmp_organization_id == organization_id+ ):+ user_id = orgs["members"][0]["user_id"]++ response_json = await list_organization(session, i=0)+ print(len(response_json))++ await update_member_role(+ session,+ i=0,+ organization_id=organization_id,+ user_id=user_id,+ user_role="org_admin",+ )++ response_json = await list_organization(session, i=0)+ print(len(response_json))++ await delete_member_from_org(+ session, i=0, organization_id=organization_id, user_id=user_id+ )++ response_json = await list_organization(session, i=0)+ print(len(response_json))diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsxindex 97f6e179438..e00bb56c422 100644--- a/ui/litellm-dashboard/src/components/networking.tsx+++ b/ui/litellm-dashboard/src/components/networking.tsx@@ -905,6 +905,37 @@ export const organizationCreateCall = async (}};+export const organizationDeleteCall = async (+ accessToken: string,+ organizationID: string+) => {+ try {+ const url = proxyBaseUrl ? `${proxyBaseUrl}/organization/delete` : `/organization/delete`;+ const response = await fetch(url, {+ method: "DELETE",+ headers: {+ [globalLitellmHeaderName]: `Bearer ${accessToken}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ organization_ids: [organizationID]+ }),+ });++ if (!response.ok) {+ const errorData = await response.text();+ handleError(errorData);+ throw new Error(`Error deleting organization: ${errorData}`);+ }++ const data = await response.json();+ return data;+ } catch (error) {+ console.error("Failed to delete organization:", error);+ throw error;+ }+};+export const getTotalSpendCall = async (accessToken: String) => {/*** Get all models on proxy@@ -2642,6 +2673,85 @@ export const organizationMemberAddCall = async (}};+export const organizationMemberDeleteCall = async (+ accessToken: string,+ organizationId: string,+ userId: string+) => {+ try {+ console.log("Form Values in organizationMemberDeleteCall:", userId); // Log the form values before making the API call++ const url = proxyBaseUrl+ ? `${proxyBaseUrl}/organization/member_delete`+ : `/organization/member_delete`;++ const response = await fetch(url, {+ method: "DELETE",+ headers: {+ [globalLitellmHeaderName]: `Bearer ${accessToken}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ organization_id: organizationId,+ user_id: userId+ }),+ });++ if (!response.ok) {+ const errorData = await response.text();+ handleError(errorData);+ console.error("Error response from the server:", errorData);+ throw new Error("Network response was not ok");+ }++ const data = await response.json();+ console.log("API Response:", data);+ return data;+ } catch (error) {+ console.error("Failed to delete organization member:", error);+ throw error;+ }+};+export const organizationMemberUpdateCall = async (+ accessToken: string,+ organizationId: string,+ formValues: Member // Assuming formValues is an object+) => {+ try {+ console.log("Form Values in organizationMemberUpdateCall:", formValues); // Log the form values before making the API call++ const url = proxyBaseUrl+ ? `${proxyBaseUrl}/organization/member_update`+ : `/organization/member_update`;++ const response = await fetch(url, {+ method: "PATCH",+ headers: {+ [globalLitellmHeaderName]: `Bearer ${accessToken}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ organization_id: organizationId,+ ...formValues, // Include formValues in the request body+ }),+ });++ if (!response.ok) {+ const errorData = await response.text();+ handleError(errorData);+ console.error("Error response from the server:", errorData);+ throw new Error("Network response was not ok");+ }++ const data = await response.json();+ console.log("API Response:", data);+ return data;+ } catch (error) {+ console.error("Failed to update organization member:", error);+ throw error;+ }+};+export const userUpdateUserCall = async (accessToken: string,formValues: any, // Assuming formValues is an objectdiff --git a/ui/litellm-dashboard/src/components/organization/organization_view.tsx b/ui/litellm-dashboard/src/components/organization/organization_view.tsxindex 7c2994bb1fe..593e832ce15 100644--- a/ui/litellm-dashboard/src/components/organization/organization_view.tsx+++ b/ui/litellm-dashboard/src/components/organization/organization_view.tsx@@ -23,9 +23,9 @@ import { Button, Form, Input, Select, message, InputNumber, Tooltip } from "antdimport { InfoCircleOutlined } from '@ant-design/icons';import { PencilAltIcon, TrashIcon } from "@heroicons/react/outline";import { getModelDisplayName } from "../key_team_helpers/fetch_available_models_team_key";-import { Member, Organization, organizationInfoCall, organizationMemberAddCall } from "../networking";+import { Member, Organization, organizationInfoCall, organizationMemberAddCall, organizationMemberUpdateCall, organizationMemberDeleteCall } from "../networking";import UserSearchModal from "../common_components/user_search_modal";-+import MemberModal from "../team/edit_membership";interface OrganizationInfoProps {organizationId: string;@@ -51,6 +51,8 @@ const OrganizationInfoView: React.FC<OrganizationInfoProps> = ({const [form] = Form.useForm();const [isEditing, setIsEditing] = useState(false);const [isAddMemberModalVisible, setIsAddMemberModalVisible] = useState(false);+ const [isEditMemberModalVisible, setIsEditMemberModalVisible] = useState(false);+ const [selectedEditMember, setSelectedEditMember] = useState<Member | null>(null);const canEditOrg = is_org_admin || is_proxy_admin;@@ -95,6 +97,42 @@ const OrganizationInfoView: React.FC<OrganizationInfoProps> = ({}};+ const handleMemberUpdate = async (values: any) => {+ try {+ if (!accessToken) return;++ const member: Member = {+ user_email: values.user_email,+ user_id: values.user_id,+ role: values.role,+ }++ const response = await organizationMemberUpdateCall(accessToken, organizationId, member);+ message.success("Organization member updated successfully");+ setIsEditMemberModalVisible(false);+ form.resetFields();+ fetchOrgInfo();+ } catch (error) {+ message.error("Failed to update organization member");+ console.error("Error updating organization member:", error);+ }+ };++ const handleMemberDelete = async (values: any) => {+ try {+ if (!accessToken) return;++ await organizationMemberDeleteCall(accessToken, organizationId, values.user_id);+ message.success("Organization member deleted successfully");+ setIsEditMemberModalVisible(false);+ form.resetFields();+ fetchOrgInfo();+ } catch (error) {+ message.error("Failed to delete organization member");+ console.error("Error deleting organization member:", error);+ }+ };+const handleOrgUpdate = async (values: any) => {try {if (!accessToken) return;@@ -241,14 +279,19 @@ const OrganizationInfoView: React.FC<OrganizationInfoProps> = ({icon={PencilAltIcon}size="sm"onClick={() => {- // TODO: Implement edit member functionality+ setSelectedEditMember({+ "role": member.user_role,+ "user_email": member.user_email,+ "user_id": member.user_id+ });+ setIsEditMemberModalVisible(true);}}/><Iconicon={TrashIcon}size="sm"onClick={() => {- // TODO: Implement delete member functionality+ handleMemberDelete(member);}}/></>@@ -403,6 +446,23 @@ const OrganizationInfoView: React.FC<OrganizationInfoProps> = ({]}defaultRole="internal_user"/>+ <MemberModal+ visible={isEditMemberModalVisible}+ onCancel={() => setIsEditMemberModalVisible(false)}+ onSubmit={handleMemberUpdate}+ initialData={selectedEditMember}+ mode="edit"+ config={{+ title: "Edit Member",+ showEmail: true,+ showUserId: true,+ roleOptions: [+ { label: "Org Admin", value: "org_admin" },+ { label: "Internal User", value: "internal_user" },+ { label: "Internal User Viewer", value: "internal_user_viewer" }+ ]+ }}+ /></div>);};diff --git a/ui/litellm-dashboard/src/components/organizations.tsx b/ui/litellm-dashboard/src/components/organizations.tsxindex 58df99dba3f..5e6cf6f58e4 100644--- a/ui/litellm-dashboard/src/components/organizations.tsx+++ b/ui/litellm-dashboard/src/components/organizations.tsx@@ -24,8 +24,9 @@ import { InfoCircleOutlined } from '@ant-design/icons';import { PencilAltIcon, TrashIcon, RefreshIcon } from "@heroicons/react/outline";import { TextInput } from "@tremor/react";import { getModelDisplayName } from './key_team_helpers/fetch_available_models_team_key';+import { message } from 'antd';import OrganizationInfoView from './organization/organization_view';-import { Organization, organizationListCall, organizationCreateCall } from './networking';+import { Organization, organizationListCall, organizationCreateCall, organizationDeleteCall } from './networking';interface OrganizationsTableProps {organizations: Organization[];userRole: string;@@ -80,18 +81,13 @@ const OrganizationsTable: React.FC<OrganizationsTableProps> = ({if (!orgToDelete || !accessToken) return;try {- const response = await fetch(`/organization/delete?organization_id=${orgToDelete}`, {- method: 'DELETE',- headers: {- 'Authorization': `Bearer ${accessToken}`- }- });+ await organizationDeleteCall(accessToken, orgToDelete);+ message.success('Organization deleted successfully');- if (!response.ok) throw new Error('Failed to delete organization');-setIsDeleteModalOpen(false);setOrgToDelete(null);// Refresh organizations list+ fetchOrganizations(accessToken, setOrganizations);} catch (error) {console.error('Error deleting organization:', error);}diff --git a/ui/litellm-dashboard/src/components/team/edit_membership.tsx b/ui/litellm-dashboard/src/components/team/edit_membership.tsxindex 3d1696916e9..27e64193e35 100644--- a/ui/litellm-dashboard/src/components/team/edit_membership.tsx+++ b/ui/litellm-dashboard/src/components/team/edit_membership.tsx@@ -2,58 +2,130 @@ import React, { useState, useEffect } from 'react';import { Modal, Form, Input, Select as AntSelect, Button as AntButton, message } from 'antd';import { Select, SelectItem } from "@tremor/react";import { Card, Text } from "@tremor/react";-import { Member } from "@/components/networking";-interface TeamMemberModalProps {++interface BaseMember {+ user_email?: string;+ user_id?: string;+ role: string;+}++interface ModalConfig {+ title: string;+ roleOptions: Array<{+ label: string;+ value: string;+ }>;+ defaultRole?: string;+ showEmail?: boolean;+ showUserId?: boolean;+ additionalFields?: Array<{+ name: string;+ label: string;+ type: 'input' | 'select';+ options?: Array<{ label: string; value: string }>;+ rules?: any[];+ }>;+}++interface MemberModalProps<T extends BaseMember> {visible: boolean;onCancel: () => void;- onSubmit: (data: Member) => void;- initialData?: Member | null;+ onSubmit: (data: T) => void;+ initialData?: T | null;mode: 'add' | 'edit';+ config: ModalConfig;}-const TeamMemberModal: React.FC<TeamMemberModalProps> = ({+const MemberModal = <T extends BaseMember>({visible,onCancel,onSubmit,initialData,- mode-}) => {+ mode,+ config+}: MemberModalProps<T>) => {const [form] = Form.useForm();+ console.log("Initial Data:", initialData);++ // Reset form and set initial values when modal becomes visible or initialData changesuseEffect(() => {- if (initialData) {- form.setFieldsValue({- user_email: initialData.user_email,- user_id: initialData.user_id,- role: initialData.role,- });+ if (visible) {+ if (mode === 'edit' && initialData) {+ // For edit mode, use the initialData values+ form.setFieldsValue({+ ...initialData,+ // Ensure role is set correctly for editing+ role: initialData.role || config.defaultRole+ });+ } else {+ // For add mode, reset to defaults+ form.resetFields();+ form.setFieldsValue({+ role: config.defaultRole || config.roleOptions[0]?.value+ });+ }}- }, [initialData, form]);-+ }, [visible, initialData, mode, form, config.defaultRole, config.roleOptions]);const handleSubmit = async (values: any) => {try {- const formData: Member = {- user_email: values.user_email,- user_id: values.user_id,- role: values.role,- };+ // Trim string values+ const formData = Object.entries(values).reduce((acc, [key, value]) => ({+ ...acc,+ [key]: typeof value === 'string' ? value.trim() : value+ }), {}) as T;onSubmit(formData);form.resetFields();- message.success(`Successfully ${mode === 'add' ? 'added' : 'updated'} team member`);+ message.success(`Successfully ${mode === 'add' ? 'added' : 'updated'} member`);} catch (error) {message.error('Failed to submit form');console.error('Form submission error:', error);}};+ // Helper function to get role label from value+ const getRoleLabel = (value: string) => {+ return config.roleOptions.find(option => option.value === value)?.label || value;+ };+ const renderField = (field: {+ name: string;+ label: string;+ type: 'input' | 'select';+ options?: Array<{ label: string; value: string }>;+ rules?: any[];+ }) => {+ switch (field.type) {+ case 'input':+ return (+ <Input+ className="px-3 py-2 border rounded-md w-full"+ onChange={(e) => {+ e.target.value = e.target.value.trim();+ }}+ />+ );+ case 'select':+ return (+ <AntSelect>+ {field.options?.map(option => (+ <AntSelect.Option key={option.value} value={option.value}>+ {option.label}+ </AntSelect.Option>+ ))}+ </AntSelect>+ );+ default:+ return null;+ }+ };return (<Modal- title={mode === 'add' ? "Add Team Member" : "Edit Team Member"}- visible={visible}+ title={config.title || (mode === 'add' ? "Add Member" : "Edit Member")}+ open={visible}width={800}footer={null}onCancel={onCancel}@@ -64,67 +136,101 @@ const TeamMemberModal: React.FC<TeamMemberModalProps> = ({labelCol={{ span: 8 }}wrapperCol={{ span: 16 }}labelAlign="left"- initialValues={{- user_email: initialData?.user_email?.trim() || '',- user_id: initialData?.user_id?.trim() || '',- role: initialData?.role || 'user',- }}>- <Form.Item- label="Email"- name="user_email"- className="mb-4"- rules={[- { type: 'email', message: 'Please enter a valid email!' }- ]}- >- <Input+ {config.showEmail && (+ <Form.Item+ label="Email"name="user_email"- className="px-3 py-2 border rounded-md w-full"- placeholder="[email protected]"- onChange={(e) => {+ className="mb-4"+ rules={[+ { type: 'email', message: 'Please enter a valid email!' }+ ]}+ >+ <Input+ className="px-3 py-2 border rounded-md w-full"+ placeholder="[email protected]"+ onChange={(e) => {e.target.value = e.target.value.trim();- }}- />- </Form.Item>+ }}+ />+ </Form.Item>+ )}- <div className="text-center mb-4">- <Text>OR</Text>- </div>+ {config.showEmail && config.showUserId && (+ <div className="text-center mb-4">+ <Text>OR</Text>+ </div>+ )}- <Form.Item- label="User ID"- name="user_id"- className="mb-4"- >- <Input+ {config.showUserId && (+ <Form.Item+ label="User ID"name="user_id"- className="px-3 py-2 border rounded-md w-full"- placeholder="user_123"- onChange={(e) => {+ className="mb-4"+ >+ <Input+ className="px-3 py-2 border rounded-md w-full"+ placeholder="user_123"+ onChange={(e) => {e.target.value = e.target.value.trim();- }}- />- </Form.Item>+ }}+ />+ </Form.Item>+ )}<Form.Item- label="Member Role"+ label={+ <div className="flex items-center gap-2">+ <span>Role</span>+ {mode === 'edit' && initialData && (+ <span className="text-gray-500 text-sm">+ (Current: {getRoleLabel(initialData.role)})+ </span>+ )}+ </div>+ }name="role"className="mb-4"rules={[{ required: true, message: 'Please select a role!' }]}>- <AntSelect defaultValue="user">- <AntSelect.Option value="admin">admin</AntSelect.Option>- <AntSelect.Option value="user">user</AntSelect.Option>+ <AntSelect>+ {mode === 'edit' && initialData+ ? [+ // Current role first+ ...config.roleOptions.filter(option => option.value === initialData.role),+ // Then all other roles+ ...config.roleOptions.filter(option => option.value !== initialData.role)+ ].map(option => (+ <AntSelect.Option key={option.value} value={option.value}>+ {option.label}+ </AntSelect.Option>+ ))+ : config.roleOptions.map(option => (+ <AntSelect.Option key={option.value} value={option.value}>+ {option.label}+ </AntSelect.Option>+ ))}</AntSelect></Form.Item>- <div style={{ textAlign: "right", marginTop: "20px" }}>+ {config.additionalFields?.map(field => (+ <Form.Item+ key={field.name}+ label={field.label}+ name={field.name}+ className="mb-4"+ rules={field.rules}+ >+ {renderField(field)}+ </Form.Item>+ ))}++ <div className="text-right mt-6"><AntButtononClick={onCancel}- style={{ marginRight: 8 }}+ className="mr-2">Cancel</AntButton>@@ -140,4 +246,4 @@ const TeamMemberModal: React.FC<TeamMemberModalProps> = ({);};-export default TeamMemberModal;\ No newline at end of file+export default MemberModal;\ No newline at end of filediff --git a/ui/litellm-dashboard/src/components/team/team_info.tsx b/ui/litellm-dashboard/src/components/team/team_info.tsxindex 65b0f6a5614..3b6fc09e295 100644--- a/ui/litellm-dashboard/src/components/team/team_info.tsx+++ b/ui/litellm-dashboard/src/components/team/team_info.tsx@@ -26,7 +26,7 @@ import {Select as Select2,} from "antd";import { PencilAltIcon, PlusIcon, TrashIcon } from "@heroicons/react/outline";-import TeamMemberModal from "./edit_membership";+import MemberModal from "./edit_membership";import UserSearchModal from "@/components/common_components/user_search_modal";import { getModelDisplayName } from "../key_team_helpers/fetch_available_models_team_key";import ModelAliasesCard from "./model_aliases_card";@@ -486,12 +486,21 @@ const TeamInfoView: React.FC<TeamInfoProps> = ({</TabPanels></TabGroup>- <TeamMemberModal+ <MemberModalvisible={isEditMemberModalVisible}onCancel={() => setIsEditMemberModalVisible(false)}onSubmit={handleMemberUpdate}initialData={selectedEditMember}mode="edit"+ config={{+ title: "Edit Member",+ showEmail: true,+ showUserId: true,+ roleOptions: [+ { label: "Admin", value: "admin" },+ { label: "User", value: "user" }+ ]+ }}/><UserSearchModal