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]

fix(key_management_endpoints.py): check if key is a hashed token or sk key before lookup

Krrish Dholakia0f6898ad0a9d
diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py
index bd8faf34be8..a7f15cc6c19 100644
--- a/litellm/proxy/management_endpoints/key_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py
@@ -346,6 +346,7 @@ def handle_key_type(data: GenerateKeyRequest, data_json: dict) -> dict:
data_json["allowed_routes"] = ["info_routes"]
return data_json
+
async def validate_team_id_used_in_service_account_request(
team_id: Optional[str],
prisma_client: Optional[PrismaClient],
@@ -358,13 +359,13 @@ async def validate_team_id_used_in_service_account_request(
status_code=400,
detail="team_id is required for service account keys. Please specify `team_id` in the request body.",
)
-
+
if prisma_client is None:
raise HTTPException(
status_code=400,
detail="prisma_client is required for service account keys. Please specify `prisma_client` in the request body.",
)
-
+
# check if team_id exists in the database
team = await prisma_client.db.litellm_teamtable.find_unique(
where={"team_id": team_id},
@@ -376,6 +377,7 @@ async def validate_team_id_used_in_service_account_request(
)
return True
+
async def _common_key_generation_helper( # noqa: PLR0915
data: GenerateKeyRequest,
user_api_key_dict: UserAPIKeyAuth,
@@ -557,7 +559,7 @@ async def _common_key_generation_helper( # noqa: PLR0915
status_code=400,
detail={
"error": f"Invalid key format. LiteLLM Virtual Key must start with 'sk-'. Received: {data.key}"
- }
+ },
)
response = await generate_key_helper_fn(
@@ -2885,7 +2887,10 @@ async def unblock_key(
param="key",
code=status.HTTP_400_BAD_REQUEST,
)
- hashed_token = hash_token(token=data.key)
+ if data.key.startswith("sk-"):
+ hashed_token = hash_token(token=data.key)
+ else:
+ hashed_token = data.key
if litellm.store_audit_logs is True:
# make an audit log for key update