diff --git a/litellm/batches/batch_utils.py b/litellm/batches/batch_utils.py index 246ac4fd369..9974e77d017 100644 --- a/litellm/batches/batch_utils.py +++ b/litellm/batches/batch_utils.py @@ -43,7 +43,7 @@ def _uses_native_vertex_output( ) -> bool: if custom_llm_provider != "vertex_ai": return False - if model_name and getattr(litellm, "disable_vertex_batch_output_transformation", False): + if model_name and litellm.disable_vertex_batch_output_transformation: return True return first_row is not None and is_native_vertex_batch_output_row(first_row) diff --git a/litellm/integrations/langfuse/langfuse_sdk.py b/litellm/integrations/langfuse/langfuse_sdk.py index 66819c95ebf..986f35297d2 100644 --- a/litellm/integrations/langfuse/langfuse_sdk.py +++ b/litellm/integrations/langfuse/langfuse_sdk.py @@ -684,7 +684,7 @@ class LangfuseSpanExporter(SpanExporter): def _round(self, halving: _Halving) -> _Halving: sent: Final = tuple((batch, self._send_batch(batch)) for batch in halving.pending) return _Halving( - pending=tuple(part for batch, outcome in sent if outcome == "too_large" for part in _smaller(batch)), + pending=tuple(chain.from_iterable(_smaller(batch) for batch, outcome in sent if outcome == "too_large")), settled=halving.settled + tuple( SpanExportResult.SUCCESS if outcome == "delivered" else SpanExportResult.FAILURE diff --git a/litellm/llms/openai/organization_costs.py b/litellm/llms/openai/organization_costs.py index e7fb22f9b19..856072ddb99 100644 --- a/litellm/llms/openai/organization_costs.py +++ b/litellm/llms/openai/organization_costs.py @@ -3,6 +3,7 @@ from collections.abc import Awaitable, Callable, Mapping, Sequence from dataclasses import dataclass from datetime import date, datetime, timedelta, timezone +from itertools import chain from types import MappingProxyType from typing import Final, Literal, TypeAlias @@ -126,7 +127,8 @@ async def fetch_openai_daily_costs( return MappingProxyType( { day: sum( - result.amount.value for bucket in buckets if _bucket_day(bucket) == day for result in bucket.results + result.amount.value + for result in chain.from_iterable(bucket.results for bucket in buckets if _bucket_day(bucket) == day) ) for day in days } diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py index 6baa695433c..8befc99cad4 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py @@ -6855,12 +6855,13 @@ class MCPServerManager: if not tool_permissions: return {} expanded: Final = tuple( - (server_id, tuple(tools or ())) - for key, tools in tool_permissions.items() - for server_id in self.expand_permission_list([key]) + chain.from_iterable( + ((server_id, tuple(tools or ())) for server_id in self.expand_permission_list([key])) + for key, tools in tool_permissions.items() + ) ) return { - server_id: list(dict.fromkeys(tool for _, tools in group for tool in tools)) + server_id: list(dict.fromkeys(chain.from_iterable(tools for _, tools in group))) for server_id, group in groupby(sorted(expanded, key=itemgetter(0)), key=itemgetter(0)) } diff --git a/litellm/proxy/common_utils/model_listing_utils.py b/litellm/proxy/common_utils/model_listing_utils.py index 8958fb20918..0c702ac4139 100644 --- a/litellm/proxy/common_utils/model_listing_utils.py +++ b/litellm/proxy/common_utils/model_listing_utils.py @@ -14,6 +14,7 @@ import re from collections.abc import Container, Mapping, Sequence from dataclasses import dataclass from functools import reduce +from itertools import chain from types import MappingProxyType from typing import TYPE_CHECKING, Final, cast @@ -191,7 +192,7 @@ def alias_map(aliases: object) -> Mapping[str, str]: def _alias_names(alias_maps: Sequence[Mapping[str, str]]) -> tuple[str, ...]: - return tuple(dict.fromkeys(alias for aliases in alias_maps for alias in aliases)) + return tuple(dict.fromkeys(chain.from_iterable(alias_maps))) def _rewrite(model_id: str, alias_maps: Sequence[Mapping[str, str]]) -> str | None: diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index e097debde77..0178465739b 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -474,7 +474,9 @@ class _ProxyDBLogger(CustomLogger): spend_log_error("Error in tracking cost callback - %s", str(e), exc=e) @staticmethod - async def _enrich_failure_metadata_unless_db_stalled(metadata: dict, original_exception: Exception) -> dict: + async def _enrich_failure_metadata_unless_db_stalled( + metadata: dict[str, object], original_exception: Exception + ) -> dict[str, object]: if isinstance(original_exception, DBLookupDeadlineExceeded): return metadata return await _ProxyDBLogger._enrich_failure_metadata_with_key_info(metadata=metadata) diff --git a/litellm/proxy/spend_tracking/key_metadata_recovery.py b/litellm/proxy/spend_tracking/key_metadata_recovery.py index 08e8fff8f1d..965cded59c4 100644 --- a/litellm/proxy/spend_tracking/key_metadata_recovery.py +++ b/litellm/proxy/spend_tracking/key_metadata_recovery.py @@ -215,17 +215,23 @@ def _meta_with_user_details( return updated +def _user_id_needing_details(api_key: str, meta: KeyMetadataDict) -> str | None: + user_id: Final = meta.get("user_id") + if not isinstance(user_id, str) or not user_id: + return None + if meta.get("user_email") and not (_is_cli_session_key(api_key) and not meta.get("team_id")): + return None + return user_id + + async def attach_user_details( prisma_client: PrismaClient, recovered: Mapping[str, KeyMetadataDict], ) -> Mapping[str, KeyMetadataDict]: needing_details: Final = frozenset( user_id - for api_key, meta in recovered.items() - for user_id in (meta.get("user_id"),) - if isinstance(user_id, str) - and user_id - and (not meta.get("user_email") or (_is_cli_session_key(api_key) and not meta.get("team_id"))) + for user_id in (_user_id_needing_details(api_key, meta) for api_key, meta in recovered.items()) + if user_id is not None ) details: Final = await _details_for_user_ids(prisma_client, needing_details) if not details: diff --git a/litellm/types/litellm_params.py b/litellm/types/litellm_params.py index 83a42c235f9..f5ba9ebd3da 100644 --- a/litellm/types/litellm_params.py +++ b/litellm/types/litellm_params.py @@ -3,6 +3,7 @@ models and KWARG_ARTIFACTS into all_litellm_params.""" from collections.abc import Callable, Iterator, Mapping, MutableMapping, Sequence from dataclasses import dataclass, field, fields, is_dataclass +from itertools import chain from types import MappingProxyType from typing import TYPE_CHECKING, Final, Literal, TypeAlias @@ -359,6 +360,6 @@ def owned_wire_names(root: type) -> tuple[str, ...]: return tuple(names()) -OWNED_KWARG_NAMES: Final = tuple(name for root in LITELLM_OWNED_ROOTS for name in owned_wire_names(root)) +OWNED_KWARG_NAMES: Final = tuple(chain.from_iterable(owned_wire_names(root) for root in LITELLM_OWNED_ROOTS)) AGENTIC_LOOP_KWARG_NAMES: Final = (*wire_names(AgenticLoopState), *wire_names(AgenticLoopOptions)) BEDROCK_BATCH_KWARG_NAMES: Final = wire_names(BedrockBatchConnection)