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]

chore: merge main into litellm_org_alias_from_team

jesus83b5f6880118
diff --cc litellm/proxy/auth/user_api_key_auth.py
index df5257908ce,4cbd4213463..ac1cc3cfb35
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@@ -54,8 -57,8 +57,9 @@@ from litellm.proxy.auth.auth_checks imp
get_end_user_object,
get_jwt_key_mapping_object,
get_object_permission,
+ get_org_object,
get_project_object,
+ get_team_membership,
get_team_object,
get_user_object,
is_valid_fallback_model,
@@@ -2399,52 -2602,13 +2603,59 @@@ def _token_can_vouch_for_team(valid_tok
return PrismaDBExceptionHandler.should_allow_request_on_db_unavailable()
+async def _inherit_org_identity(
+ user_api_key_auth_obj: UserAPIKeyAuth,
+ team_object: LiteLLM_TeamTableCachedObj | None,
+ prisma_client: PrismaClient | None,
+ user_api_key_cache: UserApiKeyCache,
+ parent_otel_span: Span | None,
+ proxy_logging_obj: ProxyLogging | None,
+) -> None:
+ if user_api_key_auth_obj.org_id is None and team_object is not None and team_object.organization_id is not None:
+ user_api_key_auth_obj.org_id = team_object.organization_id
+ already_populated: Final = any(
+ value is not None
+ for value in (
+ user_api_key_auth_obj.organization_alias,
+ user_api_key_auth_obj.organization_max_budget,
+ user_api_key_auth_obj.organization_tpm_limit,
+ user_api_key_auth_obj.organization_rpm_limit,
+ user_api_key_auth_obj.organization_metadata,
+ )
+ )
+ if user_api_key_auth_obj.org_id is None or already_populated or prisma_client is None:
+ return
+ try:
+ org_object: Final = await get_org_object(
+ org_id=user_api_key_auth_obj.org_id,
+ prisma_client=prisma_client,
+ user_api_key_cache=user_api_key_cache,
+ parent_otel_span=parent_otel_span,
+ proxy_logging_obj=proxy_logging_obj,
+ include_budget_table=True,
+ )
+ except Exception:
+ verbose_proxy_logger.debug("org lookup failed for org_id=%s", user_api_key_auth_obj.org_id, exc_info=True)
+ return
+ if org_object is None:
+ return
+ user_api_key_auth_obj.organization_alias = org_object.organization_alias
+ user_api_key_auth_obj.organization_metadata = org_object.metadata
+ budget: Final = org_object.litellm_budget_table
+ if budget is None:
+ return
+ user_api_key_auth_obj.organization_max_budget = budget.max_budget
+ user_api_key_auth_obj.organization_tpm_limit = budget.tpm_limit
+ user_api_key_auth_obj.organization_rpm_limit = budget.rpm_limit
+
+
+ def is_no_auth_dev_mode(master_key: str | None, general_settings: Mapping[str, object]) -> bool:
+ return master_key is None and not any(
+ general_settings.get(flag, False)
+ for flag in ("enable_jwt_auth", "enable_oauth2_auth", "enable_oauth2_proxy_auth")
+ )
+
+
@tracer.wrap()
async def _run_centralized_common_checks(
user_api_key_auth_obj: UserAPIKeyAuth,
@@@ -2668,15 -2829,14 +2876,20 @@@
None if isinstance(end_user_result, BaseException) else end_user_result
)
global_proxy_spend: float | None = None if isinstance(global_spend_result, BaseException) else global_spend_result
+ carry_team_and_user_budget_state(
+ valid_token=user_api_key_auth_obj,
+ team_object=team_object,
+ user_object=user_object,
+ )
- if user_api_key_auth_obj.org_id is None and team_object is not None and team_object.organization_id is not None:
- user_api_key_auth_obj.org_id = team_object.organization_id
+ await _inherit_org_identity(
+ user_api_key_auth_obj=user_api_key_auth_obj,
+ team_object=cast(LiteLLM_TeamTableCachedObj | None, team_object),
+ prisma_client=prisma_client,
+ user_api_key_cache=user_api_key_cache,
+ parent_otel_span=parent_otel_span,
+ proxy_logging_obj=proxy_logging_obj,
+ )
# common_checks identifies admin via user_object, not the token
# (non_proxy_admin_allowed_routes_check). JWT admin shortcut and
diff --cc tests/test_litellm/proxy/auth/test_user_api_key_auth.py
index fd9b6f09678,ba3e98ee718..b5200de115e
--- a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py
+++ b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py
@@@ -23,7 -24,7 +24,8 @@@ from litellm.proxy._types import
LiteLLM_JWTAuth,
LiteLLM_BudgetTable,
LiteLLM_EndUserTable,
+ LiteLLM_OrganizationTable,
+ LiteLLM_TeamTableCachedObj,
LiteLLM_UserTable,
LitellmUserRoles,
ProxyErrorTypes,
@@@ -32,8 -33,15 +34,16 @@@
JWTRoutingOverride,
)
from litellm.proxy.auth.handle_jwt import JWTHandler
- from litellm.proxy.auth.auth_checks import OrganizationNotFoundError, get_key_object, _cache_key_object
+ from litellm.proxy.auth.auth_checks import (
++ OrganizationNotFoundError,
+ TeamNotFoundError,
+ UserNotFoundError,
+ get_key_object,
+ _cache_key_object,
+ jwt_key_mapping_cache_key,
+ )
from litellm.proxy.auth.route_checks import RouteChecks
+ from litellm.proxy.common_utils.http_parsing_utils import get_client_requested_model
from litellm.proxy.auth.user_api_key_auth import (
_check_key_model_budget_with_fallback,
_ensure_litellm_received_at_on_request_state,