Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
76a399b
feat(guardrails): implement team-based isolation guardrails mgmnt (#1…
Harshit28j Feb 3, 2026
04c348e
fixes failure metrics labels (#20152)
Harshit28j Feb 3, 2026
ec279eb
fix: proxy failure cases, now log ip and user agent, key hash, name (…
Harshit28j Feb 3, 2026
b33e1e8
feat(sdk): add proxy_auth for auto OAuth2/JWT token management (#20238)
Chesars Feb 3, 2026
a904c3f
fix(github_copilot): preserve system prompts and auto-inject headers …
Chesars Feb 3, 2026
17c0a88
fix: add missing capability flags to vercel_ai_gateway models (#20276)
krauckbot Feb 3, 2026
b7f0d05
Merge pull request #20337 from BerriAI/main
Sameerlite Feb 3, 2026
cc76f95
fix: check for model_response_choices before guardrail input (#19784)
agrattan0820 Feb 3, 2026
2b25d03
Fix fail-open for grayswan and pass metadata to cygnal api endpoint (…
Reapor-Yurnero Feb 3, 2026
0cb6b58
fix(proxy): forward extra_headers in chat
nehaaprasad Feb 4, 2026
25fa1ad
Merge pull request #20386 from naaa760/fix/extra-head-chat-comp-brid
Sameerlite Feb 4, 2026
7056d99
Custom Code Guardrails UI Playground (#20377)
Feb 4, 2026
49c6785
fix: fix schema.prisma on litellm proxy extras
Feb 4, 2026
ca34b4e
Revert "feat(guardrails): implement team-based isolation guardrails m…
Feb 4, 2026
6b25e37
Revert "fix: proxy failure cases, now log ip and user agent, key hash…
Sameerlite Feb 4, 2026
674593e
Merge pull request #20413 from BerriAI/revert-20145-fix/proxy_failure…
Sameerlite Feb 4, 2026
f8b6af5
Fix test_transform_messages_disable_copilot_system_to_assistant
Sameerlite Feb 4, 2026
54b5c7d
Fix: litellm/tests/test_litellm/proxy/guardrails/guardrail_hooks/test…
Sameerlite Feb 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "LiteLLM_GuardrailsTable" ADD COLUMN "team_id" TEXT;

-- AlterTable
ALTER TABLE "LiteLLM_TeamTable" ADD COLUMN "allow_team_guardrail_config" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "LiteLLM_DeletedTeamTable" ADD COLUMN "allow_team_guardrail_config" BOOLEAN NOT NULL DEFAULT false;
169 changes: 153 additions & 16 deletions litellm/integrations/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,108 @@ async def async_post_call_success_hook(
)
pass

def _safe_get(self, obj: Any, key: str, default: Any = None) -> Any:
"""Get value from dict or Pydantic model."""
if obj is None:
return default
if isinstance(obj, dict):
return obj.get(key, default)
return getattr(obj, key, default)

def _extract_deployment_failure_label_values(
self, request_kwargs: dict
) -> Dict[str, Optional[str]]:
"""
Extract label values for deployment failure metrics from all available
sources in request_kwargs. Falls back to litellm_params metadata and
user_api_key_auth when standard_logging_payload has None values.
"""
standard_logging_payload = (
request_kwargs.get("standard_logging_object", {}) or {}
)
_litellm_params = request_kwargs.get("litellm_params", {}) or {}
_metadata_raw = self._safe_get(standard_logging_payload, "metadata") or {}
if isinstance(_metadata_raw, dict):
_metadata = _metadata_raw
else:
_metadata = {
"user_api_key_alias": getattr(
_metadata_raw, "user_api_key_alias", None
),
"user_api_key_team_id": getattr(
_metadata_raw, "user_api_key_team_id", None
),
"user_api_key_team_alias": getattr(
_metadata_raw, "user_api_key_team_alias", None
),
"user_api_key_hash": getattr(_metadata_raw, "user_api_key_hash", None),
"requester_ip_address": getattr(
_metadata_raw, "requester_ip_address", None
),
"user_agent": getattr(_metadata_raw, "user_agent", None),
}
_litellm_params_metadata = _litellm_params.get("metadata", {}) or {}

# Extract user_api_key_auth if present (proxy injects this, skipped in merge)
user_api_key_auth = _litellm_params_metadata.get("user_api_key_auth")

def _get_api_key_alias() -> Optional[str]:
val = _metadata.get("user_api_key_alias")
if val is not None:
return val
val = _litellm_params_metadata.get("user_api_key_alias")
if val is not None:
return val
if user_api_key_auth is not None:
return getattr(user_api_key_auth, "key_alias", None)
return None

def _get_team_id() -> Optional[str]:
val = _metadata.get("user_api_key_team_id")
if val is not None:
return val
val = _litellm_params_metadata.get("user_api_key_team_id")
if val is not None:
return val
if user_api_key_auth is not None:
return getattr(user_api_key_auth, "team_id", None)
return None

def _get_team_alias() -> Optional[str]:
val = _metadata.get("user_api_key_team_alias")
if val is not None:
return val
val = _litellm_params_metadata.get("user_api_key_team_alias")
if val is not None:
return val
if user_api_key_auth is not None:
return getattr(user_api_key_auth, "team_alias", None)
return None

def _get_hashed_api_key() -> Optional[str]:
val = _metadata.get("user_api_key_hash")
if val is not None:
return val
val = _litellm_params_metadata.get("user_api_key_hash")
if val is not None:
return val
if user_api_key_auth is not None:
return getattr(user_api_key_auth, "api_key", None) or getattr(
user_api_key_auth, "api_key_hash", None
)
return None

return {
"api_key_alias": _get_api_key_alias(),
"team": _get_team_id(),
"team_alias": _get_team_alias(),
"hashed_api_key": _get_hashed_api_key(),
"client_ip": _metadata.get("requester_ip_address")
or _litellm_params_metadata.get("requester_ip_address"),
"user_agent": _metadata.get("user_agent")
or _litellm_params_metadata.get("user_agent"),
}

def set_llm_deployment_failure_metrics(self, request_kwargs: dict):
"""
Sets Failure metrics when an LLM API call fails
Expand All @@ -1707,43 +1809,78 @@ def set_llm_deployment_failure_metrics(self, request_kwargs: dict):
model_id = standard_logging_payload.get("model_id", None)
exception = request_kwargs.get("exception", None)

# Fallback: model_id from litellm_metadata.model_info
if model_id is None:
_model_info = (
(_litellm_params.get("litellm_metadata") or {}).get("model_info")
or (_litellm_params.get("metadata") or {}).get("model_info")
or {}
)
model_id = _model_info.get("id")

# Fallback: model_group from litellm_metadata
if model_group is None:
model_group = (_litellm_params.get("litellm_metadata") or {}).get(
"model_group"
) or (_litellm_params.get("metadata") or {}).get("model_group")

llm_provider = _litellm_params.get("custom_llm_provider", None)

if self._should_skip_metrics_for_invalid_key(
kwargs=request_kwargs,
standard_logging_payload=standard_logging_payload,
):
return
hashed_api_key = standard_logging_payload.get("metadata", {}).get(

# Extract context labels from all available sources (fix for None labels)
fallback_values = self._extract_deployment_failure_label_values(
request_kwargs
)
_metadata = standard_logging_payload.get("metadata", {}) or {}
hashed_api_key = fallback_values.get("hashed_api_key") or _metadata.get(
"user_api_key_hash"
)
api_key_alias = fallback_values.get("api_key_alias") or _metadata.get(
"user_api_key_alias"
)
team = fallback_values.get("team") or _metadata.get("user_api_key_team_id")
team_alias = fallback_values.get("team_alias") or _metadata.get(
"user_api_key_team_alias"
)
client_ip = fallback_values.get("client_ip") or _metadata.get(
"requester_ip_address"
)
user_agent = fallback_values.get("user_agent") or _metadata.get(
"user_agent"
)

# exception_status: prefer status_code, fallback to exception class for known types
exception_status = None
if exception is not None:
exception_status = str(getattr(exception, "status_code", None))
if exception_status == "None" or not exception_status:
code = getattr(exception, "code", None)
if code is not None:
exception_status = str(code)

# Create enum_values for the label factory (always create for use in different metrics)
enum_values = UserAPIKeyLabelValues(
litellm_model_name=litellm_model_name,
model_id=model_id,
api_base=api_base,
api_provider=llm_provider,
exception_status=(
str(getattr(exception, "status_code", None)) if exception else None
),
exception_status=exception_status,
exception_class=(
self._get_exception_class_name(exception) if exception else None
),
requested_model=model_group,
requested_model=model_group or litellm_model_name,
hashed_api_key=hashed_api_key,
api_key_alias=standard_logging_payload["metadata"][
"user_api_key_alias"
],
team=standard_logging_payload["metadata"]["user_api_key_team_id"],
team_alias=standard_logging_payload["metadata"][
"user_api_key_team_alias"
],
api_key_alias=api_key_alias,
team=team,
team_alias=team_alias,
tags=standard_logging_payload.get("request_tags", []),
client_ip=standard_logging_payload["metadata"].get(
"requester_ip_address"
),
user_agent=standard_logging_payload["metadata"].get("user_agent"),
client_ip=client_ip,
user_agent=user_agent,
)

"""
Expand Down
10 changes: 9 additions & 1 deletion litellm/proxy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ class LiteLLMRoutes(enum.Enum):
"/v1/vector_stores/{vector_store_id}/files/{file_id}/content",
"/vector_store/list",
"/v1/vector_store/list",

# search
"/search",
"/v1/search",
Expand Down Expand Up @@ -630,6 +629,9 @@ class LiteLLMRoutes(enum.Enum):
"/model/{model_id}/update",
"/prompt/list",
"/prompt/info",
"/guardrails",
"/guardrails/{guardrail_id}",
"/v2/guardrails/list",
] # routes that manage their own allowed/disallowed logic

## Org Admin Routes ##
Expand Down Expand Up @@ -1477,6 +1479,9 @@ class TeamBase(LiteLLMPydanticObjectBase):
members: list = []
members_with_roles: List[Member] = []
team_member_permissions: Optional[List[str]] = None
allow_team_guardrail_config: Optional[
bool
] = None # if True, team admin can configure guardrails for this team
metadata: Optional[dict] = None
tpm_limit: Optional[int] = None
rpm_limit: Optional[int] = None
Expand Down Expand Up @@ -1574,6 +1579,9 @@ class UpdateTeamRequest(LiteLLMPydanticObjectBase):
model_tpm_limit: Optional[Dict[str, int]] = None
allowed_vector_store_indexes: Optional[List[AllowedVectorStoreIndexItem]] = None
router_settings: Optional[dict] = None
allow_team_guardrail_config: Optional[
bool
] = None # if True, team admin can configure guardrails for this team


class ResetTeamBudgetRequest(LiteLLMPydanticObjectBase):
Expand Down
60 changes: 58 additions & 2 deletions litellm/proxy/auth/login_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@
from litellm.types.proxy.ui_sso import ReturnedUITokenObject


async def expire_previous_ui_session_tokens(
user_id: str, prisma_client: Optional[PrismaClient]
) -> None:
"""
Expire (block) all other valid UI session tokens for a user.

This prevents accumulation of multiple valid UI session tokens that
are supposed to be short-lived test keys. Only affects keys with
team_id = "litellm-dashboard" and that haven't expired yet.

Args:
user_id: The user ID whose previous UI session tokens should be expired
prisma_client: Database client for performing the update
"""
if prisma_client is None:
return

try:
from datetime import datetime, timezone

current_time = datetime.now(timezone.utc)

# Find all unblocked AND non-expired UI session tokens for this user
ui_session_tokens = await prisma_client.db.litellm_verificationtoken.find_many(
where={
"user_id": user_id,
"team_id": "litellm-dashboard",
"OR": [
{"blocked": None}, # Tokens that have never been blocked (null)
{"blocked": False}, # Tokens explicitly set to not blocked
],
"expires": {"gt": current_time}, # Only get tokens that haven't expired
}
)

if not ui_session_tokens:
return

# Block all the found tokens
tokens_to_block = [token.token for token in ui_session_tokens if token.token]

if tokens_to_block:
await prisma_client.db.litellm_verificationtoken.update_many(
where={"token": {"in": tokens_to_block}}, data={"blocked": True}
)

except Exception:
# Silently fail - don't block login if cleanup fails
# This is a best-effort operation
pass


def get_ui_credentials(master_key: Optional[str]) -> tuple[str, str]:
"""
Get UI username and password from environment variables or master key.
Expand Down Expand Up @@ -245,6 +297,11 @@ async def authenticate_user( # noqa: PLR0915
)
user_email = getattr(_user_row, "user_email", "unknown")
_password = getattr(_user_row, "password", "unknown")
user_team_id = getattr(_user_row, "team_id", "litellm-dashboard")

# if user_team_id is None, set it to "litellm-dashboard"
if user_team_id is None:
user_team_id = "litellm-dashboard"

if _password is None:
raise ProxyException(
Expand All @@ -271,7 +328,7 @@ async def authenticate_user( # noqa: PLR0915
"config": {},
"spend": 0,
"user_id": user_id,
"team_id": "litellm-dashboard",
"team_id": user_team_id,
},
)
else:
Expand Down Expand Up @@ -340,4 +397,3 @@ def create_ui_token_object(
disabled_non_admin_personal_key_creation=disabled_non_admin_personal_key_creation,
server_root_path=get_server_root_path(),
)

Loading
Loading