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 lint and ruff

Sameer Kankute94162916a81e
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 0668880f908..1c286b7f2e4 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -10285,74 +10285,63 @@ def _paginate_models_response(
}
-async def _filter_models_by_team_id(
- all_models: List[Dict[str, Any]],
- team_id: str,
- prisma_client: PrismaClient,
- llm_router: Router,
-) -> List[Dict[str, Any]]:
- """
- Filter models by team ID. Returns models where:
- - direct_access is True, OR
- - team_id is in access_via_team_ids
-
- Also searches config and database for models accessible to the team.
+def _team_models_resolve_to_names(
+ team_models: List[str], access_groups: Dict[str, Any]
+) -> List[str]:
+ """Expand team model entries (including access group names) to concrete model names."""
+ resolved: List[str] = []
+ for name in team_models:
+ if name in access_groups:
+ resolved.extend(access_groups[name])
+ else:
+ resolved.append(name)
+ return resolved
- Args:
- all_models: List of models to filter
- team_id: Team ID to filter by
- prisma_client: Prisma client for database queries
- llm_router: Router instance for config queries
- Returns:
- Filtered list of models
- """
- # Get team from database
+async def _load_team_object_for_model_filter(
+ team_id: str, prisma_client: PrismaClient
+) -> Optional[LiteLLM_TeamTable]:
+ """Load team row from DB; returns None if missing or on error."""
try:
team_db_object = await prisma_client.db.litellm_teamtable.find_unique(
where={"team_id": team_id}
)
if team_db_object is None:
verbose_proxy_logger.warning(f"Team {team_id} not found in database")
- # If team doesn't exist, return empty list
- return []
-
- team_object = LiteLLM_TeamTable(**team_db_object.model_dump())
+ return None
+ return LiteLLM_TeamTable(**team_db_object.model_dump())
except Exception as e:
verbose_proxy_logger.exception(f"Error fetching team {team_id}: {str(e)}")
- return []
+ return None
+
- # Get models accessible to this team (similar to _add_team_models_to_all_models)
+async def _gather_team_accessible_model_ids(
+ team_object: LiteLLM_TeamTable,
+ team_id: str,
+ prisma_client: PrismaClient,
+ llm_router: Router,
+) -> Set[str]:
+ """Collect model IDs the team can use from router config and DB."""
team_accessible_model_ids: Set[str] = set()
access_groups = llm_router.get_model_access_groups() if llm_router else {}
if (
- not team_object.models # empty list = all model access
+ not team_object.models
or SpecialModelNames.all_proxy_models.value in team_object.models
):
- # Team has access to all models
model_list = llm_router.get_model_list() if llm_router else []
if model_list is not None:
for model in model_list:
model_id = model.get("model_info", {}).get("id", None)
if model_id is None:
continue
- # if team model id set, check if team id matches
team_model_id = model.get("model_info", {}).get("team_id", None)
- can_add_model = False
- if team_model_id is None:
- can_add_model = True
- elif team_model_id == team_id:
- can_add_model = True
-
- if can_add_model:
+ if team_model_id is None or team_model_id == team_id:
team_accessible_model_ids.add(model_id)
else:
- # Team has access to specific models — resolve access group names first
resolved_model_names: Set[str] = set()
for model_name in team_object.models:
if model_name in access_groups:
- # This is an access group name — expand to member model names
resolved_model_names.update(access_groups[model_name])
else:
resolved_model_names.add(model_name)
@@ -10369,33 +10358,58 @@ async def _filter_models_by_team_id(
if model_id is not None:
team_accessible_model_ids.add(model_id)
- # Also search database for models accessible to this team
- # This complements the config search done above
try:
if (
team_object.models
and SpecialModelNames.all_proxy_models.value not in team_object.models
):
- # Team has specific models - check database for those model names
- # Resolve access group names to actual model names for the DB query
- _resolved_names: list[str] = []
- for _m in team_object.models:
- if _m in access_groups:
- _resolved_names.extend(access_groups[_m])
- else:
- _resolved_names.append(_m)
+ _resolved_names = _team_models_resolve_to_names(
+ team_object.models, access_groups
+ )
db_models = await prisma_client.db.litellm_proxymodeltable.find_many(
where={"model_name": {"in": _resolved_names}}
)
for db_model in db_models:
- model_id = db_model.model_id
- if model_id:
- team_accessible_model_ids.add(model_id)
+ if db_model.model_id:
+ team_accessible_model_ids.add(db_model.model_id)
except Exception as e:
verbose_proxy_logger.debug(
f"Error querying database models for team {team_id}: {str(e)}"
)
+ return team_accessible_model_ids
+
+
+async def _filter_models_by_team_id(
+ all_models: List[Dict[str, Any]],
+ team_id: str,
+ prisma_client: PrismaClient,
+ llm_router: Router,
+) -> List[Dict[str, Any]]:
+ """
+ Filter models by team ID. Returns models where:
+ - direct_access is True, OR
+ - team_id is in access_via_team_ids
+
+ Also searches config and database for models accessible to the team.
+
+ Args:
+ all_models: List of models to filter
+ team_id: Team ID to filter by
+ prisma_client: Prisma client for database queries
+ llm_router: Router instance for config queries
+
+ Returns:
+ Filtered list of models
+ """
+ team_object = await _load_team_object_for_model_filter(team_id, prisma_client)
+ if team_object is None:
+ return []
+
+ team_accessible_model_ids = await _gather_team_accessible_model_ids(
+ team_object, team_id, prisma_client, llm_router
+ )
+
# Filter models based on direct_access or access_via_team_ids
# Models are already enriched with these fields before this function is called
filtered_models = []
diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py
index 1902a347215..da8da1b486f 100644
--- a/litellm/responses/streaming_iterator.py
+++ b/litellm/responses/streaming_iterator.py
@@ -25,6 +25,7 @@ from litellm.litellm_core_utils.llm_response_utils.response_metadata import (
from litellm.litellm_core_utils.thread_pool_executor import executor
from litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfig
from litellm.responses.utils import ResponsesAPIRequestUtils
+from litellm.types.llms.openai import ResponsesAPIStreamEvents
from litellm.types.utils import CallTypes
from litellm.utils import CustomStreamWrapper, async_post_call_success_deployment_hook