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(proxy): ignore team_id="" on /key/update so team-less keys can be updated and imported (#40421)

devin-ai-integration[bot]7c6e33ef7088
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index df7ee8f508c..0e2fd50ac9f 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -3,6 +3,7 @@ import json
import os
from collections.abc import Callable, Mapping
from datetime import datetime
+from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final, Literal, NamedTuple
import httpx
@@ -1294,6 +1295,13 @@ class UpdateKeyRequest(KeyRequestBase):
rotation_interval: str | None = None
organization_id: str | None = None
+ @model_validator(mode="before")
+ @classmethod
+ def drop_blank_team_id(cls, values: object) -> object:
+ if isinstance(values, Mapping) and values.get("team_id") == "":
+ return MappingProxyType({k: v for k, v in values.items() if k != "team_id"})
+ return values
+
@field_validator("organization_id", mode="before")
@classmethod
def treat_cleared_organization_id_as_unset(cls, v: object) -> object:
diff --git a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
index a873a367eab..d2aeba18f7d 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
@@ -17975,6 +17975,32 @@ def test_key_request_blank_organization_id_is_unset():
assert UpdateKeyRequest(key="sk-1", organization_id="org-1").organization_id == "org-1"
+def test_update_key_request_blank_team_id_is_not_a_team_change():
+ from litellm.proxy._types import UpdateKeyRequest
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ is_different_team,
+ )
+
+ blank = UpdateKeyRequest(key="sk-1", team_id="", key_alias="renamed")
+ assert blank.team_id is None
+ assert "team_id" not in blank.model_dump(exclude_unset=True)
+ assert blank.model_dump(exclude_unset=True) == {"key": "sk-1", "key_alias": "renamed"}
+ assert is_different_team(data=blank, existing_key_row=LiteLLM_VerificationToken(token="hashed")) is False
+ assert (
+ is_different_team(data=blank, existing_key_row=LiteLLM_VerificationToken(token="hashed", team_id="team-1"))
+ is False
+ )
+ assert "team_id" in UpdateKeyRequest(key="sk-1", team_id=None).model_dump(exclude_unset=True)
+ assert UpdateKeyRequest(key="sk-1", team_id="team-1").team_id == "team-1"
+ assert (
+ is_different_team(
+ data=UpdateKeyRequest(key="sk-1", team_id="team-1"),
+ existing_key_row=LiteLLM_VerificationToken(token="hashed"),
+ )
+ is True
+ )
+
+
def test_key_generation_check_blank_team_id_uses_personal_permissions(monkeypatch):
"""key_generation_check with team_id="" must take the personal-key path instead
of failing the team lookup with "Unable to find team object" (LIT-3925)."""