From 7f95b5f3615b383ae08551156ddfad6af87acfcf Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:15:12 -0700 Subject: [PATCH] refactor: clean up fresh tech debt from 2026-09-28 (#43674) * refactor: clean up fresh tech debt from 2026-09-28 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: group leaderboard rows in one pass and wrap docstring at 120 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../mcp_server/tool_catalog_guard.py | 16 ++++++------- litellm/proxy/auth/auth_checks.py | 7 +++--- litellm/proxy/db/model_usage_rollup.py | 16 ++++++++----- .../model_insights_endpoints.py | 24 ++++++++++++------- .../rust_bridge/callbacks_legacy_python.py | 13 ++++++---- .../test_model_insights_endpoints.py | 2 +- 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/litellm/proxy/_experimental/mcp_server/tool_catalog_guard.py b/litellm/proxy/_experimental/mcp_server/tool_catalog_guard.py index 58227640fba..b3c70a33d0e 100644 --- a/litellm/proxy/_experimental/mcp_server/tool_catalog_guard.py +++ b/litellm/proxy/_experimental/mcp_server/tool_catalog_guard.py @@ -39,10 +39,10 @@ class _ScanKwargs(TypedDict): server_name: ReadOnly[str] mcp_rate_limit_server_name: ReadOnly[str] user_api_key_auth: ReadOnly[UserAPIKeyAuth | None] - user_api_key_user_id: ReadOnly[object] - user_api_key_team_id: ReadOnly[object] - user_api_key_end_user_id: ReadOnly[object] - user_api_key_hash: ReadOnly[object] + user_api_key_user_id: ReadOnly[str | None] + user_api_key_team_id: ReadOnly[str | None] + user_api_key_end_user_id: ReadOnly[str | None] + user_api_key_hash: ReadOnly[str | None] headers: ReadOnly[Mapping[str, str]] mcp_tool_description: ReadOnly[str] mcp_input_schema: ReadOnly[Mapping[str, object]] @@ -208,10 +208,10 @@ async def _guarded_catalog_entry( "server_name": server.name, "mcp_rate_limit_server_name": server.alias or server.server_name or server.name, "user_api_key_auth": user_api_key_auth, - "user_api_key_user_id": getattr(user_api_key_auth, "user_id", None), - "user_api_key_team_id": getattr(user_api_key_auth, "team_id", None), - "user_api_key_end_user_id": getattr(user_api_key_auth, "end_user_id", None), - "user_api_key_hash": getattr(user_api_key_auth, "api_key", None), + "user_api_key_user_id": user_api_key_auth.user_id if user_api_key_auth else None, + "user_api_key_team_id": user_api_key_auth.team_id if user_api_key_auth else None, + "user_api_key_end_user_id": user_api_key_auth.end_user_id if user_api_key_auth else None, + "user_api_key_hash": user_api_key_auth.api_key if user_api_key_auth else None, "headers": logging_safe_mcp_headers(raw_headers), "mcp_tool_description": tool.description or "", "mcp_input_schema": tool.input_schema, diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 51cc70c010b..8fbeaf18460 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -591,10 +591,9 @@ def _is_cost_explicitly_configured(model: str, llm_router: "Router") -> bool: The group is resolved through ``Router.get_model_list()``, the same resolution ``get_model_group_info()`` applies when the caller reads the cost a few lines earlier, so the - two lookups cannot disagree: names defined in ``Router.model_group_alias`` are not - ``model_name`` entries in ``Router.model_list``, and scanning that list by exact name reported - every aliased group as unconfigured. It also reaches a deployment that prices itself through - its ``model_info`` block, whose entry lands in the cost map under the deployment id. + two lookups cannot disagree, including for names defined in ``Router.model_group_alias``. + It also reaches a deployment that prices itself through its ``model_info`` block, whose entry + lands in the cost map under the deployment id. """ for deployment in llm_router.get_model_list(model_name=model) or (): model_id = (deployment.get("model_info") or _EMPTY_COST_ENTRY).get("id") diff --git a/litellm/proxy/db/model_usage_rollup.py b/litellm/proxy/db/model_usage_rollup.py index 808c3528051..acd9130da30 100644 --- a/litellm/proxy/db/model_usage_rollup.py +++ b/litellm/proxy/db/model_usage_rollup.py @@ -22,12 +22,16 @@ def model_usage_task_type(request_tags: str) -> str: tags: Final = _TAGS.validate_json(request_tags) except ValidationError: return MODEL_INSIGHTS_DEFAULT_TASK - for tag in tags: - if isinstance(tag, str) and tag.startswith(MODEL_INSIGHTS_TASK_TAG_PREFIX): - task = tag.removeprefix(MODEL_INSIGHTS_TASK_TAG_PREFIX) - if task in load_model_insight_tasks(): - return task - return MODEL_INSIGHTS_DEFAULT_TASK + return next( + ( + task + for tag in tags + if isinstance(tag, str) + and tag.startswith(MODEL_INSIGHTS_TASK_TAG_PREFIX) + and (task := tag.removeprefix(MODEL_INSIGHTS_TASK_TAG_PREFIX)) in load_model_insight_tasks() + ), + MODEL_INSIGHTS_DEFAULT_TASK, + ) def _is_internal_call(metadata: str) -> bool: diff --git a/litellm/proxy/management_endpoints/model_insights_endpoints.py b/litellm/proxy/management_endpoints/model_insights_endpoints.py index dbdaa59d7d4..0c6c7d1227d 100644 --- a/litellm/proxy/management_endpoints/model_insights_endpoints.py +++ b/litellm/proxy/management_endpoints/model_insights_endpoints.py @@ -1,3 +1,5 @@ +import functools +import itertools from collections.abc import Mapping from datetime import date, datetime, timedelta, timezone from typing import Annotated, Final @@ -111,14 +113,20 @@ def _daily_metric(row: _GroupedDaily) -> ModelInsightDailyMetric: def _summarize_tasks(rows: list[_GroupedTask], metric: ModelInsightsMetric) -> list[ModelInsightTaskSummary]: catalog: Final = load_model_insight_tasks() - totals: Final[dict[str, float]] = {} - leaders: Final[dict[str, _GroupedTask]] = {} - for row in rows: - value = _rank_value(row, metric) - totals[row.task_type] = totals.get(row.task_type, 0.0) + value - leader = leaders.get(row.task_type) - if leader is None or value > _rank_value(leader, metric): - leaders[row.task_type] = row + first_seen: Final = {task: index for index, task in enumerate(dict.fromkeys(row.task_type for row in rows))} + by_task: Final = { + task: tuple(group) + for task, group in itertools.groupby( + sorted(rows, key=lambda row: first_seen[row.task_type]), key=lambda row: row.task_type + ) + } + totals: Final = { + task: functools.reduce(lambda total, row: total + _rank_value(row, metric), task_rows, 0.0) + for task, task_rows in by_task.items() + } + leaders: Final = { + task: max(task_rows, key=lambda row: _rank_value(row, metric)) for task, task_rows in by_task.items() + } grand: Final = sum(totals.values()) return [ ModelInsightTaskSummary( diff --git a/litellm/rust_bridge/callbacks_legacy_python.py b/litellm/rust_bridge/callbacks_legacy_python.py index 4f1f2b3c9fd..25513666c43 100644 --- a/litellm/rust_bridge/callbacks_legacy_python.py +++ b/litellm/rust_bridge/callbacks_legacy_python.py @@ -16,6 +16,7 @@ from dataclasses import dataclass from typing import ( TYPE_CHECKING, Final, + Literal, Protocol, cast, # noqa: TID251 # bounded compatibility calls into legacy Python integrations ) @@ -231,6 +232,10 @@ def defer_success(logger: LoggingSurface, pending: object) -> None: setattr(logger, "_native_pending_logging", pending) +def _cache_hit(logger: LoggingSurface) -> Literal[True] | None: + return True if logger.model_call_details.get("cache_hit") is True else None + + def sync_success_for_async_call( logger: LoggingSurface, response: object, start: datetime.datetime, end: datetime.datetime ) -> None: @@ -238,7 +243,7 @@ def sync_success_for_async_call( result=response, start_time=start, end_time=end, - cache_hit=True if logger.model_call_details.get("cache_hit") is True else None, + cache_hit=_cache_hit(logger), ) @@ -265,16 +270,14 @@ def submit_success(logger: LoggingSurface, response: object, start: datetime.dat response, start, end, - cache_hit=True if logger.model_call_details.get("cache_hit") is True else None, + cache_hit=_cache_hit(logger), ) def async_success_handler( logger: LoggingSurface, response: object, start: datetime.datetime, end: datetime.datetime ) -> Coroutine[object, object, None]: - return logger.async_success_handler( - response, start, end, cache_hit=True if logger.model_call_details.get("cache_hit") is True else None - ) + return logger.async_success_handler(response, start, end, cache_hit=_cache_hit(logger)) def enqueue_logging(coroutine: Coroutine[object, object, None]) -> None: diff --git a/tests/test_litellm/proxy/management_endpoints/test_model_insights_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_model_insights_endpoints.py index af58c2d9884..2cb66771e72 100644 --- a/tests/test_litellm/proxy/management_endpoints/test_model_insights_endpoints.py +++ b/tests/test_litellm/proxy/management_endpoints/test_model_insights_endpoints.py @@ -122,7 +122,7 @@ def test_model_insights_scopes_daily_to_ranked_deployments() -> None: def _task_rows() -> list[dict[str, object]]: def row(task: str, group: str, requests: str, spend: float) -> dict[str, object]: base = _grouped_row(task_type=task, model_group=group, model=group, custom_llm_provider="openai") - base["_sum"].update({"request_count": requests, "spend": spend}) # type: ignore[union-attr] + base["_sum"].update({"request_count": requests, "spend": spend}) return base return [