Skip to content

Commit a962a97

Browse files
feat(endpoints.py): support writing credentials to db
1 parent f1cdc26 commit a962a97

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

litellm/proxy/credential_endpoints/endpoints.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import traceback
77
from typing import Optional
88

9-
from fastapi import APIRouter, Depends, Request, Response
9+
from fastapi import APIRouter, Depends, HTTPException, Request, Response
1010

1111
import litellm
12-
from litellm.proxy._types import UserAPIKeyAuth
12+
from litellm._logging import verbose_proxy_logger
13+
from litellm.proxy._types import CommonProxyErrors, UserAPIKeyAuth
1314
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
14-
from litellm.proxy.utils import handle_exception_on_proxy
15+
from litellm.proxy.utils import handle_exception_on_proxy, jsonify_object
1516
from litellm.types.utils import CredentialItem
1617

1718
router = APIRouter()
@@ -28,11 +29,33 @@ async def create_credential(
2829
credential: CredentialItem,
2930
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
3031
):
32+
"""
33+
Stores credential in DB.
34+
Reloads credentials in memory.
35+
"""
36+
from litellm.proxy.proxy_server import prisma_client
37+
3138
try:
32-
litellm.credential_list.append(credential)
39+
if prisma_client is None:
40+
raise HTTPException(
41+
status_code=500,
42+
detail={"error": CommonProxyErrors.db_not_connected_error.value},
43+
)
44+
45+
credentials_dict = credential.model_dump()
46+
credentials_dict_jsonified = jsonify_object(credentials_dict)
47+
await prisma_client.db.litellm_credentialstable.create(
48+
data={
49+
**credentials_dict_jsonified,
50+
"created_by": user_api_key_dict.user_id,
51+
"updated_by": user_api_key_dict.user_id,
52+
}
53+
)
54+
3355
return {"success": True, "message": "Credential created successfully"}
3456
except Exception as e:
35-
return handle_exception_on_proxy(e)
57+
verbose_proxy_logger.exception(e)
58+
raise handle_exception_on_proxy(e)
3659

3760

3861
@router.get(

litellm/proxy/schema.prisma

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ model LiteLLM_BudgetTable {
2929
organization_membership LiteLLM_OrganizationMembership[] // budgets of Users within a Organization
3030
}
3131

32+
// Models on proxy
33+
model LiteLLM_CredentialsTable {
34+
credential_id String @id @default(uuid())
35+
credential_name String @unique
36+
credential_values Json
37+
credential_info Json?
38+
created_at DateTime @default(now()) @map("created_at")
39+
created_by String
40+
updated_at DateTime @default(now()) @updatedAt @map("updated_at")
41+
updated_by String
42+
}
43+
3244
// Models on proxy
3345
model LiteLLM_ProxyModelTable {
3446
model_id String @id @default(uuid())

litellm/types/utils.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
CategoryScores,
1919
)
2020
from openai.types.moderation_create_response import Moderation, ModerationCreateResponse
21-
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr, Secret
21+
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr
2222
from typing_extensions import Callable, Dict, Required, TypedDict, override
2323

2424
import litellm
2525

26-
SecretDict = Secret[dict]
27-
2826
from ..litellm_core_utils.core_helpers import map_finish_reason
2927
from .guardrails import GuardrailEventHooks
3028
from .llms.openai import (
@@ -2018,5 +2016,5 @@ class RawRequestTypedDict(TypedDict, total=False):
20182016

20192017
class CredentialItem(BaseModel):
20202018
credential_name: str
2021-
credential_values: SecretDict
2019+
credential_values: dict
20222020
credential_info: dict

schema.prisma

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ model LiteLLM_BudgetTable {
2929
organization_membership LiteLLM_OrganizationMembership[] // budgets of Users within a Organization
3030
}
3131

32+
// Models on proxy
33+
model LiteLLM_CredentialsTable {
34+
credential_id String @id @default(uuid())
35+
credential_name String @unique
36+
credential_values Json
37+
credential_info Json?
38+
created_at DateTime @default(now()) @map("created_at")
39+
created_by String
40+
updated_at DateTime @default(now()) @updatedAt @map("updated_at")
41+
updated_by String
42+
}
43+
3244
// Models on proxy
3345
model LiteLLM_ProxyModelTable {
3446
model_id String @id @default(uuid())

0 commit comments

Comments
 (0)