diff --git a/litellm/__init__.py b/litellm/__init__.py index d4418c661a3..0840c6312d7 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -167,12 +167,12 @@ prometheus_initialize_budget_metrics: Optional[bool] = False require_auth_for_metrics_endpoint: Optional[bool] = False argilla_batch_size: Optional[int] = None datadog_use_v1: Optional[bool] = False # if you want to use v1 datadog logged payload. -gcs_pub_sub_use_v1: Optional[ - bool -] = False # if you want to use v1 gcs pubsub logged payload -generic_api_use_v1: Optional[ - bool -] = False # if you want to use v1 generic api logged payload +gcs_pub_sub_use_v1: Optional[bool] = ( + False # if you want to use v1 gcs pubsub logged payload +) +generic_api_use_v1: Optional[bool] = ( + False # if you want to use v1 generic api logged payload +) argilla_transformation_object: Optional[Dict[str, Any]] = None _async_input_callback: List[ Union[str, Callable, "CustomLogger"] @@ -192,25 +192,25 @@ _async_failure_callback: List[ pre_call_rules: List[Callable] = [] post_call_rules: List[Callable] = [] turn_off_message_logging: Optional[bool] = False -standard_logging_payload_excluded_fields: Optional[ - List[str] -] = None # Fields to exclude from StandardLoggingPayload before callbacks receive it +standard_logging_payload_excluded_fields: Optional[List[str]] = ( + None # Fields to exclude from StandardLoggingPayload before callbacks receive it +) log_raw_request_response: bool = False redact_messages_in_exceptions: Optional[bool] = False redact_user_api_key_info: Optional[bool] = False filter_invalid_headers: Optional[bool] = False -add_user_information_to_llm_headers: Optional[ - bool -] = None # adds user_id, team_id, token hash (params from StandardLoggingMetadata) to request headers +add_user_information_to_llm_headers: Optional[bool] = ( + None # adds user_id, team_id, token hash (params from StandardLoggingMetadata) to request headers +) store_audit_logs = False # Enterprise feature, allow users to see audit logs ### end of callbacks ############# -email: Optional[ - str -] = None # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 -token: Optional[ - str -] = None # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 +email: Optional[str] = ( + None # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 +) +token: Optional[str] = ( + None # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 +) telemetry = True max_tokens: int = DEFAULT_MAX_TOKENS # OpenAI Defaults drop_params = bool(os.getenv("LITELLM_DROP_PARAMS", False)) @@ -272,9 +272,9 @@ use_client: bool = False ssl_verify: Union[str, bool] = True ssl_security_level: Optional[str] = None ssl_certificate: Optional[str] = None -ssl_ecdh_curve: Optional[ - str -] = None # Set to 'X25519' to disable PQC and improve performance +ssl_ecdh_curve: Optional[str] = ( + None # Set to 'X25519' to disable PQC and improve performance +) disable_streaming_logging: bool = False disable_token_counter: bool = False disable_add_transform_inline_image_block: bool = False @@ -327,20 +327,24 @@ enable_loadbalancing_on_batch_endpoints: Optional[bool] = None enable_caching_on_provider_specific_optional_params: bool = ( False # feature-flag for caching on optional params - e.g. 'top_k' ) -caching: bool = False # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 -caching_with_models: bool = False # # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 -cache: Optional[ - "Cache" -] = None # cache object <- use this - https://docs.litellm.ai/docs/caching +caching: bool = ( + False # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 +) +caching_with_models: bool = ( + False # # Not used anymore, will be removed in next MAJOR release - https://github.com/BerriAI/litellm/discussions/648 +) +cache: Optional["Cache"] = ( + None # cache object <- use this - https://docs.litellm.ai/docs/caching +) default_in_memory_ttl: Optional[float] = None default_redis_ttl: Optional[float] = None default_redis_batch_cache_expiry: Optional[float] = None model_alias_map: Dict[str, str] = {} model_group_settings: Optional["ModelGroupSettings"] = None max_budget: float = 0.0 # set the max budget across all providers -budget_duration: Optional[ - str -] = None # proxy only - resets budget after fixed duration. You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d"). +budget_duration: Optional[str] = ( + None # proxy only - resets budget after fixed duration. You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d"). +) default_soft_budget: float = ( DEFAULT_SOFT_BUDGET # by default all litellm proxy keys have a soft budget of 50.0 ) @@ -349,7 +353,9 @@ forward_traceparent_to_llm_provider: bool = False _current_cost = 0.0 # private variable, used if max budget is set error_logs: Dict = {} -add_function_to_prompt: bool = False # if function calling not supported by api, append function call details to system prompt +add_function_to_prompt: bool = ( + False # if function calling not supported by api, append function call details to system prompt +) client_session: Optional[httpx.Client] = None aclient_session: Optional[httpx.AsyncClient] = None model_fallbacks: Optional[List] = None # Deprecated for 'litellm.fallbacks' @@ -396,7 +402,9 @@ prometheus_emit_stream_label: bool = False disable_add_prefix_to_prompt: bool = ( False # used by anthropic, to disable adding prefix to prompt ) -disable_copilot_system_to_assistant: bool = False # If false (default), converts all 'system' role messages to 'assistant' for GitHub Copilot compatibility. Set to true to disable this behavior. +disable_copilot_system_to_assistant: bool = ( + False # If false (default), converts all 'system' role messages to 'assistant' for GitHub Copilot compatibility. Set to true to disable this behavior. +) public_mcp_servers: Optional[List[str]] = None public_model_groups: Optional[List[str]] = None public_agent_groups: Optional[List[str]] = None @@ -405,9 +413,9 @@ public_agent_groups: Optional[List[str]] = None # Old format: { "displayName": "url" } (for backward compatibility) public_model_groups_links: Dict[str, Union[str, Dict[str, Any]]] = {} #### REQUEST PRIORITIZATION ####### -priority_reservation: Optional[ - Dict[str, Union[float, "PriorityReservationDict"]] -] = None +priority_reservation: Optional[Dict[str, Union[float, "PriorityReservationDict"]]] = ( + None +) # priority_reservation_settings is lazy-loaded via __getattr__ # Only declare for type checking - at runtime __getattr__ handles it if TYPE_CHECKING: @@ -415,13 +423,17 @@ if TYPE_CHECKING: ######## Networking Settings ######## -use_aiohttp_transport: bool = True # Older variable, aiohttp is now the default. use disable_aiohttp_transport instead. +use_aiohttp_transport: bool = ( + True # Older variable, aiohttp is now the default. use disable_aiohttp_transport instead. +) aiohttp_trust_env: bool = False # set to true to use HTTP_ Proxy settings disable_aiohttp_transport: bool = False # Set this to true to use httpx instead disable_aiohttp_trust_env: bool = ( False # When False, aiohttp will respect HTTP(S)_PROXY env vars ) -force_ipv4: bool = False # when True, litellm will force ipv4 for all LLM requests. Some users have seen httpx ConnectionError when using ipv6. +force_ipv4: bool = ( + False # when True, litellm will force ipv4 for all LLM requests. Some users have seen httpx ConnectionError when using ipv6. +) network_mock: bool = False # When True, use mock transport — no real network calls ####### STOP SEQUENCE LIMIT ####### @@ -436,13 +448,13 @@ context_window_fallbacks: Optional[List] = None content_policy_fallbacks: Optional[List] = None allowed_fails: int = 3 allow_dynamic_callback_disabling: bool = True -num_retries_per_request: Optional[ - int -] = None # for the request overall (incl. fallbacks + model retries) +num_retries_per_request: Optional[int] = ( + None # for the request overall (incl. fallbacks + model retries) +) ####### SECRET MANAGERS ##################### -secret_manager_client: Optional[ - Any -] = None # list of instantiated key management clients - e.g. azure kv, infisical, etc. +secret_manager_client: Optional[Any] = ( + None # list of instantiated key management clients - e.g. azure kv, infisical, etc. +) _google_kms_resource_name: Optional[str] = None _key_management_system: Optional["KeyManagementSystem"] = None # Note: KeyManagementSettings must be eagerly imported because _key_management_settings @@ -455,12 +467,12 @@ output_parse_pii: bool = False from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map model_cost = get_model_cost_map(url=model_cost_map_url) -cost_discount_config: Dict[ - str, float -] = {} # Provider-specific cost discounts {"vertex_ai": 0.05} = 5% discount -cost_margin_config: Dict[ - str, Union[float, Dict[str, float]] -] = {} # Provider-specific or global cost margins. Examples: +cost_discount_config: Dict[str, float] = ( + {} +) # Provider-specific cost discounts {"vertex_ai": 0.05} = 5% discount +cost_margin_config: Dict[str, Union[float, Dict[str, float]]] = ( + {} +) # Provider-specific or global cost margins. Examples: # Percentage: {"openai": 0.10} = 10% margin # Fixed: {"openai": {"fixed_amount": 0.001}} = $0.001 per request # Global: {"global": 0.05} = 5% global margin on all providers @@ -1309,12 +1321,12 @@ from . import rag from .types.llms.custom_llm import CustomLLMItem custom_provider_map: List[CustomLLMItem] = [] -_custom_providers: List[ - str -] = [] # internal helper util, used to track names of custom providers -disable_hf_tokenizer_download: Optional[ - bool -] = None # disable huggingface tokenizer download. Defaults to openai clk100 +_custom_providers: List[str] = ( + [] +) # internal helper util, used to track names of custom providers +disable_hf_tokenizer_download: Optional[bool] = ( + None # disable huggingface tokenizer download. Defaults to openai clk100 +) global_disable_no_log_param: bool = False ### CLI UTILITIES ### diff --git a/litellm/_lazy_imports.py b/litellm/_lazy_imports.py index 3604506d406..4d811c3d7d9 100644 --- a/litellm/_lazy_imports.py +++ b/litellm/_lazy_imports.py @@ -14,6 +14,7 @@ How it works: This makes importing litellm much faster because we don't load heavy dependencies until they're actually needed. """ + import importlib import sys from typing import Any, Optional, cast, Callable diff --git a/litellm/a2a_protocol/main.py b/litellm/a2a_protocol/main.py index c86549da77a..96a592f4314 100644 --- a/litellm/a2a_protocol/main.py +++ b/litellm/a2a_protocol/main.py @@ -120,9 +120,9 @@ def _get_a2a_model_info(a2a_client: Any, kwargs: Dict[str, Any]) -> str: litellm_logging_obj.model = model litellm_logging_obj.custom_llm_provider = custom_llm_provider litellm_logging_obj.model_call_details["model"] = model - litellm_logging_obj.model_call_details[ - "custom_llm_provider" - ] = custom_llm_provider + litellm_logging_obj.model_call_details["custom_llm_provider"] = ( + custom_llm_provider + ) return agent_name diff --git a/litellm/a2a_protocol/providers/bedrock_agentcore/handler.py b/litellm/a2a_protocol/providers/bedrock_agentcore/handler.py index d7445dfc252..11676aaa895 100644 --- a/litellm/a2a_protocol/providers/bedrock_agentcore/handler.py +++ b/litellm/a2a_protocol/providers/bedrock_agentcore/handler.py @@ -99,9 +99,7 @@ class BedrockAgentCoreA2AHandler: ) ) - verbose_logger.info( - f"BedrockAgentCore A2A: Sending streaming request to {url}" - ) + verbose_logger.info(f"BedrockAgentCore A2A: Sending streaming request to {url}") client = get_async_httpx_client( llm_provider=cast(Any, httpxSpecialProvider.A2AProvider), diff --git a/litellm/a2a_protocol/streaming_iterator.py b/litellm/a2a_protocol/streaming_iterator.py index 98d45cf2ac1..c5ae9bcdc3c 100644 --- a/litellm/a2a_protocol/streaming_iterator.py +++ b/litellm/a2a_protocol/streaming_iterator.py @@ -168,9 +168,9 @@ class A2AStreamingIterator: result: Dict[str, Any] = { "id": getattr(self.request, "id", "unknown"), "jsonrpc": "2.0", - "usage": usage.model_dump() - if hasattr(usage, "model_dump") - else dict(usage), + "usage": ( + usage.model_dump() if hasattr(usage, "model_dump") else dict(usage) + ), } # Add final chunk result if available diff --git a/litellm/anthropic_interface/__init__.py b/litellm/anthropic_interface/__init__.py index 9902fdc553b..280d70142b1 100644 --- a/litellm/anthropic_interface/__init__.py +++ b/litellm/anthropic_interface/__init__.py @@ -1,6 +1,7 @@ """ Anthropic module for LiteLLM """ + from .messages import acreate, create __all__ = ["acreate", "create"] diff --git a/litellm/caching/caching_handler.py b/litellm/caching/caching_handler.py index 7cdbd3fc03d..2bec705946c 100644 --- a/litellm/caching/caching_handler.py +++ b/litellm/caching/caching_handler.py @@ -78,7 +78,9 @@ class CachingHandlerResponse(BaseModel): cached_result: Optional[Any] = None final_embedding_cached_response: Optional[EmbeddingResponse] = None - embedding_all_elements_cache_hit: bool = False # this is set to True when all elements in the list have a cache hit in the embedding cache, if true return the final_embedding_cached_response no need to make an API call + embedding_all_elements_cache_hit: bool = ( + False # this is set to True when all elements in the list have a cache hit in the embedding cache, if true return the final_embedding_cached_response no need to make an API call + ) in_memory_cache_obj = InMemoryCache() @@ -1014,9 +1016,9 @@ class LLMCachingHandler: } if litellm.cache is not None: - litellm_params[ - "preset_cache_key" - ] = litellm.cache._get_preset_cache_key_from_kwargs(**kwargs) + litellm_params["preset_cache_key"] = ( + litellm.cache._get_preset_cache_key_from_kwargs(**kwargs) + ) else: litellm_params["preset_cache_key"] = None diff --git a/litellm/caching/gcs_cache.py b/litellm/caching/gcs_cache.py index a5bd092f154..3327e094bc2 100644 --- a/litellm/caching/gcs_cache.py +++ b/litellm/caching/gcs_cache.py @@ -1,6 +1,7 @@ """GCS Cache implementation Supports syncing responses to Google Cloud Storage Buckets using HTTP requests. """ + import json import asyncio from typing import Optional diff --git a/litellm/completion_extras/litellm_responses_transformation/handler.py b/litellm/completion_extras/litellm_responses_transformation/handler.py index 2164a2c0f01..ce398ee8288 100644 --- a/litellm/completion_extras/litellm_responses_transformation/handler.py +++ b/litellm/completion_extras/litellm_responses_transformation/handler.py @@ -142,9 +142,7 @@ class ResponsesToCompletionBridgeHandler: custom_llm_provider=custom_llm_provider, ) - def completion( - self, *args, **kwargs - ) -> Union[ + def completion(self, *args, **kwargs) -> Union[ Coroutine[Any, Any, Union["ModelResponse", "CustomStreamWrapper"]], "ModelResponse", "CustomStreamWrapper", diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index ff1bc0d3839..da3b9184edb 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -300,10 +300,10 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): if key in ("max_tokens", "max_completion_tokens"): responses_api_request["max_output_tokens"] = value elif key == "tools" and value is not None: - responses_api_request[ - "tools" - ] = self._convert_tools_to_responses_format( - cast(List[Dict[str, Any]], value) + responses_api_request["tools"] = ( + self._convert_tools_to_responses_format( + cast(List[Dict[str, Any]], value) + ) ) elif key == "response_format": text_format = self._transform_response_format_to_text_format(value) @@ -506,9 +506,11 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): annotations=annotations, reasoning_items=cast( Optional[List[ChatCompletionReasoningItem]], - [pending_reasoning_item] - if pending_reasoning_item is not None - else None, + ( + [pending_reasoning_item] + if pending_reasoning_item is not None + else None + ), ), ) @@ -566,9 +568,11 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): reasoning_content=reasoning_content, reasoning_items=cast( Optional[List[ChatCompletionReasoningItem]], - [pending_reasoning_item] - if pending_reasoning_item is not None - else None, + ( + [pending_reasoning_item] + if pending_reasoning_item is not None + else None + ), ), ) choices.append( @@ -1154,9 +1158,9 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator): ) if provider_specific_fields: - function_chunk[ - "provider_specific_fields" - ] = provider_specific_fields + function_chunk["provider_specific_fields"] = ( + provider_specific_fields + ) tool_call_index = parsed_chunk.get("output_index", 0) tool_call_chunk = ChatCompletionToolCallChunk( @@ -1229,9 +1233,9 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator): # Add provider_specific_fields to function if present if provider_specific_fields: - function_chunk[ - "provider_specific_fields" - ] = provider_specific_fields + function_chunk["provider_specific_fields"] = ( + provider_specific_fields + ) tool_call_index = parsed_chunk.get("output_index", 0) tool_call_chunk = ChatCompletionToolCallChunk( diff --git a/litellm/constants.py b/litellm/constants.py index 1af53b2dae0..de961d0be83 100644 --- a/litellm/constants.py +++ b/litellm/constants.py @@ -1398,7 +1398,7 @@ APSCHEDULER_REPLACE_EXISTING = os.getenv( "1", ] # always replace existing jobs -# The number of tag entries are higher than number of user, team entries. This leads to a higher QPS. +# The number of tag entries are higher than number of user, team entries. This leads to a higher QPS. # This will run tag spcific tasks at a later time to smooth QPS DAILY_TAG_SPEND_BATCH_MULTIPLIER = 2.3 diff --git a/litellm/containers/endpoint_factory.py b/litellm/containers/endpoint_factory.py index 1d8e50856fe..a3624a90674 100644 --- a/litellm/containers/endpoint_factory.py +++ b/litellm/containers/endpoint_factory.py @@ -76,10 +76,10 @@ def create_sync_endpoint_function(endpoint_config: Dict) -> Callable: # Get provider config litellm_params = GenericLiteLLMParams(**kwargs) - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: diff --git a/litellm/containers/main.py b/litellm/containers/main.py index 916fc26351b..a7b37d3f469 100644 --- a/litellm/containers/main.py +++ b/litellm/containers/main.py @@ -165,7 +165,10 @@ def create_container( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[ContainerObject, Coroutine[Any, Any, ContainerObject],]: +) -> Union[ + ContainerObject, + Coroutine[Any, Any, ContainerObject], +]: """Create a container using the OpenAI Container API. Currently supports OpenAI @@ -205,10 +208,10 @@ def create_container( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: @@ -391,7 +394,10 @@ def list_containers( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[ContainerListResponse, Coroutine[Any, Any, ContainerListResponse],]: +) -> Union[ + ContainerListResponse, + Coroutine[Any, Any, ContainerListResponse], +]: """List containers using the OpenAI Container API. Currently supports OpenAI @@ -420,10 +426,10 @@ def list_containers( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: @@ -587,7 +593,10 @@ def retrieve_container( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[ContainerObject, Coroutine[Any, Any, ContainerObject],]: +) -> Union[ + ContainerObject, + Coroutine[Any, Any, ContainerObject], +]: """Retrieve a container using the OpenAI Container API. Currently supports OpenAI @@ -616,10 +625,10 @@ def retrieve_container( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: @@ -773,7 +782,10 @@ def delete_container( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[DeleteContainerResult, Coroutine[Any, Any, DeleteContainerResult],]: +) -> Union[ + DeleteContainerResult, + Coroutine[Any, Any, DeleteContainerResult], +]: """Delete a container using the OpenAI Container API. Currently supports OpenAI @@ -802,10 +814,10 @@ def delete_container( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: @@ -973,7 +985,10 @@ def list_container_files( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[ContainerFileListResponse, Coroutine[Any, Any, ContainerFileListResponse],]: +) -> Union[ + ContainerFileListResponse, + Coroutine[Any, Any, ContainerFileListResponse], +]: """List files in a container using the OpenAI Container API. Currently supports OpenAI @@ -1002,10 +1017,10 @@ def list_container_files( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: @@ -1190,7 +1205,10 @@ def upload_container_file( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[ContainerFileObject, Coroutine[Any, Any, ContainerFileObject],]: +) -> Union[ + ContainerFileObject, + Coroutine[Any, Any, ContainerFileObject], +]: """Upload a file to a container using the OpenAI Container API. This endpoint allows uploading files directly to a container session, @@ -1248,10 +1266,10 @@ def upload_container_file( **kwargs, ) # get provider config - container_provider_config: Optional[ - BaseContainerConfig - ] = ProviderConfigManager.get_provider_container_config( - provider=litellm.LlmProviders(custom_llm_provider), + container_provider_config: Optional[BaseContainerConfig] = ( + ProviderConfigManager.get_provider_container_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if container_provider_config is None: diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 3b73b853eca..7d0d7001f7d 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -544,10 +544,9 @@ def cost_per_token( # noqa: PLR0915 model=model, custom_llm_provider=custom_llm_provider ) - if ( - (model_info.get("input_cost_per_token") or 0.0) > 0 - or (model_info.get("output_cost_per_token") or 0.0) > 0 - ): + if (model_info.get("input_cost_per_token") or 0.0) > 0 or ( + model_info.get("output_cost_per_token") or 0.0 + ) > 0: return generic_cost_per_token( model=model, usage=usage_block, @@ -1136,9 +1135,9 @@ def completion_cost( # noqa: PLR0915 or isinstance(completion_response, dict) ): # tts returns a custom class if isinstance(completion_response, dict): - usage_obj: Optional[ - Union[dict, Usage] - ] = completion_response.get("usage", {}) + usage_obj: Optional[Union[dict, Usage]] = ( + completion_response.get("usage", {}) + ) else: usage_obj = getattr(completion_response, "usage", {}) if isinstance(usage_obj, BaseModel) and not _is_known_usage_objects( diff --git a/litellm/evals/main.py b/litellm/evals/main.py index eab909a6b11..df6d3accb82 100644 --- a/litellm/evals/main.py +++ b/litellm/evals/main.py @@ -152,10 +152,10 @@ def create_eval( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -343,10 +343,10 @@ def list_evals( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -513,10 +513,10 @@ def get_eval( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -682,10 +682,10 @@ def update_eval( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -893,10 +893,10 @@ def delete_eval( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1047,10 +1047,10 @@ def cancel_eval( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1230,10 +1230,10 @@ def create_run( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1418,10 +1418,10 @@ def list_runs( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1592,10 +1592,10 @@ def get_run( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1752,10 +1752,10 @@ def cancel_run( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: @@ -1921,10 +1921,10 @@ def delete_run( custom_llm_provider = "openai" # Get provider config - evals_api_provider_config: Optional[ - BaseEvalsAPIConfig - ] = ProviderConfigManager.get_provider_evals_api_config( # type: ignore - provider=litellm.LlmProviders(custom_llm_provider), + evals_api_provider_config: Optional[BaseEvalsAPIConfig] = ( + ProviderConfigManager.get_provider_evals_api_config( # type: ignore + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if evals_api_provider_config is None: diff --git a/litellm/images/main.py b/litellm/images/main.py index a5ae154190a..0d3b2e97294 100644 --- a/litellm/images/main.py +++ b/litellm/images/main.py @@ -210,7 +210,10 @@ def image_generation( # noqa: PLR0915 api_version: Optional[str] = None, custom_llm_provider=None, **kwargs, -) -> Union[ImageResponse, Coroutine[Any, Any, ImageResponse],]: +) -> Union[ + ImageResponse, + Coroutine[Any, Any, ImageResponse], +]: """ Maps the https://api.openai.com/v1/images/generations endpoint. @@ -864,11 +867,11 @@ def image_edit( # noqa: PLR0915 ) # get provider config - image_edit_provider_config: Optional[ - BaseImageEditConfig - ] = ProviderConfigManager.get_provider_image_edit_config( - model=model, - provider=litellm.LlmProviders(custom_llm_provider), + image_edit_provider_config: Optional[BaseImageEditConfig] = ( + ProviderConfigManager.get_provider_image_edit_config( + model=model, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if image_edit_provider_config is None: @@ -876,20 +879,20 @@ def image_edit( # noqa: PLR0915 local_vars.update(kwargs) # Get ImageEditOptionalRequestParams with only valid parameters - image_edit_optional_params: ImageEditOptionalRequestParams = ( - _get_ImageEditRequestUtils().get_requested_image_edit_optional_param( - local_vars - ) + image_edit_optional_params: ( + ImageEditOptionalRequestParams + ) = _get_ImageEditRequestUtils().get_requested_image_edit_optional_param( + local_vars ) # Get optional parameters for the responses API - image_edit_request_params: Dict = ( - _get_ImageEditRequestUtils().get_optional_params_image_edit( - model=model, - image_edit_provider_config=image_edit_provider_config, - image_edit_optional_params=image_edit_optional_params, - drop_params=kwargs.get("drop_params"), - additional_drop_params=kwargs.get("additional_drop_params"), - ) + image_edit_request_params: ( + Dict + ) = _get_ImageEditRequestUtils().get_optional_params_image_edit( + model=model, + image_edit_provider_config=image_edit_provider_config, + image_edit_optional_params=image_edit_optional_params, + drop_params=kwargs.get("drop_params"), + additional_drop_params=kwargs.get("additional_drop_params"), ) # Pre Call logging diff --git a/litellm/integrations/SlackAlerting/hanging_request_check.py b/litellm/integrations/SlackAlerting/hanging_request_check.py index b9c485dce82..d2f70c9caf1 100644 --- a/litellm/integrations/SlackAlerting/hanging_request_check.py +++ b/litellm/integrations/SlackAlerting/hanging_request_check.py @@ -102,10 +102,10 @@ class AlertingHangingRequestCheck: ) for request_id in hanging_requests: - hanging_request_data: Optional[ - HangingRequestData - ] = await self.hanging_request_cache.async_get_cache( - key=request_id, + hanging_request_data: Optional[HangingRequestData] = ( + await self.hanging_request_cache.async_get_cache( + key=request_id, + ) ) if hanging_request_data is None: diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.py index 013cef74805..0ec17bbea5d 100644 --- a/litellm/integrations/SlackAlerting/slack_alerting.py +++ b/litellm/integrations/SlackAlerting/slack_alerting.py @@ -852,9 +852,9 @@ class SlackAlerting(CustomBatchLogger): ### UNIQUE CACHE KEY ### cache_key = provider + region_name - outage_value: Optional[ - ProviderRegionOutageModel - ] = await self.internal_usage_cache.async_get_cache(key=cache_key) + outage_value: Optional[ProviderRegionOutageModel] = ( + await self.internal_usage_cache.async_get_cache(key=cache_key) + ) # Convert deployment_ids back to set if it was stored as a list if outage_value is not None: @@ -1443,9 +1443,9 @@ Model Info: self.alert_to_webhook_url is not None and alert_type in self.alert_to_webhook_url ): - _digest_webhook: Optional[ - Union[str, List[str]] - ] = self.alert_to_webhook_url[alert_type] + _digest_webhook: Optional[Union[str, List[str]]] = ( + self.alert_to_webhook_url[alert_type] + ) elif self.default_webhook_url is not None: _digest_webhook = self.default_webhook_url else: @@ -1499,9 +1499,9 @@ Model Info: self.alert_to_webhook_url is not None and alert_type in self.alert_to_webhook_url ): - slack_webhook_url: Optional[ - Union[str, List[str]] - ] = self.alert_to_webhook_url[alert_type] + slack_webhook_url: Optional[Union[str, List[str]]] = ( + self.alert_to_webhook_url[alert_type] + ) elif self.default_webhook_url is not None: slack_webhook_url = self.default_webhook_url else: diff --git a/litellm/integrations/agentops/agentops.py b/litellm/integrations/agentops/agentops.py index 38b91c06587..4f17806a6b7 100644 --- a/litellm/integrations/agentops/agentops.py +++ b/litellm/integrations/agentops/agentops.py @@ -1,6 +1,7 @@ """ AgentOps integration for LiteLLM - Provides OpenTelemetry tracing for LLM calls """ + import os from dataclasses import dataclass from typing import Optional, Dict, Any diff --git a/litellm/integrations/anthropic_cache_control_hook.py b/litellm/integrations/anthropic_cache_control_hook.py index 0e99537d5db..213622cb43a 100644 --- a/litellm/integrations/anthropic_cache_control_hook.py +++ b/litellm/integrations/anthropic_cache_control_hook.py @@ -106,10 +106,10 @@ class AnthropicCacheControlHook(CustomPromptManagement): targetted_index += len(messages) if 0 <= targetted_index < len(messages): - messages[ - targetted_index - ] = AnthropicCacheControlHook._safe_insert_cache_control_in_message( - messages[targetted_index], control + messages[targetted_index] = ( + AnthropicCacheControlHook._safe_insert_cache_control_in_message( + messages[targetted_index], control + ) ) else: verbose_logger.warning( diff --git a/litellm/integrations/arize/arize_phoenix.py b/litellm/integrations/arize/arize_phoenix.py index 00bc24d4188..b8cd04836c3 100644 --- a/litellm/integrations/arize/arize_phoenix.py +++ b/litellm/integrations/arize/arize_phoenix.py @@ -178,9 +178,9 @@ class ArizePhoenixLogger(OpenTelemetry): # type: ignore start_time_val = kwargs.get("start_time", kwargs.get("api_call_start_time")) parent_span = self.tracer.start_span( name="litellm_proxy_request", - start_time=self._to_ns(start_time_val) - if start_time_val is not None - else None, + start_time=( + self._to_ns(start_time_val) if start_time_val is not None else None + ), context=traceparent_ctx, kind=self.span_kind.SERVER, ) diff --git a/litellm/integrations/azure_storage/azure_storage.py b/litellm/integrations/azure_storage/azure_storage.py index 50c1cd9d989..b06fa13e918 100644 --- a/litellm/integrations/azure_storage/azure_storage.py +++ b/litellm/integrations/azure_storage/azure_storage.py @@ -54,12 +54,12 @@ class AzureBlobStorageLogger(CustomBatchLogger): self._service_client_timeout: Optional[float] = None # Internal variables used for Token based authentication - self.azure_auth_token: Optional[ - str - ] = None # the Azure AD token to use for Azure Storage API requests - self.token_expiry: Optional[ - datetime - ] = None # the expiry time of the currentAzure AD token + self.azure_auth_token: Optional[str] = ( + None # the Azure AD token to use for Azure Storage API requests + ) + self.token_expiry: Optional[datetime] = ( + None # the expiry time of the currentAzure AD token + ) asyncio.create_task(self.periodic_flush()) self.flush_lock = asyncio.Lock() diff --git a/litellm/integrations/braintrust_logging.py b/litellm/integrations/braintrust_logging.py index cb1b2bc5531..9b1c5077882 100644 --- a/litellm/integrations/braintrust_logging.py +++ b/litellm/integrations/braintrust_logging.py @@ -52,9 +52,9 @@ class BraintrustLogger(CustomLogger): "Authorization": "Bearer " + self.api_key, "Content-Type": "application/json", } - self._project_id_cache: Dict[ - str, str - ] = {} # Cache mapping project names to IDs + self._project_id_cache: Dict[str, str] = ( + {} + ) # Cache mapping project names to IDs self.global_braintrust_http_handler = get_async_httpx_client( llm_provider=httpxSpecialProvider.LoggingCallback ) diff --git a/litellm/integrations/cloudzero/cloudzero.py b/litellm/integrations/cloudzero/cloudzero.py index 9da8ea52b5c..8decd4ef23f 100644 --- a/litellm/integrations/cloudzero/cloudzero.py +++ b/litellm/integrations/cloudzero/cloudzero.py @@ -402,10 +402,10 @@ class CloudZeroLogger(CustomLogger): from litellm.constants import CLOUDZERO_EXPORT_INTERVAL_MINUTES from litellm.integrations.custom_logger import CustomLogger - prometheus_loggers: List[ - CustomLogger - ] = litellm.logging_callback_manager.get_custom_loggers_for_type( - callback_type=CloudZeroLogger + prometheus_loggers: List[CustomLogger] = ( + litellm.logging_callback_manager.get_custom_loggers_for_type( + callback_type=CloudZeroLogger + ) ) # we need to get the initialized prometheus logger instance(s) and call logger.initialize_remaining_budget_metrics() on them verbose_logger.debug("found %s cloudzero loggers", len(prometheus_loggers)) diff --git a/litellm/integrations/cloudzero/transform.py b/litellm/integrations/cloudzero/transform.py index c1b0d5cf411..2d84796150a 100644 --- a/litellm/integrations/cloudzero/transform.py +++ b/litellm/integrations/cloudzero/transform.py @@ -159,9 +159,9 @@ class CBFTransformer: # CloudZero CBF format with proper column names cbf_record = { # Required CBF fields - "time/usage_start": usage_date.isoformat() - if usage_date - else None, # Required: ISO-formatted UTC datetime + "time/usage_start": ( + usage_date.isoformat() if usage_date else None + ), # Required: ISO-formatted UTC datetime "cost/cost": float(row.get("spend", 0.0)), # Required: billed cost "resource/id": resource_id, # CZRN (CloudZero Resource Name) # Usage metrics for token consumption @@ -182,9 +182,9 @@ class CBFTransformer: # Add CZRN components that don't have direct CBF column mappings as resource tags cbf_record["resource/tag:provider"] = provider # CZRN provider component - cbf_record[ - "resource/tag:model" - ] = cloud_local_id # CZRN cloud-local-id component (model) + cbf_record["resource/tag:model"] = ( + cloud_local_id # CZRN cloud-local-id component (model) + ) # Add resource tags for all dimensions (using resource/tag: format) for key, value in dimensions.items(): diff --git a/litellm/integrations/custom_logger.py b/litellm/integrations/custom_logger.py index cccabf53e51..45c8e2f6262 100644 --- a/litellm/integrations/custom_logger.py +++ b/litellm/integrations/custom_logger.py @@ -874,9 +874,9 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac model_response_dict = model_response.model_dump() standard_logging_object_copy["response"] = model_response_dict - model_call_details_copy[ - "standard_logging_object" - ] = standard_logging_object_copy + model_call_details_copy["standard_logging_object"] = ( + standard_logging_object_copy + ) return model_call_details_copy async def get_proxy_server_request_from_cold_storage_with_object_key( diff --git a/litellm/integrations/datadog/datadog_llm_obs.py b/litellm/integrations/datadog/datadog_llm_obs.py index ec6c00961b6..201d3fb0a41 100644 --- a/litellm/integrations/datadog/datadog_llm_obs.py +++ b/litellm/integrations/datadog/datadog_llm_obs.py @@ -349,9 +349,9 @@ class DataDogLLMObsLogger(CustomBatchLogger): if standard_logging_payload.get("status") == "failure": # Try to get structured error information first - error_information: Optional[ - StandardLoggingPayloadErrorInformation - ] = standard_logging_payload.get("error_information") + error_information: Optional[StandardLoggingPayloadErrorInformation] = ( + standard_logging_payload.get("error_information") + ) if error_information: error_info = DDLLMObsError( @@ -621,9 +621,9 @@ class DataDogLLMObsLogger(CustomBatchLogger): latency_metrics["litellm_overhead_time_ms"] = litellm_overhead_ms # Guardrail overhead latency - guardrail_info: Optional[ - list[StandardLoggingGuardrailInformation] - ] = standard_logging_payload.get("guardrail_information") + guardrail_info: Optional[list[StandardLoggingGuardrailInformation]] = ( + standard_logging_payload.get("guardrail_information") + ) if guardrail_info is not None: total_duration = 0.0 for info in guardrail_info: @@ -793,15 +793,15 @@ class DataDogLLMObsLogger(CustomBatchLogger): if function_arguments: # Store arguments as JSON string for Datadog if isinstance(function_arguments, str): - kv_pairs[ - f"tool_calls.{idx}.function.arguments" - ] = function_arguments + kv_pairs[f"tool_calls.{idx}.function.arguments"] = ( + function_arguments + ) else: import json - kv_pairs[ - f"tool_calls.{idx}.function.arguments" - ] = json.dumps(function_arguments) + kv_pairs[f"tool_calls.{idx}.function.arguments"] = ( + json.dumps(function_arguments) + ) except (KeyError, TypeError, ValueError) as e: verbose_logger.debug( f"DataDogLLMObs: Error processing tool call {idx}: {str(e)}" diff --git a/litellm/integrations/gcs_bucket/gcs_bucket_base.py b/litellm/integrations/gcs_bucket/gcs_bucket_base.py index 0089e54b1c2..e84b37e689b 100644 --- a/litellm/integrations/gcs_bucket/gcs_bucket_base.py +++ b/litellm/integrations/gcs_bucket/gcs_bucket_base.py @@ -150,9 +150,9 @@ class GCSBucketBase(CustomBatchLogger): if kwargs is None: kwargs = {} - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = kwargs.get("standard_callback_dynamic_params", None) + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + kwargs.get("standard_callback_dynamic_params", None) + ) bucket_name: str path_service_account: Optional[str] diff --git a/litellm/integrations/humanloop.py b/litellm/integrations/humanloop.py index 11414869a65..369df5ee0bd 100644 --- a/litellm/integrations/humanloop.py +++ b/litellm/integrations/humanloop.py @@ -162,7 +162,11 @@ class HumanloopLogger(CustomLogger): prompt_version: Optional[int] = None, ignore_prompt_manager_model: Optional[bool] = False, ignore_prompt_manager_optional_params: Optional[bool] = False, - ) -> Tuple[str, List[AllMessageValues], dict,]: + ) -> Tuple[ + str, + List[AllMessageValues], + dict, + ]: humanloop_api_key = dynamic_callback_params.get( "humanloop_api_key" ) or get_secret_str("HUMANLOOP_API_KEY") diff --git a/litellm/integrations/langfuse/langfuse.py b/litellm/integrations/langfuse/langfuse.py index 6ac337d99a9..e691c490c85 100644 --- a/litellm/integrations/langfuse/langfuse.py +++ b/litellm/integrations/langfuse/langfuse.py @@ -572,9 +572,9 @@ class LangFuseLogger: # we clean out all extra litellm metadata params before logging clean_metadata: Dict[str, Any] = {} if prompt_management_metadata is not None: - clean_metadata[ - "prompt_management_metadata" - ] = prompt_management_metadata + clean_metadata["prompt_management_metadata"] = ( + prompt_management_metadata + ) if isinstance(metadata, dict): for key, value in metadata.items(): # generate langfuse tags - Default Tags sent to Langfuse from LiteLLM Proxy diff --git a/litellm/integrations/langfuse/langfuse_handler.py b/litellm/integrations/langfuse/langfuse_handler.py index f9d27f6cf00..fbadf1a2fc7 100644 --- a/litellm/integrations/langfuse/langfuse_handler.py +++ b/litellm/integrations/langfuse/langfuse_handler.py @@ -86,9 +86,7 @@ class LangFuseHandler: if globalLangfuseLogger is not None: return globalLangfuseLogger - credentials_dict: Dict[ - str, Any - ] = ( + credentials_dict: Dict[str, Any] = ( {} ) # the global langfuse logger uses Environment Variables, there are no dynamic credentials globalLangfuseLogger = in_memory_dynamic_logger_cache.get_cache( diff --git a/litellm/integrations/langfuse/langfuse_prompt_management.py b/litellm/integrations/langfuse/langfuse_prompt_management.py index bea027aa63d..5f4ced3a5cb 100644 --- a/litellm/integrations/langfuse/langfuse_prompt_management.py +++ b/litellm/integrations/langfuse/langfuse_prompt_management.py @@ -190,7 +190,11 @@ class LangfusePromptManagement(LangFuseLogger, PromptManagementBase, CustomLogge prompt_version: Optional[int] = None, ignore_prompt_manager_model: Optional[bool] = False, ignore_prompt_manager_optional_params: Optional[bool] = False, - ) -> Tuple[str, List[AllMessageValues], dict,]: + ) -> Tuple[ + str, + List[AllMessageValues], + dict, + ]: return self.get_chat_completion_prompt( model, messages, diff --git a/litellm/integrations/langsmith.py b/litellm/integrations/langsmith.py index b931d7ecfe7..3d4fd39ebe1 100644 --- a/litellm/integrations/langsmith.py +++ b/litellm/integrations/langsmith.py @@ -83,9 +83,9 @@ class LangsmithLogger(CustomBatchLogger): if _batch_size: self.batch_size = int(_batch_size) self.log_queue: List[LangsmithQueueObject] = [] - self._flush_task: Optional[ - asyncio.Task[Any] - ] = self._start_periodic_flush_task() + self._flush_task: Optional[asyncio.Task[Any]] = ( + self._start_periodic_flush_task() + ) def _start_periodic_flush_task(self) -> Optional[asyncio.Task[Any]]: """Start the periodic flush task only when an event loop is already running.""" @@ -501,9 +501,9 @@ class LangsmithLogger(CustomBatchLogger): return log_queue_by_credentials def _get_sampling_rate_to_use_for_request(self, kwargs: Dict[str, Any]) -> float: - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = kwargs.get("standard_callback_dynamic_params", None) + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + kwargs.get("standard_callback_dynamic_params", None) + ) sampling_rate: float = self.sampling_rate if standard_callback_dynamic_params is not None: _sampling_rate = standard_callback_dynamic_params.get( @@ -523,9 +523,9 @@ class LangsmithLogger(CustomBatchLogger): Otherwise, use the default credentials. """ - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = kwargs.get("standard_callback_dynamic_params", None) + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + kwargs.get("standard_callback_dynamic_params", None) + ) if standard_callback_dynamic_params is not None: credentials = self.get_credentials_from_env( langsmith_api_key=standard_callback_dynamic_params.get( diff --git a/litellm/integrations/mock_client_factory.py b/litellm/integrations/mock_client_factory.py index 3f2f0ae5b6d..02a927fe64f 100644 --- a/litellm/integrations/mock_client_factory.py +++ b/litellm/integrations/mock_client_factory.py @@ -25,9 +25,9 @@ class MockClientConfig: default_latency_ms: int = 100 # Default mock latency in milliseconds default_status_code: int = 200 # Default HTTP status code default_json_data: Optional[Dict] = None # Default JSON response data - url_matchers: Optional[ - List[str] - ] = None # List of strings to match in URLs (e.g., ["storage.googleapis.com"]) + url_matchers: Optional[List[str]] = ( + None # List of strings to match in URLs (e.g., ["storage.googleapis.com"]) + ) patch_async_handler: bool = True # Whether to patch AsyncHTTPHandler.post patch_sync_client: bool = False # Whether to patch httpx.Client.post patch_http_handler: bool = ( diff --git a/litellm/integrations/opentelemetry.py b/litellm/integrations/opentelemetry.py index 559ed05d30a..ecfb42cea7b 100644 --- a/litellm/integrations/opentelemetry.py +++ b/litellm/integrations/opentelemetry.py @@ -655,9 +655,9 @@ class OpenTelemetry(CustomLogger): def _get_dynamic_otel_headers_from_kwargs(self, kwargs) -> Optional[dict]: """Extract dynamic headers from kwargs if available.""" - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = kwargs.get("standard_callback_dynamic_params") + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + kwargs.get("standard_callback_dynamic_params") + ) if not standard_callback_dynamic_params: return None diff --git a/litellm/integrations/posthog.py b/litellm/integrations/posthog.py index 17bb56b8f17..072ae4945a0 100644 --- a/litellm/integrations/posthog.py +++ b/litellm/integrations/posthog.py @@ -349,9 +349,9 @@ class PostHogLogger(CustomBatchLogger): Returns: tuple[str, str]: (api_key, api_url) """ - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = kwargs.get("standard_callback_dynamic_params", None) + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + kwargs.get("standard_callback_dynamic_params", None) + ) if standard_callback_dynamic_params is not None: api_key = ( diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index fb5fc253ae4..d022af689d3 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -1087,9 +1087,11 @@ class PrometheusLogger(CustomLogger): ), client_ip=standard_logging_payload["metadata"].get("requester_ip_address"), user_agent=standard_logging_payload["metadata"].get("user_agent"), - stream=str(standard_logging_payload.get("stream")) - if litellm.prometheus_emit_stream_label - else None, + stream=( + str(standard_logging_payload.get("stream")) + if litellm.prometheus_emit_stream_label + else None + ), ) if ( @@ -1755,9 +1757,11 @@ class PrometheusLogger(CustomLogger): client_ip=_metadata.get("requester_ip_address"), user_agent=_metadata.get("user_agent"), model_id=model_id, - stream=str(request_data.get("stream")) - if litellm.prometheus_emit_stream_label - else None, + stream=( + str(request_data.get("stream")) + if litellm.prometheus_emit_stream_label + else None + ), ) _labels = prometheus_label_factory( supported_enum_labels=self.get_labels_for_metric( @@ -2081,9 +2085,9 @@ class PrometheusLogger(CustomLogger): ): try: verbose_logger.debug("setting remaining tokens requests metric") - standard_logging_payload: Optional[ - StandardLoggingPayload - ] = request_kwargs.get("standard_logging_object") + standard_logging_payload: Optional[StandardLoggingPayload] = ( + request_kwargs.get("standard_logging_object") + ) if standard_logging_payload is None: return @@ -2716,9 +2720,7 @@ class PrometheusLogger(CustomLogger): ) return - async def fetch_keys( - page_size: int, page: int - ) -> Tuple[ + async def fetch_keys(page_size: int, page: int) -> Tuple[ List[Union[str, UserAPIKeyAuth, LiteLLM_DeletedVerificationToken]], Optional[int], ]: @@ -2909,9 +2911,11 @@ class PrometheusLogger(CustomLogger): org_alias=org.organization_alias or "", spend=org.spend or 0.0, max_budget=budget_table.max_budget if budget_table else None, - budget_reset_at=getattr(budget_table, "budget_reset_at", None) - if budget_table - else None, + budget_reset_at=( + getattr(budget_table, "budget_reset_at", None) + if budget_table + else None + ), ) async def _set_team_budget_metrics_after_api_request( @@ -3393,10 +3397,10 @@ class PrometheusLogger(CustomLogger): from litellm.constants import PROMETHEUS_BUDGET_METRICS_REFRESH_INTERVAL_MINUTES from litellm.integrations.custom_logger import CustomLogger - prometheus_loggers: List[ - CustomLogger - ] = litellm.logging_callback_manager.get_custom_loggers_for_type( - callback_type=PrometheusLogger + prometheus_loggers: List[CustomLogger] = ( + litellm.logging_callback_manager.get_custom_loggers_for_type( + callback_type=PrometheusLogger + ) ) # we need to get the initialized prometheus logger instance(s) and call logger.initialize_remaining_budget_metrics() on them verbose_logger.debug("found %s prometheus loggers", len(prometheus_loggers)) diff --git a/litellm/integrations/s3_v2.py b/litellm/integrations/s3_v2.py index 405bf9698cc..9eac7c265ce 100644 --- a/litellm/integrations/s3_v2.py +++ b/litellm/integrations/s3_v2.py @@ -578,9 +578,11 @@ class S3Logger(CustomBatchLogger, BaseAWSLLM): signed_headers = dict(aws_request.headers.items()) httpx_client = _get_httpx_client( - params={"ssl_verify": self.s3_verify} - if self.s3_verify is not None - else None + params=( + {"ssl_verify": self.s3_verify} + if self.s3_verify is not None + else None + ) ) # Make the request response = httpx_client.put(url, data=json_string, headers=signed_headers) diff --git a/litellm/integrations/vantage/vantage_logger.py b/litellm/integrations/vantage/vantage_logger.py index e0942472bec..1e6e46b36ae 100644 --- a/litellm/integrations/vantage/vantage_logger.py +++ b/litellm/integrations/vantage/vantage_logger.py @@ -83,9 +83,11 @@ class VantageLogger(FocusLogger): verbose_logger.debug( "VantageLogger initialized (integration_token=%s)", - resolved_token[:4] + "***" - if resolved_token and len(resolved_token) > 4 - else "***", + ( + resolved_token[:4] + "***" + if resolved_token and len(resolved_token) > 4 + else "***" + ), ) async def initialize_focus_export_job(self) -> None: @@ -124,10 +126,10 @@ class VantageLogger(FocusLogger): scheduler: AsyncIOScheduler, ) -> None: """Register the Vantage export job with the provided scheduler.""" - vantage_loggers: List[ - CustomLogger - ] = litellm.logging_callback_manager.get_custom_loggers_for_type( - callback_type=VantageLogger + vantage_loggers: List[CustomLogger] = ( + litellm.logging_callback_manager.get_custom_loggers_for_type( + callback_type=VantageLogger + ) ) if not vantage_loggers: verbose_logger.debug("No Vantage logger registered; skipping scheduler") diff --git a/litellm/integrations/vector_store_integrations/vector_store_pre_call_hook.py b/litellm/integrations/vector_store_integrations/vector_store_pre_call_hook.py index 50420fb7137..482a19c5d72 100644 --- a/litellm/integrations/vector_store_integrations/vector_store_pre_call_hook.py +++ b/litellm/integrations/vector_store_integrations/vector_store_pre_call_hook.py @@ -88,12 +88,12 @@ class VectorStorePreCallHook(CustomLogger): pass # Use database fallback to ensure synchronization across instances - vector_stores_to_run: List[ - LiteLLM_ManagedVectorStore - ] = await litellm.vector_store_registry.pop_vector_stores_to_run_with_db_fallback( - non_default_params=non_default_params, - tools=tools, - prisma_client=prisma_client, + vector_stores_to_run: List[LiteLLM_ManagedVectorStore] = ( + await litellm.vector_store_registry.pop_vector_stores_to_run_with_db_fallback( + non_default_params=non_default_params, + tools=tools, + prisma_client=prisma_client, + ) ) if not vector_stores_to_run: @@ -147,9 +147,9 @@ class VectorStorePreCallHook(CustomLogger): # Store search results as-is (already in OpenAI-compatible format) if litellm_logging_obj and all_search_results: - litellm_logging_obj.model_call_details[ - "search_results" - ] = all_search_results + litellm_logging_obj.model_call_details["search_results"] = ( + all_search_results + ) return model, modified_messages, non_default_params @@ -208,9 +208,9 @@ class VectorStorePreCallHook(CustomLogger): Returns: Modified list of messages with context appended """ - search_response_data: Optional[ - List[VectorStoreSearchResult] - ] = search_response.get("data") + search_response_data: Optional[List[VectorStoreSearchResult]] = ( + search_response.get("data") + ) if not search_response_data: return messages @@ -268,9 +268,9 @@ class VectorStorePreCallHook(CustomLogger): ) # Get search results from model_call_details (already in OpenAI format) - search_results: Optional[ - List[VectorStoreSearchResponse] - ] = litellm_logging_obj.model_call_details.get("search_results") + search_results: Optional[List[VectorStoreSearchResponse]] = ( + litellm_logging_obj.model_call_details.get("search_results") + ) verbose_logger.debug(f"Search results found: {search_results is not None}") @@ -328,9 +328,9 @@ class VectorStorePreCallHook(CustomLogger): ) # Get search results from model_call_details (already in OpenAI format) - search_results: Optional[ - List[VectorStoreSearchResponse] - ] = request_data.get("search_results") + search_results: Optional[List[VectorStoreSearchResponse]] = ( + request_data.get("search_results") + ) verbose_logger.debug( f"Search results found for streaming chunk: {search_results is not None}" diff --git a/litellm/integrations/websearch_interception/transformation.py b/litellm/integrations/websearch_interception/transformation.py index f777a7d7418..00d4829ad39 100644 --- a/litellm/integrations/websearch_interception/transformation.py +++ b/litellm/integrations/websearch_interception/transformation.py @@ -3,6 +3,7 @@ WebSearch Tool Transformation Transforms between Anthropic/OpenAI tool_use format and LiteLLM search format. """ + import json from typing import Any, Dict, List, Optional, Tuple, Union @@ -326,9 +327,11 @@ class WebSearchTransformation: "type": "function", "function": { "name": tc["name"], - "arguments": json.dumps(tc["input"]) - if isinstance(tc["input"], dict) - else str(tc["input"]), + "arguments": ( + json.dumps(tc["input"]) + if isinstance(tc["input"], dict) + else str(tc["input"]) + ), }, } for tc in tool_calls diff --git a/litellm/integrations/weights_biases.py b/litellm/integrations/weights_biases.py index 028b6e69a81..e9539d27e97 100644 --- a/litellm/integrations/weights_biases.py +++ b/litellm/integrations/weights_biases.py @@ -21,8 +21,7 @@ try: # contains a (known) object attribute object: Literal["chat.completion", "edit", "text_completion"] - def __getitem__(self, key: K) -> V: - ... # noqa + def __getitem__(self, key: K) -> V: ... # noqa def get(self, key: K, default: Optional[V] = None) -> Optional[V]: # noqa ... # pragma: no cover diff --git a/litellm/interactions/litellm_responses_transformation/transformation.py b/litellm/interactions/litellm_responses_transformation/transformation.py index b07e61c76dd..100300af7b5 100644 --- a/litellm/interactions/litellm_responses_transformation/transformation.py +++ b/litellm/interactions/litellm_responses_transformation/transformation.py @@ -45,10 +45,10 @@ class LiteLLMResponsesInteractionsConfig: # Transform input if input is not None: - responses_request[ - "input" - ] = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( - input + responses_request["input"] = ( + LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input( + input + ) ) # Transform system_instruction -> instructions diff --git a/litellm/litellm_core_utils/default_encoding.py b/litellm/litellm_core_utils/default_encoding.py index f704ba568de..f58b90c8e72 100644 --- a/litellm/litellm_core_utils/default_encoding.py +++ b/litellm/litellm_core_utils/default_encoding.py @@ -26,9 +26,9 @@ if custom_cache_dir: else: cache_dir = filename -os.environ[ - "TIKTOKEN_CACHE_DIR" -] = cache_dir # use local copy of tiktoken b/c of - https://github.com/BerriAI/litellm/issues/1071 +os.environ["TIKTOKEN_CACHE_DIR"] = ( + cache_dir # use local copy of tiktoken b/c of - https://github.com/BerriAI/litellm/issues/1071 +) import tiktoken import time diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 7395b65626f..1ea8c78c2e0 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -354,9 +354,9 @@ class Logging(LiteLLMLoggingBaseClass): ) self.function_id = function_id self.streaming_chunks: List[Any] = [] # for generating complete stream response - self.sync_streaming_chunks: List[ - Any - ] = [] # for generating complete stream response + self.sync_streaming_chunks: List[Any] = ( + [] + ) # for generating complete stream response self.log_raw_request_response = log_raw_request_response # Initialize dynamic callbacks @@ -801,9 +801,9 @@ class Logging(LiteLLMLoggingBaseClass): prompt_spec=prompt_spec, dynamic_callback_params=dynamic_callback_params, ): - self.model_call_details[ - "prompt_integration" - ] = logger.__class__.__name__ + self.model_call_details["prompt_integration"] = ( + logger.__class__.__name__ + ) return logger except Exception: # If check fails, continue to next logger @@ -871,9 +871,9 @@ class Logging(LiteLLMLoggingBaseClass): if anthropic_cache_control_logger := AnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook( non_default_params ): - self.model_call_details[ - "prompt_integration" - ] = anthropic_cache_control_logger.__class__.__name__ + self.model_call_details["prompt_integration"] = ( + anthropic_cache_control_logger.__class__.__name__ + ) return anthropic_cache_control_logger ######################################################### @@ -885,9 +885,9 @@ class Logging(LiteLLMLoggingBaseClass): internal_usage_cache=None, llm_router=None, ) - self.model_call_details[ - "prompt_integration" - ] = vector_store_custom_logger.__class__.__name__ + self.model_call_details["prompt_integration"] = ( + vector_store_custom_logger.__class__.__name__ + ) # Add to global callbacks so post-call hooks are invoked if ( vector_store_custom_logger @@ -947,9 +947,9 @@ class Logging(LiteLLMLoggingBaseClass): model ): # if model name was changes pre-call, overwrite the initial model call name with the new one self.model_call_details["model"] = model - self.model_call_details["litellm_params"][ - "api_base" - ] = self._get_masked_api_base(additional_args.get("api_base", "")) + self.model_call_details["litellm_params"]["api_base"] = ( + self._get_masked_api_base(additional_args.get("api_base", "")) + ) def pre_call(self, input, api_key, model=None, additional_args={}): # noqa: PLR0915 # Log the exact input to the LLM API @@ -978,10 +978,10 @@ class Logging(LiteLLMLoggingBaseClass): try: # [Non-blocking Extra Debug Information in metadata] if turn_off_message_logging is True: - _metadata[ - "raw_request" - ] = "redacted by litellm. \ + _metadata["raw_request"] = ( + "redacted by litellm. \ 'litellm.turn_off_message_logging=True'" + ) else: curl_command = self._get_request_curl_command( api_base=additional_args.get("api_base", ""), @@ -992,34 +992,34 @@ class Logging(LiteLLMLoggingBaseClass): _metadata["raw_request"] = str(curl_command) # split up, so it's easier to parse in the UI - self.model_call_details[ - "raw_request_typed_dict" - ] = RawRequestTypedDict( - raw_request_api_base=str( - additional_args.get("api_base") or "" - ), - raw_request_body=self._get_raw_request_body( - additional_args.get("complete_input_dict", {}) - ), - # NOTE: setting ignore_sensitive_headers to True will cause - # the Authorization header to be leaked when calls to the health - # endpoint are made and fail. - raw_request_headers=self._get_masked_headers( - additional_args.get("headers", {}) or {}, - ), - error=None, + self.model_call_details["raw_request_typed_dict"] = ( + RawRequestTypedDict( + raw_request_api_base=str( + additional_args.get("api_base") or "" + ), + raw_request_body=self._get_raw_request_body( + additional_args.get("complete_input_dict", {}) + ), + # NOTE: setting ignore_sensitive_headers to True will cause + # the Authorization header to be leaked when calls to the health + # endpoint are made and fail. + raw_request_headers=self._get_masked_headers( + additional_args.get("headers", {}) or {}, + ), + error=None, + ) ) except Exception as e: - self.model_call_details[ - "raw_request_typed_dict" - ] = RawRequestTypedDict( - error=str(e), + self.model_call_details["raw_request_typed_dict"] = ( + RawRequestTypedDict( + error=str(e), + ) ) - _metadata[ - "raw_request" - ] = "Unable to Log \ + _metadata["raw_request"] = ( + "Unable to Log \ raw request: {}".format( - str(e) + str(e) + ) ) if getattr(self, "logger_fn", None) and callable(self.logger_fn): try: @@ -1320,13 +1320,13 @@ class Logging(LiteLLMLoggingBaseClass): for callback in callbacks: try: if isinstance(callback, CustomLogger): - response: Optional[ - MCPPostCallResponseObject - ] = await callback.async_post_mcp_tool_call_hook( - kwargs=kwargs, - response_obj=post_mcp_tool_call_response_obj, - start_time=start_time, - end_time=end_time, + response: Optional[MCPPostCallResponseObject] = ( + await callback.async_post_mcp_tool_call_hook( + kwargs=kwargs, + response_obj=post_mcp_tool_call_response_obj, + start_time=start_time, + end_time=end_time, + ) ) ###################################################################### # if any of the callbacks modify the response, use the modified response @@ -1527,9 +1527,9 @@ class Logging(LiteLLMLoggingBaseClass): verbose_logger.debug( f"response_cost_failure_debug_information: {debug_info}" ) - self.model_call_details[ - "response_cost_failure_debug_information" - ] = debug_info + self.model_call_details["response_cost_failure_debug_information"] = ( + debug_info + ) return None try: @@ -1555,9 +1555,9 @@ class Logging(LiteLLMLoggingBaseClass): verbose_logger.debug( f"response_cost_failure_debug_information: {debug_info}" ) - self.model_call_details[ - "response_cost_failure_debug_information" - ] = debug_info + self.model_call_details["response_cost_failure_debug_information"] = ( + debug_info + ) return None @@ -1706,9 +1706,9 @@ class Logging(LiteLLMLoggingBaseClass): self.model_call_details["litellm_params"].setdefault("metadata", {}) if self.model_call_details["litellm_params"]["metadata"] is None: self.model_call_details["litellm_params"]["metadata"] = {} - self.model_call_details["litellm_params"]["metadata"][ - "hidden_params" - ] = getattr(logging_result, "_hidden_params", {}) + self.model_call_details["litellm_params"]["metadata"]["hidden_params"] = ( + getattr(logging_result, "_hidden_params", {}) + ) def _process_hidden_params_and_response_cost( self, @@ -1737,9 +1737,9 @@ class Logging(LiteLLMLoggingBaseClass): result=logging_result ) - self.model_call_details[ - "standard_logging_object" - ] = self._build_standard_logging_payload(logging_result, start_time, end_time) + self.model_call_details["standard_logging_object"] = ( + self._build_standard_logging_payload(logging_result, start_time, end_time) + ) if ( standard_logging_payload := self.model_call_details.get( @@ -1817,9 +1817,9 @@ class Logging(LiteLLMLoggingBaseClass): end_time = datetime.datetime.now() if self.completion_start_time is None: self.completion_start_time = end_time - self.model_call_details[ - "completion_start_time" - ] = self.completion_start_time + self.model_call_details["completion_start_time"] = ( + self.completion_start_time + ) self.model_call_details["log_event_type"] = "successful_api_call" self.model_call_details["end_time"] = end_time @@ -1856,10 +1856,10 @@ class Logging(LiteLLMLoggingBaseClass): end_time=end_time, ) elif isinstance(result, dict) or isinstance(result, list): - self.model_call_details[ - "standard_logging_object" - ] = self._build_standard_logging_payload( - result, start_time, end_time + self.model_call_details["standard_logging_object"] = ( + self._build_standard_logging_payload( + result, start_time, end_time + ) ) if ( standard_logging_payload := self.model_call_details.get( @@ -1868,9 +1868,9 @@ class Logging(LiteLLMLoggingBaseClass): ) is not None: emit_standard_logging_payload(standard_logging_payload) elif standard_logging_object is not None: - self.model_call_details[ - "standard_logging_object" - ] = standard_logging_object + self.model_call_details["standard_logging_object"] = ( + standard_logging_object + ) else: self.model_call_details["response_cost"] = None @@ -2028,20 +2028,20 @@ class Logging(LiteLLMLoggingBaseClass): verbose_logger.debug( "Logging Details LiteLLM-Success Call streaming complete" ) - self.model_call_details[ - "complete_streaming_response" - ] = complete_streaming_response - self.model_call_details[ - "response_cost" - ] = self._response_cost_calculator(result=complete_streaming_response) + self.model_call_details["complete_streaming_response"] = ( + complete_streaming_response + ) + self.model_call_details["response_cost"] = ( + self._response_cost_calculator(result=complete_streaming_response) + ) self._merge_hidden_params_from_response_into_metadata( complete_streaming_response ) ## STANDARDIZED LOGGING PAYLOAD - self.model_call_details[ - "standard_logging_object" - ] = self._build_standard_logging_payload( - complete_streaming_response, start_time, end_time + self.model_call_details["standard_logging_object"] = ( + self._build_standard_logging_payload( + complete_streaming_response, start_time, end_time + ) ) if ( standard_logging_payload := self.model_call_details.get( @@ -2375,10 +2375,10 @@ class Logging(LiteLLMLoggingBaseClass): ) else: if self.stream and complete_streaming_response: - self.model_call_details[ - "complete_response" - ] = self.model_call_details.get( - "complete_streaming_response", {} + self.model_call_details["complete_response"] = ( + self.model_call_details.get( + "complete_streaming_response", {} + ) ) result = self.model_call_details["complete_response"] openMeterLogger.log_success_event( @@ -2402,10 +2402,10 @@ class Logging(LiteLLMLoggingBaseClass): ) else: if self.stream and complete_streaming_response: - self.model_call_details[ - "complete_response" - ] = self.model_call_details.get( - "complete_streaming_response", {} + self.model_call_details["complete_response"] = ( + self.model_call_details.get( + "complete_streaming_response", {} + ) ) result = self.model_call_details["complete_response"] @@ -2544,9 +2544,9 @@ class Logging(LiteLLMLoggingBaseClass): if complete_streaming_response is not None: print_verbose("Async success callbacks: Got a complete streaming response") - self.model_call_details[ - "async_complete_streaming_response" - ] = complete_streaming_response + self.model_call_details["async_complete_streaming_response"] = ( + complete_streaming_response + ) try: if self.model_call_details.get("cache_hit", False) is True: @@ -2557,10 +2557,10 @@ class Logging(LiteLLMLoggingBaseClass): model_call_details=self.model_call_details ) # base_model defaults to None if not set on model_info - self.model_call_details[ - "response_cost" - ] = self._response_cost_calculator( - result=complete_streaming_response + self.model_call_details["response_cost"] = ( + self._response_cost_calculator( + result=complete_streaming_response + ) ) verbose_logger.debug( @@ -2577,10 +2577,10 @@ class Logging(LiteLLMLoggingBaseClass): ) ## STANDARDIZED LOGGING PAYLOAD - self.model_call_details[ - "standard_logging_object" - ] = self._build_standard_logging_payload( - complete_streaming_response, start_time, end_time + self.model_call_details["standard_logging_object"] = ( + self._build_standard_logging_payload( + complete_streaming_response, start_time, end_time + ) ) # print standard logging payload @@ -2607,9 +2607,9 @@ class Logging(LiteLLMLoggingBaseClass): # _success_handler_helper_fn if self.model_call_details.get("standard_logging_object") is None: ## STANDARDIZED LOGGING PAYLOAD - self.model_call_details[ - "standard_logging_object" - ] = self._build_standard_logging_payload(result, start_time, end_time) + self.model_call_details["standard_logging_object"] = ( + self._build_standard_logging_payload(result, start_time, end_time) + ) # print standard logging payload if ( @@ -2852,18 +2852,18 @@ class Logging(LiteLLMLoggingBaseClass): ## STANDARDIZED LOGGING PAYLOAD - self.model_call_details[ - "standard_logging_object" - ] = get_standard_logging_object_payload( - kwargs=self.model_call_details, - init_response_obj={}, - start_time=start_time, - end_time=end_time, - logging_obj=self, - status="failure", - error_str=str(exception), - original_exception=exception, - standard_built_in_tools_params=self.standard_built_in_tools_params, + self.model_call_details["standard_logging_object"] = ( + get_standard_logging_object_payload( + kwargs=self.model_call_details, + init_response_obj={}, + start_time=start_time, + end_time=end_time, + logging_obj=self, + status="failure", + error_str=str(exception), + original_exception=exception, + standard_built_in_tools_params=self.standard_built_in_tools_params, + ) ) return start_time, end_time @@ -3833,9 +3833,9 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915 service_name=arize_config.project_name, ) - os.environ[ - "OTEL_EXPORTER_OTLP_TRACES_HEADERS" - ] = f"space_id={arize_config.space_key or arize_config.space_id},api_key={arize_config.api_key}" + os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = ( + f"space_id={arize_config.space_key or arize_config.space_id},api_key={arize_config.api_key}" + ) for callback in _in_memory_loggers: if ( isinstance(callback, ArizeLogger) @@ -3861,13 +3861,13 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915 existing_attrs = os.environ.get("OTEL_RESOURCE_ATTRIBUTES", "") # Add openinference.project.name attribute if existing_attrs: - os.environ[ - "OTEL_RESOURCE_ATTRIBUTES" - ] = f"{existing_attrs},openinference.project.name={arize_phoenix_config.project_name}" + os.environ["OTEL_RESOURCE_ATTRIBUTES"] = ( + f"{existing_attrs},openinference.project.name={arize_phoenix_config.project_name}" + ) else: - os.environ[ - "OTEL_RESOURCE_ATTRIBUTES" - ] = f"openinference.project.name={arize_phoenix_config.project_name}" + os.environ["OTEL_RESOURCE_ATTRIBUTES"] = ( + f"openinference.project.name={arize_phoenix_config.project_name}" + ) # Set Phoenix project name from environment variable phoenix_project_name = os.environ.get("PHOENIX_PROJECT_NAME", None) @@ -3875,19 +3875,19 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915 existing_attrs = os.environ.get("OTEL_RESOURCE_ATTRIBUTES", "") # Add openinference.project.name attribute if existing_attrs: - os.environ[ - "OTEL_RESOURCE_ATTRIBUTES" - ] = f"{existing_attrs},openinference.project.name={phoenix_project_name}" + os.environ["OTEL_RESOURCE_ATTRIBUTES"] = ( + f"{existing_attrs},openinference.project.name={phoenix_project_name}" + ) else: - os.environ[ - "OTEL_RESOURCE_ATTRIBUTES" - ] = f"openinference.project.name={phoenix_project_name}" + os.environ["OTEL_RESOURCE_ATTRIBUTES"] = ( + f"openinference.project.name={phoenix_project_name}" + ) # auth can be disabled on local deployments of arize phoenix if arize_phoenix_config.otlp_auth_headers is not None: - os.environ[ - "OTEL_EXPORTER_OTLP_TRACES_HEADERS" - ] = arize_phoenix_config.otlp_auth_headers + os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = ( + arize_phoenix_config.otlp_auth_headers + ) for callback in _in_memory_loggers: if ( @@ -4074,9 +4074,9 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915 exporter="otlp_http", endpoint="https://langtrace.ai/api/trace", ) - os.environ[ - "OTEL_EXPORTER_OTLP_TRACES_HEADERS" - ] = f"api_key={os.getenv('LANGTRACE_API_KEY')}" + os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = ( + f"api_key={os.getenv('LANGTRACE_API_KEY')}" + ) for callback in _in_memory_loggers: if ( isinstance(callback, OpenTelemetry) @@ -5001,10 +5001,10 @@ class StandardLoggingPayloadSetup: for key in StandardLoggingHiddenParams.__annotations__.keys(): if key in hidden_params: if key == "additional_headers": - clean_hidden_params[ - "additional_headers" - ] = StandardLoggingPayloadSetup.get_additional_headers( - hidden_params[key] + clean_hidden_params["additional_headers"] = ( + StandardLoggingPayloadSetup.get_additional_headers( + hidden_params[key] + ) ) else: clean_hidden_params[key] = hidden_params[key] # type: ignore @@ -5644,9 +5644,9 @@ def scrub_sensitive_keys_in_metadata(litellm_params: Optional[dict]): ): for k, v in metadata["user_api_key_metadata"].items(): if k == "logging": # prevent logging user logging keys - cleaned_user_api_key_metadata[ - k - ] = "scrubbed_by_litellm_for_sensitive_keys" + cleaned_user_api_key_metadata[k] = ( + "scrubbed_by_litellm_for_sensitive_keys" + ) else: cleaned_user_api_key_metadata[k] = v diff --git a/litellm/litellm_core_utils/llm_request_utils.py b/litellm/litellm_core_utils/llm_request_utils.py index dc70069ac5a..f5f28822ca1 100644 --- a/litellm/litellm_core_utils/llm_request_utils.py +++ b/litellm/litellm_core_utils/llm_request_utils.py @@ -56,8 +56,9 @@ def pick_cheapest_chat_models_from_llm_provider(custom_llm_provider: str, n=1): continue if model_info.get("mode") != "chat": continue - _cost = (model_info.get("input_cost_per_token") or 0.0) + (model_info.get( - "output_cost_per_token") or 0.0) + _cost = (model_info.get("input_cost_per_token") or 0.0) + ( + model_info.get("output_cost_per_token") or 0.0 + ) model_costs.append((model, _cost)) # Sort by cost (ascending) diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py index 20cc5746667..78378faa262 100644 --- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py +++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py @@ -596,9 +596,9 @@ def convert_to_model_response_object( # noqa: PLR0915 provider_specific_fields["thinking_blocks"] = thinking_blocks if reasoning_content: - provider_specific_fields[ - "reasoning_content" - ] = reasoning_content + provider_specific_fields["reasoning_content"] = ( + reasoning_content + ) message = Message( content=content, @@ -787,9 +787,9 @@ def convert_to_model_response_object( # noqa: PLR0915 # tracking without exposing it in the response body. Must be set # after hidden_params assignment to avoid being overwritten. if "_audio_transcription_duration" in response_object: - model_response_object._hidden_params[ - "audio_transcription_duration" - ] = response_object["_audio_transcription_duration"] + model_response_object._hidden_params["audio_transcription_duration"] = ( + response_object["_audio_transcription_duration"] + ) if _response_headers is not None: model_response_object._response_headers = _response_headers diff --git a/litellm/litellm_core_utils/model_param_helper.py b/litellm/litellm_core_utils/model_param_helper.py index 66b174feac4..4d45c47c224 100644 --- a/litellm/litellm_core_utils/model_param_helper.py +++ b/litellm/litellm_core_utils/model_param_helper.py @@ -93,9 +93,9 @@ class ModelParamHelper: streaming_params: Set[str] = set( getattr(CompletionCreateParamsStreaming, "__annotations__", {}).keys() ) - litellm_provider_specific_params: Set[ - str - ] = ModelParamHelper.get_litellm_provider_specific_params_for_chat_params() + litellm_provider_specific_params: Set[str] = ( + ModelParamHelper.get_litellm_provider_specific_params_for_chat_params() + ) all_chat_completion_kwargs: Set[str] = non_streaming_params.union( streaming_params ).union(litellm_provider_specific_params) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index d29ca1649ff..461c72f202f 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -1393,10 +1393,10 @@ def convert_to_gemini_tool_call_invoke( if tool_calls is not None: for idx, tool in enumerate(tool_calls): if "function" in tool: - gemini_function_call: Optional[ - VertexFunctionCall - ] = _gemini_tool_call_invoke_helper( - function_call_params=tool["function"] + gemini_function_call: Optional[VertexFunctionCall] = ( + _gemini_tool_call_invoke_helper( + function_call_params=tool["function"] + ) ) if gemini_function_call is not None: part_dict: VertexPartType = { @@ -1574,9 +1574,7 @@ def convert_to_gemini_tool_call_result( # noqa: PLR0915 file_data = ( file_content.get("file_data", "") if isinstance(file_content, dict) - else file_content - if isinstance(file_content, str) - else "" + else file_content if isinstance(file_content, str) else "" ) if file_data: @@ -2081,9 +2079,9 @@ def _sanitize_empty_text_content( if isinstance(content, str): if not content or not content.strip(): message = cast(AllMessageValues, dict(message)) # Make a copy - message[ - "content" - ] = "[System: Empty message content sanitised to satisfy protocol]" + message["content"] = ( + "[System: Empty message content sanitised to satisfy protocol]" + ) verbose_logger.debug( f"_sanitize_empty_text_content: Replaced empty text content in {message.get('role')} message" ) @@ -2423,9 +2421,9 @@ def anthropic_messages_pt( # noqa: PLR0915 # Convert ChatCompletionImageUrlObject to dict if needed image_url_value = m["image_url"] if isinstance(image_url_value, str): - image_url_input: Union[ - str, dict[str, Any] - ] = image_url_value + image_url_input: Union[str, dict[str, Any]] = ( + image_url_value + ) else: # ChatCompletionImageUrlObject or dict case - convert to dict image_url_input = { @@ -2452,9 +2450,9 @@ def anthropic_messages_pt( # noqa: PLR0915 ) if "cache_control" in _content_element: - _anthropic_content_element[ - "cache_control" - ] = _content_element["cache_control"] + _anthropic_content_element["cache_control"] = ( + _content_element["cache_control"] + ) user_content.append(_anthropic_content_element) elif m.get("type", "") == "text": m = cast(ChatCompletionTextObject, m) @@ -2514,9 +2512,9 @@ def anthropic_messages_pt( # noqa: PLR0915 ) if "cache_control" in _content_element: - _anthropic_content_text_element[ - "cache_control" - ] = _content_element["cache_control"] + _anthropic_content_text_element["cache_control"] = ( + _content_element["cache_control"] + ) user_content.append(_anthropic_content_text_element) @@ -2649,9 +2647,9 @@ def anthropic_messages_pt( # noqa: PLR0915 original_content_element=dict(assistant_content_block), ) if "cache_control" in _content_element: - _anthropic_text_content_element[ - "cache_control" - ] = _content_element["cache_control"] + _anthropic_text_content_element["cache_control"] = ( + _content_element["cache_control"] + ) text_element = _anthropic_text_content_element # Interleave: each thinking block precedes its server tool group. @@ -2811,9 +2809,9 @@ def anthropic_messages_pt( # noqa: PLR0915 ) if "cache_control" in _content_element: - _anthropic_text_content_element[ - "cache_control" - ] = _content_element["cache_control"] + _anthropic_text_content_element["cache_control"] = ( + _content_element["cache_control"] + ) assistant_content.append(_anthropic_text_content_element) diff --git a/litellm/litellm_core_utils/realtime_streaming.py b/litellm/litellm_core_utils/realtime_streaming.py index 37233680714..4493a58f78b 100644 --- a/litellm/litellm_core_utils/realtime_streaming.py +++ b/litellm/litellm_core_utils/realtime_streaming.py @@ -199,12 +199,12 @@ class RealTimeStreaming: if self.input_messages: self.logging_obj.model_call_details["messages"] = self.input_messages if self.session_tools or self.tool_calls: - self.logging_obj.model_call_details[ - "realtime_tools" - ] = self.session_tools - self.logging_obj.model_call_details[ - "realtime_tool_calls" - ] = self.tool_calls + self.logging_obj.model_call_details["realtime_tools"] = ( + self.session_tools + ) + self.logging_obj.model_call_details["realtime_tool_calls"] = ( + self.tool_calls + ) ## ASYNC LOGGING # Create an event loop for the new thread asyncio.create_task(self.logging_obj.async_success_handler(self.messages)) diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index dbeb4111077..f3f560b33b9 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -285,9 +285,9 @@ def _get_turn_off_message_logging_from_dynamic_params( handles boolean and string values of `turn_off_message_logging` """ - standard_callback_dynamic_params: Optional[ - StandardCallbackDynamicParams - ] = model_call_details.get("standard_callback_dynamic_params", None) + standard_callback_dynamic_params: Optional[StandardCallbackDynamicParams] = ( + model_call_details.get("standard_callback_dynamic_params", None) + ) if standard_callback_dynamic_params: _turn_off_message_logging = standard_callback_dynamic_params.get( "turn_off_message_logging" diff --git a/litellm/litellm_core_utils/safe_json_loads.py b/litellm/litellm_core_utils/safe_json_loads.py index bb4b72cfd97..b0a8e57d552 100644 --- a/litellm/litellm_core_utils/safe_json_loads.py +++ b/litellm/litellm_core_utils/safe_json_loads.py @@ -1,6 +1,7 @@ """ Helper for safe JSON loading in LiteLLM. """ + from typing import Any import json diff --git a/litellm/litellm_core_utils/specialty_caches/dynamic_logging_cache.py b/litellm/litellm_core_utils/specialty_caches/dynamic_logging_cache.py index c2acc708bb5..13341f27a61 100644 --- a/litellm/litellm_core_utils/specialty_caches/dynamic_logging_cache.py +++ b/litellm/litellm_core_utils/specialty_caches/dynamic_logging_cache.py @@ -7,6 +7,7 @@ This ensures we do 1. Proper cleanup of Langfuse initialized clients. 2. Re-use created langfuse clients. """ + import hashlib import json from typing import Any, Optional diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 1935372e5df..0829010ddb0 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -160,9 +160,9 @@ class ChunkProcessor: self, tool_call_chunks: List[Dict[str, Any]] ) -> List[ChatCompletionMessageToolCall]: tool_calls_list: List[ChatCompletionMessageToolCall] = [] - tool_call_map: Dict[ - int, Dict[str, Any] - ] = {} # Map to store tool calls by index + tool_call_map: Dict[int, Dict[str, Any]] = ( + {} + ) # Map to store tool calls by index for chunk in tool_call_chunks: choices = chunk["choices"] @@ -643,12 +643,12 @@ class ChunkProcessor: web_search_requests: Optional[int] = calculated_usage_per_chunk[ "web_search_requests" ] - completion_tokens_details: Optional[ - CompletionTokensDetails - ] = calculated_usage_per_chunk["completion_tokens_details"] - prompt_tokens_details: Optional[ - PromptTokensDetailsWrapper - ] = calculated_usage_per_chunk["prompt_tokens_details"] + completion_tokens_details: Optional[CompletionTokensDetails] = ( + calculated_usage_per_chunk["completion_tokens_details"] + ) + prompt_tokens_details: Optional[PromptTokensDetailsWrapper] = ( + calculated_usage_per_chunk["prompt_tokens_details"] + ) try: returned_usage.prompt_tokens = prompt_tokens or token_counter( diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index e402023d240..2c1ec00b8c9 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -127,9 +127,9 @@ class CustomStreamWrapper: self.system_fingerprint: Optional[str] = None self.received_finish_reason: Optional[str] = None - self.intermittent_finish_reason: Optional[ - str - ] = None # finish reasons that show up mid-stream + self.intermittent_finish_reason: Optional[str] = ( + None # finish reasons that show up mid-stream + ) self.special_tokens = [ "<|assistant|>", "<|system|>", @@ -1520,9 +1520,9 @@ class CustomStreamWrapper: t.function.arguments = "" _json_delta = delta.model_dump() if "role" not in _json_delta or _json_delta["role"] is None: - _json_delta[ - "role" - ] = "assistant" # mistral's api returns role as None + _json_delta["role"] = ( + "assistant" # mistral's api returns role as None + ) if "tool_calls" in _json_delta and isinstance( _json_delta["tool_calls"], list ): diff --git a/litellm/llms/a2a/__init__.py b/litellm/llms/a2a/__init__.py index 043efa5e8bf..340f45dbab6 100644 --- a/litellm/llms/a2a/__init__.py +++ b/litellm/llms/a2a/__init__.py @@ -1,6 +1,7 @@ """ A2A (Agent-to-Agent) Protocol Provider for LiteLLM """ + from .chat.transformation import A2AConfig __all__ = ["A2AConfig"] diff --git a/litellm/llms/a2a/chat/__init__.py b/litellm/llms/a2a/chat/__init__.py index 76bf4dd71d9..c7cc8a7b0da 100644 --- a/litellm/llms/a2a/chat/__init__.py +++ b/litellm/llms/a2a/chat/__init__.py @@ -1,6 +1,7 @@ """ A2A Chat Completion Implementation """ + from .transformation import A2AConfig __all__ = ["A2AConfig"] diff --git a/litellm/llms/a2a/chat/streaming_iterator.py b/litellm/llms/a2a/chat/streaming_iterator.py index 72902f65f7c..29167d89ae7 100644 --- a/litellm/llms/a2a/chat/streaming_iterator.py +++ b/litellm/llms/a2a/chat/streaming_iterator.py @@ -1,6 +1,7 @@ """ A2A Streaming Response Iterator """ + from typing import Optional, Union from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIterator diff --git a/litellm/llms/a2a/chat/transformation.py b/litellm/llms/a2a/chat/transformation.py index d0887028632..b9c9f944b3e 100644 --- a/litellm/llms/a2a/chat/transformation.py +++ b/litellm/llms/a2a/chat/transformation.py @@ -1,6 +1,7 @@ """ A2A Protocol Transformation for LiteLLM """ + import uuid from typing import Any, Dict, Iterator, List, Optional, Union diff --git a/litellm/llms/a2a/common_utils.py b/litellm/llms/a2a/common_utils.py index aa817ce0fe6..15ea9f01abd 100644 --- a/litellm/llms/a2a/common_utils.py +++ b/litellm/llms/a2a/common_utils.py @@ -1,6 +1,7 @@ """ Common utilities for A2A (Agent-to-Agent) Protocol """ + from typing import Any, Dict, List from pydantic import BaseModel diff --git a/litellm/llms/amazon_nova/chat/transformation.py b/litellm/llms/amazon_nova/chat/transformation.py index 0fd08e62872..74c7fd234fe 100644 --- a/litellm/llms/amazon_nova/chat/transformation.py +++ b/litellm/llms/amazon_nova/chat/transformation.py @@ -1,6 +1,7 @@ """ Translate from OpenAI's `/v1/chat/completions` to Amazon Nova's `/v1/chat/completions` """ + from typing import Any, List, Optional, Tuple import httpx diff --git a/litellm/llms/anthropic/batches/transformation.py b/litellm/llms/anthropic/batches/transformation.py index 98c0588a091..3f03c744efe 100644 --- a/litellm/llms/anthropic/batches/transformation.py +++ b/litellm/llms/anthropic/batches/transformation.py @@ -229,12 +229,12 @@ class AnthropicBatchesConfig(BaseBatchesConfig): completed_at=ended_at if processing_status == "ended" else None, failed_at=None, expired_at=archived_at if archived_at else None, - cancelling_at=cancel_initiated_at - if processing_status == "canceling" - else None, - cancelled_at=ended_at - if processing_status == "canceling" and ended_at - else None, + cancelling_at=( + cancel_initiated_at if processing_status == "canceling" else None + ), + cancelled_at=( + ended_at if processing_status == "canceling" and ended_at else None + ), request_counts=request_counts, metadata={}, ) diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index d31a0a091e9..cd1a3309df2 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -87,9 +87,9 @@ class AnthropicMessagesHandler(BaseTranslation): texts_to_check: List[str] = [] images_to_check: List[str] = [] - tools_to_check: List[ - ChatCompletionToolParam - ] = chat_completion_compatible_request.get("tools", []) + tools_to_check: List[ChatCompletionToolParam] = ( + chat_completion_compatible_request.get("tools", []) + ) task_mappings: List[Tuple[int, Optional[int]]] = [] # Track (message_index, content_index) for each text # content_index is None for string content, int for list content diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.py index 9f2ddcae2c7..a2389f44295 100644 --- a/litellm/llms/anthropic/chat/handler.py +++ b/litellm/llms/anthropic/chat/handler.py @@ -578,9 +578,7 @@ class ModelResponseIterator: speed=self.speed, ) - def _content_block_delta_helper( - self, chunk: dict - ) -> Tuple[ + def _content_block_delta_helper(self, chunk: dict) -> Tuple[ str, Optional[ChatCompletionToolCallChunk], List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]], @@ -805,9 +803,9 @@ class ModelResponseIterator: tool_input = content_block_start["content_block"].get( "input", {} ) - self._server_tool_inputs[ - self._current_server_tool_id - ] = tool_input + self._server_tool_inputs[self._current_server_tool_id] = ( + tool_input + ) # Include caller information if present (for programmatic tool calling) if "caller" in content_block_start["content_block"]: caller_data = content_block_start["content_block"]["caller"] @@ -828,9 +826,9 @@ class ModelResponseIterator: # Handle compaction blocks # The full content comes in content_block_start self.compaction_blocks.append(content_block_start["content_block"]) - provider_specific_fields[ - "compaction_blocks" - ] = self.compaction_blocks + provider_specific_fields["compaction_blocks"] = ( + self.compaction_blocks + ) provider_specific_fields["compaction_start"] = { "type": "compaction", "content": content_block_start["content_block"].get( @@ -852,9 +850,9 @@ class ModelResponseIterator: self.web_search_results.append( content_block_start["content_block"] ) - provider_specific_fields[ - "web_search_results" - ] = self.web_search_results + provider_specific_fields["web_search_results"] = ( + self.web_search_results + ) elif content_type == "web_fetch_tool_result": # Capture web_fetch_tool_result for multi-turn reconstruction # The full content comes in content_block_start, not in deltas @@ -862,18 +860,18 @@ class ModelResponseIterator: self.web_search_results.append( content_block_start["content_block"] ) - provider_specific_fields[ - "web_search_results" - ] = self.web_search_results + provider_specific_fields["web_search_results"] = ( + self.web_search_results + ) elif content_type != "tool_search_tool_result": # Handle other tool results (code execution, etc.) # Skip tool_search_tool_result as it's internal metadata self.tool_results.append(content_block_start["content_block"]) provider_specific_fields["tool_results"] = self.tool_results # Convert to provider-neutral code_interpreter_results - provider_specific_fields[ - "code_interpreter_results" - ] = self._build_code_interpreter_results() + provider_specific_fields["code_interpreter_results"] = ( + self._build_code_interpreter_results() + ) elif type_chunk == "content_block_stop": ContentBlockStop(**chunk) # type: ignore @@ -930,9 +928,9 @@ class ModelResponseIterator: ) if container_id and self.tool_results: self._container_id = container_id - provider_specific_fields[ - "code_interpreter_results" - ] = self._build_code_interpreter_results() + provider_specific_fields["code_interpreter_results"] = ( + self._build_code_interpreter_results() + ) elif type_chunk == "message_start": """ Anthropic diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 9a99f9efc82..375973d5c96 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -964,11 +964,11 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if mcp_servers: optional_params["mcp_servers"] = mcp_servers elif param == "tool_choice" or param == "parallel_tool_calls": - _tool_choice: Optional[ - AnthropicMessagesToolChoice - ] = self._map_tool_choice( - tool_choice=non_default_params.get("tool_choice"), - parallel_tool_use=non_default_params.get("parallel_tool_calls"), + _tool_choice: Optional[AnthropicMessagesToolChoice] = ( + self._map_tool_choice( + tool_choice=non_default_params.get("tool_choice"), + parallel_tool_use=non_default_params.get("parallel_tool_calls"), + ) ) if _tool_choice is not None: @@ -1066,9 +1066,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): self.map_openai_context_management_to_anthropic(value) ) if anthropic_context_management is not None: - optional_params[ - "context_management" - ] = anthropic_context_management + optional_params["context_management"] = ( + anthropic_context_management + ) elif param == "speed" and isinstance(value, str): # Pass through Anthropic-specific speed parameter for fast mode optional_params["speed"] = value @@ -1142,9 +1142,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): text=system_message_block["content"], ) if "cache_control" in system_message_block: - anthropic_system_message_content[ - "cache_control" - ] = system_message_block["cache_control"] + anthropic_system_message_content["cache_control"] = ( + system_message_block["cache_control"] + ) anthropic_system_message_list.append( anthropic_system_message_content ) @@ -1168,9 +1168,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) ) if "cache_control" in _content: - anthropic_system_message_content[ - "cache_control" - ] = _content["cache_control"] + anthropic_system_message_content["cache_control"] = ( + _content["cache_control"] + ) anthropic_system_message_list.append( anthropic_system_message_content @@ -1477,9 +1477,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) return _message - def extract_response_content( - self, completion_response: dict - ) -> Tuple[ + def extract_response_content(self, completion_response: dict) -> Tuple[ str, Optional[List[Any]], Optional[ @@ -1773,9 +1771,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): code_interpreter_results = self._build_code_interpreter_results( tool_results, code_by_id, container_id ) - provider_specific_fields[ - "code_interpreter_results" - ] = code_interpreter_results + provider_specific_fields["code_interpreter_results"] = ( + code_interpreter_results + ) container = completion_response.get("container") if container is not None: diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 7d2d0a74961..6ece0079a3d 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -464,9 +464,9 @@ class AnthropicModelInfo(BaseLLMModelInfo): if web_search_tool_used: from litellm.types.llms.anthropic import ANTHROPIC_BETA_HEADER_VALUES - headers[ - "anthropic-beta" - ] = ANTHROPIC_BETA_HEADER_VALUES.WEB_SEARCH_2025_03_05.value + headers["anthropic-beta"] = ( + ANTHROPIC_BETA_HEADER_VALUES.WEB_SEARCH_2025_03_05.value + ) elif len(betas) > 0: headers["anthropic-beta"] = ",".join(betas) diff --git a/litellm/llms/anthropic/completion/transformation.py b/litellm/llms/anthropic/completion/transformation.py index 576ddb57fb1..a8798cd5d0e 100644 --- a/litellm/llms/anthropic/completion/transformation.py +++ b/litellm/llms/anthropic/completion/transformation.py @@ -55,9 +55,9 @@ class AnthropicTextConfig(BaseConfig): to pass metadata to anthropic, it's {"user_id": "any-relevant-information"} """ - max_tokens_to_sample: Optional[ - int - ] = litellm.max_tokens # anthropic requires a default + max_tokens_to_sample: Optional[int] = ( + litellm.max_tokens + ) # anthropic requires a default stop_sequences: Optional[list] = None temperature: Optional[int] = None top_p: Optional[int] = None diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py index 6bddad09f21..3a6f0dbb88e 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py @@ -282,16 +282,16 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper): hasattr(chunk.usage, "_cache_creation_input_tokens") and chunk.usage._cache_creation_input_tokens > 0 ): - usage_dict[ - "cache_creation_input_tokens" - ] = chunk.usage._cache_creation_input_tokens + usage_dict["cache_creation_input_tokens"] = ( + chunk.usage._cache_creation_input_tokens + ) if ( hasattr(chunk.usage, "_cache_read_input_tokens") and chunk.usage._cache_read_input_tokens > 0 ): - usage_dict[ - "cache_read_input_tokens" - ] = chunk.usage._cache_read_input_tokens + usage_dict["cache_read_input_tokens"] = ( + chunk.usage._cache_read_input_tokens + ) merged_chunk["usage"] = usage_dict # Queue the merged chunk and reset diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py index ed49943b7fe..9803ddbb029 100644 --- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py @@ -550,9 +550,9 @@ class LiteLLMAnthropicMessagesAdapter: ## ASSISTANT MESSAGE ## assistant_message_str: Optional[str] = None - assistant_content_list: List[ - Dict[str, Any] - ] = [] # For content blocks with cache_control + assistant_content_list: List[Dict[str, Any]] = ( + [] + ) # For content blocks with cache_control has_cache_control_in_text = False tool_calls: List[ChatCompletionAssistantToolCall] = [] thinking_blocks: List[ @@ -595,12 +595,12 @@ class LiteLLMAnthropicMessagesAdapter: function_chunk.get("provider_specific_fields") or {} ) - provider_specific_fields[ - "thought_signature" - ] = signature - function_chunk[ - "provider_specific_fields" - ] = provider_specific_fields + provider_specific_fields["thought_signature"] = ( + signature + ) + function_chunk["provider_specific_fields"] = ( + provider_specific_fields + ) tool_call = ChatCompletionAssistantToolCall( id=content.get("id", ""), @@ -1334,9 +1334,9 @@ class LiteLLMAnthropicMessagesAdapter: hasattr(usage, "_cache_creation_input_tokens") and usage._cache_creation_input_tokens > 0 ): - anthropic_usage[ - "cache_creation_input_tokens" - ] = usage._cache_creation_input_tokens + anthropic_usage["cache_creation_input_tokens"] = ( + usage._cache_creation_input_tokens + ) if cached_tokens > 0: anthropic_usage["cache_read_input_tokens"] = cached_tokens @@ -1513,9 +1513,9 @@ class LiteLLMAnthropicMessagesAdapter: hasattr(litellm_usage_chunk, "_cache_creation_input_tokens") and litellm_usage_chunk._cache_creation_input_tokens > 0 ): - usage_delta[ - "cache_creation_input_tokens" - ] = litellm_usage_chunk._cache_creation_input_tokens + usage_delta["cache_creation_input_tokens"] = ( + litellm_usage_chunk._cache_creation_input_tokens + ) if cached_tokens > 0: usage_delta["cache_read_input_tokens"] = cached_tokens else: diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py b/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py index 7fc9b00f2c7..f704ed2c9d1 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py @@ -122,7 +122,10 @@ class FakeAnthropicMessagesStreamIterator: content_block_delta = { "type": "content_block_delta", "index": index, - "delta": {"type": "input_json_delta", "partial_json": json.dumps(input_data)}, + "delta": { + "type": "input_json_delta", + "partial_json": json.dumps(input_data), + }, } chunks.append( f"event: content_block_delta\ndata: {json.dumps(content_block_delta)}\n\n".encode() diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py index 9b60a58260b..c67cb492a66 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py @@ -208,9 +208,9 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): ) ) if transformed_context_management is not None: - anthropic_messages_optional_request_params[ - "context_management" - ] = transformed_context_management + anthropic_messages_optional_request_params["context_management"] = ( + transformed_context_management + ) ####### get required params for all anthropic messages requests ###### verbose_logger.debug(f"TRANSFORMATION DEBUG - Messages: {messages}") diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py index aa0738a0719..94c5200be64 100644 --- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py @@ -35,9 +35,9 @@ class AnthropicResponsesStreamWrapper: # Map item_id -> content_block_index so we can stop the right block later self._item_id_to_block_index: Dict[str, int] = {} # Track open function_call items by item_id so we can emit tool_use start - self._pending_tool_ids: Dict[ - str, str - ] = {} # item_id -> call_id / name accumulator + self._pending_tool_ids: Dict[str, str] = ( + {} + ) # item_id -> call_id / name accumulator self._sent_message_start = False self._sent_message_stop = False self._chunk_queue: deque = deque() diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py index dae7044a5bc..913470e7088 100644 --- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py @@ -337,10 +337,10 @@ class LiteLLMAnthropicToResponsesAPIAdapter: # tool_choice tool_choice = anthropic_request.get("tool_choice") if tool_choice: - responses_kwargs[ - "tool_choice" - ] = self.translate_tool_choice_to_responses_api( - cast(AnthropicMessagesToolChoice, tool_choice) + responses_kwargs["tool_choice"] = ( + self.translate_tool_choice_to_responses_api( + cast(AnthropicMessagesToolChoice, tool_choice) + ) ) # thinking -> reasoning diff --git a/litellm/llms/anthropic/files/transformation.py b/litellm/llms/anthropic/files/transformation.py index 0545cefb071..aeaab4e57bf 100644 --- a/litellm/llms/anthropic/files/transformation.py +++ b/litellm/llms/anthropic/files/transformation.py @@ -79,9 +79,9 @@ class AnthropicFilesConfig(BaseFilesConfig): return AnthropicError( status_code=status_code, message=error_message, - headers=cast(httpx.Headers, headers) - if isinstance(headers, dict) - else headers, + headers=( + cast(httpx.Headers, headers) if isinstance(headers, dict) else headers + ), ) def validate_environment( diff --git a/litellm/llms/azure/fine_tuning/handler.py b/litellm/llms/azure/fine_tuning/handler.py index 7e225a84454..07d6455a6fb 100644 --- a/litellm/llms/azure/fine_tuning/handler.py +++ b/litellm/llms/azure/fine_tuning/handler.py @@ -218,7 +218,14 @@ class AzureOpenAIFineTuningAPI(OpenAIFineTuningAPI, BaseAzureLLM): _is_async: bool = False, api_version: Optional[str] = None, litellm_params: Optional[dict] = None, - ) -> Optional[Union[OpenAI, AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI,]]: + ) -> Optional[ + Union[ + OpenAI, + AsyncOpenAI, + AzureOpenAI, + AsyncAzureOpenAI, + ] + ]: # Override to use Azure-specific client initialization if isinstance(client, OpenAI) or isinstance(client, AsyncOpenAI): client = None diff --git a/litellm/llms/azure_ai/anthropic/__init__.py b/litellm/llms/azure_ai/anthropic/__init__.py index 931c71de3b3..5ec22703aec 100644 --- a/litellm/llms/azure_ai/anthropic/__init__.py +++ b/litellm/llms/azure_ai/anthropic/__init__.py @@ -1,6 +1,7 @@ """ Azure Anthropic provider - supports Claude models via Azure Foundry """ + from .handler import AzureAnthropicChatCompletion from .transformation import AzureAnthropicConfig diff --git a/litellm/llms/azure_ai/anthropic/handler.py b/litellm/llms/azure_ai/anthropic/handler.py index a2263e72a14..f3a50b73c1a 100644 --- a/litellm/llms/azure_ai/anthropic/handler.py +++ b/litellm/llms/azure_ai/anthropic/handler.py @@ -1,6 +1,7 @@ """ Azure Anthropic handler - reuses AnthropicChatCompletion logic with Azure authentication """ + import copy import json from typing import TYPE_CHECKING, Callable, Union diff --git a/litellm/llms/azure_ai/anthropic/messages_transformation.py b/litellm/llms/azure_ai/anthropic/messages_transformation.py index 59d8fb02c6d..a81218ab76a 100644 --- a/litellm/llms/azure_ai/anthropic/messages_transformation.py +++ b/litellm/llms/azure_ai/anthropic/messages_transformation.py @@ -1,6 +1,7 @@ """ Azure Anthropic messages transformation config - extends AnthropicMessagesConfig with Azure authentication """ + from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple from litellm.llms.anthropic.experimental_pass_through.messages.transformation import ( diff --git a/litellm/llms/azure_ai/anthropic/transformation.py b/litellm/llms/azure_ai/anthropic/transformation.py index 5d8f27b97df..e935aa1c057 100644 --- a/litellm/llms/azure_ai/anthropic/transformation.py +++ b/litellm/llms/azure_ai/anthropic/transformation.py @@ -1,6 +1,7 @@ """ Azure Anthropic transformation config - extends AnthropicConfig with Azure authentication """ + from typing import TYPE_CHECKING, Dict, List, Optional, Union from litellm.llms.anthropic.chat.transformation import AnthropicConfig from litellm.llms.azure.common_utils import BaseAzureLLM diff --git a/litellm/llms/azure_ai/azure_model_router/__init__.py b/litellm/llms/azure_ai/azure_model_router/__init__.py index 0165d60b643..bbee759459f 100644 --- a/litellm/llms/azure_ai/azure_model_router/__init__.py +++ b/litellm/llms/azure_ai/azure_model_router/__init__.py @@ -1,4 +1,5 @@ """Azure AI Foundry Model Router support.""" + from .transformation import AzureModelRouterConfig __all__ = ["AzureModelRouterConfig"] diff --git a/litellm/llms/azure_ai/azure_model_router/transformation.py b/litellm/llms/azure_ai/azure_model_router/transformation.py index 57acb147063..e4174f41ad7 100644 --- a/litellm/llms/azure_ai/azure_model_router/transformation.py +++ b/litellm/llms/azure_ai/azure_model_router/transformation.py @@ -4,6 +4,7 @@ Transformation for Azure AI Foundry Model Router. The Model Router is a special Azure AI deployment that automatically routes requests to the best available model. It has specific cost tracking requirements. """ + from typing import Any, List, Optional from httpx import Response diff --git a/litellm/llms/azure_ai/ocr/__init__.py b/litellm/llms/azure_ai/ocr/__init__.py index e49217a5baf..ade1165b848 100644 --- a/litellm/llms/azure_ai/ocr/__init__.py +++ b/litellm/llms/azure_ai/ocr/__init__.py @@ -1,4 +1,5 @@ """Azure AI OCR module.""" + from .common_utils import get_azure_ai_ocr_config from .document_intelligence.transformation import ( AzureDocumentIntelligenceOCRConfig, diff --git a/litellm/llms/azure_ai/ocr/document_intelligence/__init__.py b/litellm/llms/azure_ai/ocr/document_intelligence/__init__.py index fb14fbbf0ac..32d700fd195 100644 --- a/litellm/llms/azure_ai/ocr/document_intelligence/__init__.py +++ b/litellm/llms/azure_ai/ocr/document_intelligence/__init__.py @@ -1,4 +1,5 @@ """Azure Document Intelligence OCR module.""" + from .transformation import AzureDocumentIntelligenceOCRConfig __all__ = ["AzureDocumentIntelligenceOCRConfig"] diff --git a/litellm/llms/azure_ai/ocr/document_intelligence/transformation.py b/litellm/llms/azure_ai/ocr/document_intelligence/transformation.py index 6ef309ca679..81d15bac481 100644 --- a/litellm/llms/azure_ai/ocr/document_intelligence/transformation.py +++ b/litellm/llms/azure_ai/ocr/document_intelligence/transformation.py @@ -7,6 +7,7 @@ This implementation transforms between Mistral OCR format and Azure Document Int Note: Azure Document Intelligence API is async - POST returns 202 Accepted with Operation-Location header. The operation location must be polled until the analysis completes. """ + import asyncio import re import time diff --git a/litellm/llms/azure_ai/ocr/transformation.py b/litellm/llms/azure_ai/ocr/transformation.py index 8f57bb3358b..f661ddb9ebc 100644 --- a/litellm/llms/azure_ai/ocr/transformation.py +++ b/litellm/llms/azure_ai/ocr/transformation.py @@ -1,6 +1,7 @@ """ Azure AI OCR transformation implementation. """ + from typing import Dict, Optional from litellm._logging import verbose_logger diff --git a/litellm/llms/base_llm/ocr/__init__.py b/litellm/llms/base_llm/ocr/__init__.py index 5965af5f2b7..2aea2d67807 100644 --- a/litellm/llms/base_llm/ocr/__init__.py +++ b/litellm/llms/base_llm/ocr/__init__.py @@ -1,4 +1,5 @@ """Base OCR transformation module.""" + from .transformation import ( BaseOCRConfig, DocumentType, diff --git a/litellm/llms/base_llm/ocr/transformation.py b/litellm/llms/base_llm/ocr/transformation.py index 7d16c696dba..b7f4d8e3b2d 100644 --- a/litellm/llms/base_llm/ocr/transformation.py +++ b/litellm/llms/base_llm/ocr/transformation.py @@ -1,6 +1,7 @@ """ Base OCR transformation configuration. """ + from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union import httpx diff --git a/litellm/llms/base_llm/search/__init__.py b/litellm/llms/base_llm/search/__init__.py index f185b4e5955..c423db9ed95 100644 --- a/litellm/llms/base_llm/search/__init__.py +++ b/litellm/llms/base_llm/search/__init__.py @@ -1,6 +1,7 @@ """ Base Search API module. """ + from litellm.llms.base_llm.search.transformation import ( BaseSearchConfig, SearchResponse, diff --git a/litellm/llms/base_llm/search/transformation.py b/litellm/llms/base_llm/search/transformation.py index 1fbc5b670a9..4dfe86685fb 100644 --- a/litellm/llms/base_llm/search/transformation.py +++ b/litellm/llms/base_llm/search/transformation.py @@ -1,6 +1,7 @@ """ Base Search transformation configuration. """ + from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union import httpx diff --git a/litellm/llms/base_llm/vector_store_files/transformation.py b/litellm/llms/base_llm/vector_store_files/transformation.py index f13de563821..02915d013e5 100644 --- a/litellm/llms/base_llm/vector_store_files/transformation.py +++ b/litellm/llms/base_llm/vector_store_files/transformation.py @@ -54,14 +54,12 @@ class BaseVectorStoreFilesConfig(ABC): @abstractmethod def get_auth_credentials( self, litellm_params: Dict[str, Any] - ) -> VectorStoreFileAuthCredentials: - ... + ) -> VectorStoreFileAuthCredentials: ... @abstractmethod def get_vector_store_file_endpoints_by_type( self, - ) -> Dict[str, Tuple[Tuple[str, str], ...]]: - ... + ) -> Dict[str, Tuple[Tuple[str, str], ...]]: ... @abstractmethod def validate_environment( @@ -91,16 +89,14 @@ class BaseVectorStoreFilesConfig(ABC): vector_store_id: str, create_request: VectorStoreFileCreateRequest, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_create_vector_store_file_response( self, *, response: httpx.Response, - ) -> VectorStoreFileObject: - ... + ) -> VectorStoreFileObject: ... @abstractmethod def transform_list_vector_store_files_request( @@ -109,16 +105,14 @@ class BaseVectorStoreFilesConfig(ABC): vector_store_id: str, query_params: VectorStoreFileListQueryParams, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_list_vector_store_files_response( self, *, response: httpx.Response, - ) -> VectorStoreFileListResponse: - ... + ) -> VectorStoreFileListResponse: ... @abstractmethod def transform_retrieve_vector_store_file_request( @@ -127,16 +121,14 @@ class BaseVectorStoreFilesConfig(ABC): vector_store_id: str, file_id: str, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_retrieve_vector_store_file_response( self, *, response: httpx.Response, - ) -> VectorStoreFileObject: - ... + ) -> VectorStoreFileObject: ... @abstractmethod def transform_retrieve_vector_store_file_content_request( @@ -145,16 +137,14 @@ class BaseVectorStoreFilesConfig(ABC): vector_store_id: str, file_id: str, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_retrieve_vector_store_file_content_response( self, *, response: httpx.Response, - ) -> VectorStoreFileContentResponse: - ... + ) -> VectorStoreFileContentResponse: ... @abstractmethod def transform_update_vector_store_file_request( @@ -164,16 +154,14 @@ class BaseVectorStoreFilesConfig(ABC): file_id: str, update_request: VectorStoreFileUpdateRequest, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_update_vector_store_file_response( self, *, response: httpx.Response, - ) -> VectorStoreFileObject: - ... + ) -> VectorStoreFileObject: ... @abstractmethod def transform_delete_vector_store_file_request( @@ -182,16 +170,14 @@ class BaseVectorStoreFilesConfig(ABC): vector_store_id: str, file_id: str, api_base: str, - ) -> Tuple[str, Dict[str, Any]]: - ... + ) -> Tuple[str, Dict[str, Any]]: ... @abstractmethod def transform_delete_vector_store_file_response( self, *, response: httpx.Response, - ) -> VectorStoreFileDeleteResponse: - ... + ) -> VectorStoreFileDeleteResponse: ... def get_error_class( self, diff --git a/litellm/llms/bedrock/batches/handler.py b/litellm/llms/bedrock/batches/handler.py index e0c7c088362..f141bbd9ab4 100644 --- a/litellm/llms/bedrock/batches/handler.py +++ b/litellm/llms/bedrock/batches/handler.py @@ -64,9 +64,11 @@ class BedrockBatchesHandler: created_at=status_response["submitTime"], in_progress_at=status_response["lastModifiedTime"], completed_at=status_response.get("endTime"), - failed_at=status_response.get("endTime") - if status_response["status"] == "failed" - else None, + failed_at=( + status_response.get("endTime") + if status_response["status"] == "failed" + else None + ), request_counts=BatchRequestCounts( total=1, completed=1 if status_response["status"] == "completed" else 0, diff --git a/litellm/llms/bedrock/chat/agentcore/transformation.py b/litellm/llms/bedrock/chat/agentcore/transformation.py index f066322b814..44ba1ce3c86 100644 --- a/litellm/llms/bedrock/chat/agentcore/transformation.py +++ b/litellm/llms/bedrock/chat/agentcore/transformation.py @@ -891,9 +891,9 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM): ) parsed = self._parse_json_response(response_json) - async def _json_as_async_stream() -> AsyncGenerator[ - ModelResponseStream, None - ]: + async def _json_as_async_stream() -> ( + AsyncGenerator[ModelResponseStream, None] + ): # Content chunk content_chunk = ModelResponseStream( id=f"chatcmpl-{uuid.uuid4()}", diff --git a/litellm/llms/bedrock/chat/converse_handler.py b/litellm/llms/bedrock/chat/converse_handler.py index ef46ae5c189..388947a4e9b 100644 --- a/litellm/llms/bedrock/chat/converse_handler.py +++ b/litellm/llms/bedrock/chat/converse_handler.py @@ -332,9 +332,9 @@ class BedrockConverseLLM(BaseAWSLLM): aws_external_id = optional_params.pop("aws_external_id", None) optional_params.pop("aws_region_name", None) - litellm_params[ - "aws_region_name" - ] = aws_region_name # [DO NOT DELETE] important for async calls + litellm_params["aws_region_name"] = ( + aws_region_name # [DO NOT DELETE] important for async calls + ) credentials: Credentials = self.get_credentials( aws_access_key_id=aws_access_key_id, diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index 5cfb00d69b6..e01f2994810 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -1744,9 +1744,7 @@ class AmazonConverseConfig(BaseConfig): return message, returned_finish_reason - def _translate_message_content( - self, content_blocks: List[ContentBlock] - ) -> Tuple[ + def _translate_message_content(self, content_blocks: List[ContentBlock]) -> Tuple[ str, List[ChatCompletionToolCallChunk], Optional[List[BedrockConverseReasoningContentBlock]], @@ -1763,9 +1761,9 @@ class AmazonConverseConfig(BaseConfig): """ content_str = "" tools: List[ChatCompletionToolCallChunk] = [] - reasoningContentBlocks: Optional[ - List[BedrockConverseReasoningContentBlock] - ] = None + reasoningContentBlocks: Optional[List[BedrockConverseReasoningContentBlock]] = ( + None + ) citationsContentBlocks: Optional[List[CitationsContentBlock]] = None for idx, content in enumerate(content_blocks): """ @@ -1976,9 +1974,9 @@ class AmazonConverseConfig(BaseConfig): chat_completion_message: ChatCompletionResponseMessage = {"role": "assistant"} content_str = "" tools: List[ChatCompletionToolCallChunk] = [] - reasoningContentBlocks: Optional[ - List[BedrockConverseReasoningContentBlock] - ] = None + reasoningContentBlocks: Optional[List[BedrockConverseReasoningContentBlock]] = ( + None + ) citationsContentBlocks: Optional[List[CitationsContentBlock]] = None if message is not None: @@ -1997,17 +1995,17 @@ class AmazonConverseConfig(BaseConfig): provider_specific_fields["citationsContent"] = citationsContentBlocks if provider_specific_fields: - chat_completion_message[ - "provider_specific_fields" - ] = provider_specific_fields + chat_completion_message["provider_specific_fields"] = ( + provider_specific_fields + ) if reasoningContentBlocks is not None: - chat_completion_message[ - "reasoning_content" - ] = self._transform_reasoning_content(reasoningContentBlocks) - chat_completion_message[ - "thinking_blocks" - ] = self._transform_thinking_blocks(reasoningContentBlocks) + chat_completion_message["reasoning_content"] = ( + self._transform_reasoning_content(reasoningContentBlocks) + ) + chat_completion_message["thinking_blocks"] = ( + self._transform_thinking_blocks(reasoningContentBlocks) + ) chat_completion_message["content"] = content_str filtered_tools = self._filter_json_mode_tools( json_mode=json_mode, diff --git a/litellm/llms/bedrock/chat/invoke_handler.py b/litellm/llms/bedrock/chat/invoke_handler.py index 67bba28e4c5..9dfada7c418 100644 --- a/litellm/llms/bedrock/chat/invoke_handler.py +++ b/litellm/llms/bedrock/chat/invoke_handler.py @@ -199,11 +199,13 @@ async def make_call( if client is None: client = get_async_httpx_client( llm_provider=litellm.LlmProviders.BEDROCK, - params={"ssl_verify": logging_obj.litellm_params.get("ssl_verify")} - if logging_obj - and logging_obj.litellm_params - and logging_obj.litellm_params.get("ssl_verify") - else None, + params=( + {"ssl_verify": logging_obj.litellm_params.get("ssl_verify")} + if logging_obj + and logging_obj.litellm_params + and logging_obj.litellm_params.get("ssl_verify") + else None + ), ) # Create a new client if none provided response = await client.post( @@ -293,11 +295,13 @@ def make_sync_call( try: if client is None: client = _get_httpx_client( - params={"ssl_verify": logging_obj.litellm_params.get("ssl_verify")} - if logging_obj - and logging_obj.litellm_params - and logging_obj.litellm_params.get("ssl_verify") - else None + params=( + {"ssl_verify": logging_obj.litellm_params.get("ssl_verify")} + if logging_obj + and logging_obj.litellm_params + and logging_obj.litellm_params.get("ssl_verify") + else None + ) ) response = client.post( @@ -547,9 +551,9 @@ class BedrockLLM(BaseAWSLLM): content=None, ) model_response.choices[0].message = _message # type: ignore - model_response._hidden_params[ - "original_response" - ] = outputText # allow user to access raw anthropic tool calling response + model_response._hidden_params["original_response"] = ( + outputText # allow user to access raw anthropic tool calling response + ) if ( _is_function_call is True and stream is not None @@ -855,8 +859,10 @@ class BedrockLLM(BaseAWSLLM): endpoint_url = f"{endpoint_url}/model/{modelId}/invoke" proxy_endpoint_url = f"{proxy_endpoint_url}/model/{modelId}/invoke" - if acompletion and provider == "anthropic" and self.is_claude_messages_api_model( - model + if ( + acompletion + and provider == "anthropic" + and self.is_claude_messages_api_model(model) ): if isinstance(client, HTTPHandler): client = None @@ -908,9 +914,9 @@ class BedrockLLM(BaseAWSLLM): ): # completion(top_k=3) > anthropic_config(top_k=3) <- allows for dynamic variables to be passed in inference_params[k] = v if stream is True: - inference_params[ - "stream" - ] = True # cohere requires stream = True in inference params + inference_params["stream"] = ( + True # cohere requires stream = True in inference params + ) data = json.dumps({"prompt": prompt, **inference_params}) elif provider == "anthropic": if self.is_claude_messages_api_model(model): @@ -1195,12 +1201,14 @@ class BedrockLLM(BaseAWSLLM): client: Optional[AsyncHTTPHandler] = None, stream_chunk_size: int = 1024, ) -> Union[ModelResponse, CustomStreamWrapper]: - transformed_request = await litellm.AmazonAnthropicClaudeConfig().async_transform_request( - model=model, - messages=messages, - optional_params=optional_params, - litellm_params=litellm_params or {}, - headers=extra_headers or {}, + transformed_request = ( + await litellm.AmazonAnthropicClaudeConfig().async_transform_request( + model=model, + messages=messages, + optional_params=optional_params, + litellm_params=litellm_params or {}, + headers=extra_headers or {}, + ) ) data = json.dumps(transformed_request) diff --git a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py index cf8aee6954b..43850440072 100644 --- a/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py +++ b/litellm/llms/bedrock/chat/invoke_transformations/base_invoke_transformation.py @@ -182,9 +182,9 @@ class AmazonInvokeConfig(BaseConfig, BaseAWSLLM): config = litellm.AmazonCohereConfig.get_config() self._apply_config_to_params(config, inference_params) if stream is True: - inference_params[ - "stream" - ] = True # cohere requires stream = True in inference params + inference_params["stream"] = ( + True # cohere requires stream = True in inference params + ) request_data = {"prompt": prompt, **inference_params} elif provider == "anthropic": transformed_request = ( diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.py index 9666aa68c99..81ea94e07c7 100644 --- a/litellm/llms/bedrock/common_utils.py +++ b/litellm/llms/bedrock/common_utils.py @@ -1062,9 +1062,11 @@ class CommonBatchFilesUtils: return ( dict(prepped.headers), - request_data.encode("utf-8") - if isinstance(request_data, str) - else request_data, + ( + request_data.encode("utf-8") + if isinstance(request_data, str) + else request_data + ), ) def generate_unique_job_name(self, model: str, prefix: str = "litellm") -> str: diff --git a/litellm/llms/bedrock/embed/amazon_titan_multimodal_transformation.py b/litellm/llms/bedrock/embed/amazon_titan_multimodal_transformation.py index 07b04734c30..2713f54e623 100644 --- a/litellm/llms/bedrock/embed/amazon_titan_multimodal_transformation.py +++ b/litellm/llms/bedrock/embed/amazon_titan_multimodal_transformation.py @@ -38,9 +38,9 @@ class AmazonTitanMultimodalEmbeddingG1Config: ) -> dict: for k, v in non_default_params.items(): if k == "dimensions": - optional_params[ - "embeddingConfig" - ] = AmazonTitanMultimodalEmbeddingConfig(outputEmbeddingLength=v) + optional_params["embeddingConfig"] = ( + AmazonTitanMultimodalEmbeddingConfig(outputEmbeddingLength=v) + ) return optional_params def _transform_request( diff --git a/litellm/llms/bedrock/image_generation/amazon_nova_canvas_transformation.py b/litellm/llms/bedrock/image_generation/amazon_nova_canvas_transformation.py index 86c005bbfad..87ef469beb5 100644 --- a/litellm/llms/bedrock/image_generation/amazon_nova_canvas_transformation.py +++ b/litellm/llms/bedrock/image_generation/amazon_nova_canvas_transformation.py @@ -103,9 +103,9 @@ class AmazonNovaCanvasConfig: imageGenerationConfig=image_generation_config_typed, ) if task_type == "COLOR_GUIDED_GENERATION": - color_guided_generation_params: Dict[ - str, Any - ] = image_generation_config.pop("colorGuidedGenerationParams", {}) + color_guided_generation_params: Dict[str, Any] = ( + image_generation_config.pop("colorGuidedGenerationParams", {}) + ) color_guided_generation_params = { "text": text, **color_guided_generation_params, diff --git a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py index c1eccaebd04..c6c2af8e8f8 100644 --- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py +++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py @@ -392,9 +392,9 @@ class AmazonAnthropicClaudeMessagesConfig( # 1. anthropic_version is required for all claude models if "anthropic_version" not in anthropic_messages_request: - anthropic_messages_request[ - "anthropic_version" - ] = self.DEFAULT_BEDROCK_ANTHROPIC_API_VERSION + anthropic_messages_request["anthropic_version"] = ( + self.DEFAULT_BEDROCK_ANTHROPIC_API_VERSION + ) # 2. `stream` is not allowed in request body for bedrock invoke if "stream" in anthropic_messages_request: diff --git a/litellm/llms/bedrock/realtime/handler.py b/litellm/llms/bedrock/realtime/handler.py index cde9f3e6fce..705ef62389c 100644 --- a/litellm/llms/bedrock/realtime/handler.py +++ b/litellm/llms/bedrock/realtime/handler.py @@ -235,7 +235,9 @@ class BedrockRealtime(BaseAWSLLM): # Transform Bedrock format to OpenAI format from litellm.types.realtime import RealtimeResponseTransformInput - realtime_response_transform_input: RealtimeResponseTransformInput = { + realtime_response_transform_input: ( + RealtimeResponseTransformInput + ) = { "current_output_item_id": session_state.get( "current_output_item_id" ), diff --git a/litellm/llms/bedrock/realtime/transformation.py b/litellm/llms/bedrock/realtime/transformation.py index 13d5bf35466..9124a8c21b4 100644 --- a/litellm/llms/bedrock/realtime/transformation.py +++ b/litellm/llms/bedrock/realtime/transformation.py @@ -1016,9 +1016,9 @@ class BedrockRealtimeConfig(BaseRealtimeConfig): "toolResult": { "promptName": self.prompt_name, "contentName": tool_content_name, - "content": output - if isinstance(output, str) - else json.dumps(output), + "content": ( + output if isinstance(output, str) else json.dumps(output) + ), } } } diff --git a/litellm/llms/chatgpt/chat/streaming_utils.py b/litellm/llms/chatgpt/chat/streaming_utils.py index e9cf2d15c20..a08fecd9625 100644 --- a/litellm/llms/chatgpt/chat/streaming_utils.py +++ b/litellm/llms/chatgpt/chat/streaming_utils.py @@ -24,9 +24,9 @@ class ChatGPTToolCallNormalizer: self._stream = stream self._seen_ids: Dict[str, int] = {} # tool_call_id -> assigned_index self._next_index: int = 0 - self._last_id: Optional[ - str - ] = None # tracks which tool call the next delta belongs to + self._last_id: Optional[str] = ( + None # tracks which tool call the next delta belongs to + ) def __getattr__(self, name: str) -> Any: return getattr(self._stream, name) diff --git a/litellm/llms/chatgpt/common_utils.py b/litellm/llms/chatgpt/common_utils.py index 9cbcd6a4f46..830414d9cad 100644 --- a/litellm/llms/chatgpt/common_utils.py +++ b/litellm/llms/chatgpt/common_utils.py @@ -1,6 +1,7 @@ """ Constants and helpers for ChatGPT subscription OAuth. """ + import os import platform from typing import Any, Optional, Union diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.py index 3c59ca16581..66acd933416 100644 --- a/litellm/llms/chatgpt/responses/transformation.py +++ b/litellm/llms/chatgpt/responses/transformation.py @@ -77,9 +77,9 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig): existing_instructions = request.get("instructions") if existing_instructions: if base_instructions not in existing_instructions: - request[ - "instructions" - ] = f"{base_instructions}\n\n{existing_instructions}" + request["instructions"] = ( + f"{base_instructions}\n\n{existing_instructions}" + ) else: request["instructions"] = base_instructions request["store"] = False diff --git a/litellm/llms/custom_httpx/async_client_cleanup.py b/litellm/llms/custom_httpx/async_client_cleanup.py index 22629383ac2..9c1f6af7e9c 100644 --- a/litellm/llms/custom_httpx/async_client_cleanup.py +++ b/litellm/llms/custom_httpx/async_client_cleanup.py @@ -1,6 +1,7 @@ """ Utility functions for cleaning up async HTTP clients to prevent resource leaks. """ + import asyncio diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index 001547557d4..a14eeb1f899 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -886,9 +886,9 @@ class AsyncHTTPHandler: if AIOHTTP_CONNECTOR_LIMIT > 0: transport_connector_kwargs["limit"] = AIOHTTP_CONNECTOR_LIMIT if AIOHTTP_CONNECTOR_LIMIT_PER_HOST > 0: - transport_connector_kwargs[ - "limit_per_host" - ] = AIOHTTP_CONNECTOR_LIMIT_PER_HOST + transport_connector_kwargs["limit_per_host"] = ( + AIOHTTP_CONNECTOR_LIMIT_PER_HOST + ) return LiteLLMAiohttpTransport( client=lambda: ClientSession( diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 4c9abaad908..30320d9adab 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -4495,9 +4495,9 @@ class BaseLLMHTTPHandler: # Second: Execute agentic loop # Add custom_llm_provider to kwargs so the agentic loop can reconstruct the full model name kwargs_with_provider = kwargs.copy() if kwargs else {} - kwargs_with_provider[ - "custom_llm_provider" - ] = custom_llm_provider + kwargs_with_provider["custom_llm_provider"] = ( + custom_llm_provider + ) agentic_response = await callback.async_run_agentic_loop( tools=tool_calls, model=model, @@ -4613,9 +4613,9 @@ class BaseLLMHTTPHandler: # Second: Execute agentic loop # Add custom_llm_provider to kwargs so the agentic loop can reconstruct the full model name kwargs_with_provider = kwargs.copy() if kwargs else {} - kwargs_with_provider[ - "custom_llm_provider" - ] = custom_llm_provider + kwargs_with_provider["custom_llm_provider"] = ( + custom_llm_provider + ) agentic_response = ( await callback.async_run_chat_completion_agentic_loop( tools=tool_calls, @@ -5099,7 +5099,10 @@ class BaseLLMHTTPHandler: _is_async: bool = False, fake_stream: bool = False, litellm_metadata: Optional[Dict[str, Any]] = None, - ) -> Union[ImageResponse, Coroutine[Any, Any, ImageResponse],]: + ) -> Union[ + ImageResponse, + Coroutine[Any, Any, ImageResponse], + ]: """ Handles image edit requests. @@ -5311,7 +5314,10 @@ class BaseLLMHTTPHandler: fake_stream: bool = False, litellm_metadata: Optional[Dict[str, Any]] = None, api_key: Optional[str] = None, - ) -> Union[ImageResponse, Coroutine[Any, Any, ImageResponse],]: + ) -> Union[ + ImageResponse, + Coroutine[Any, Any, ImageResponse], + ]: """ Handles image generation requests. When _is_async=True, returns a coroutine instead of making the call directly. @@ -5551,7 +5557,10 @@ class BaseLLMHTTPHandler: fake_stream: bool = False, litellm_metadata: Optional[Dict[str, Any]] = None, api_key: Optional[str] = None, - ) -> Union[VideoObject, Coroutine[Any, Any, VideoObject],]: + ) -> Union[ + VideoObject, + Coroutine[Any, Any, VideoObject], + ]: """ Handles video generation requests. When _is_async=True, returns a coroutine instead of making the call directly. diff --git a/litellm/llms/dashscope/chat/transformation.py b/litellm/llms/dashscope/chat/transformation.py index cc5cf991826..4d90b2a1f9f 100644 --- a/litellm/llms/dashscope/chat/transformation.py +++ b/litellm/llms/dashscope/chat/transformation.py @@ -14,8 +14,7 @@ class DashScopeChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -23,8 +22,7 @@ class DashScopeChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/databricks/chat/transformation.py b/litellm/llms/databricks/chat/transformation.py index 8ae02bd65ed..638d2d2d9e2 100644 --- a/litellm/llms/databricks/chat/transformation.py +++ b/litellm/llms/databricks/chat/transformation.py @@ -353,8 +353,7 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -362,8 +361,7 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/databricks/common_utils.py b/litellm/llms/databricks/common_utils.py index 608f29a03a7..d39d52d2d59 100644 --- a/litellm/llms/databricks/common_utils.py +++ b/litellm/llms/databricks/common_utils.py @@ -289,9 +289,9 @@ class DatabricksBase: api_base = api_base or f"{databricks_client.config.host}/serving-endpoints" if api_key is None: - databricks_auth_headers: dict[ - str, str - ] = databricks_client.config.authenticate() + databricks_auth_headers: dict[str, str] = ( + databricks_client.config.authenticate() + ) headers = {**databricks_auth_headers, **headers} return api_base, headers diff --git a/litellm/llms/databricks/embed/transformation.py b/litellm/llms/databricks/embed/transformation.py index a113a349cc6..53e3b30dd21 100644 --- a/litellm/llms/databricks/embed/transformation.py +++ b/litellm/llms/databricks/embed/transformation.py @@ -11,9 +11,9 @@ class DatabricksEmbeddingConfig: Reference: https://learn.microsoft.com/en-us/azure/databricks/machine-learning/foundation-models/api-reference#--embedding-task """ - instruction: Optional[ - str - ] = None # An optional instruction to pass to the embedding model. BGE Authors recommend 'Represent this sentence for searching relevant passages:' for retrieval queries + instruction: Optional[str] = ( + None # An optional instruction to pass to the embedding model. BGE Authors recommend 'Represent this sentence for searching relevant passages:' for retrieval queries + ) def __init__(self, instruction: Optional[str] = None) -> None: locals_ = locals().copy() diff --git a/litellm/llms/dataforseo/search/transformation.py b/litellm/llms/dataforseo/search/transformation.py index 940f1ca6007..27c10d740b5 100644 --- a/litellm/llms/dataforseo/search/transformation.py +++ b/litellm/llms/dataforseo/search/transformation.py @@ -3,6 +3,7 @@ Calls DataForSEO SERP API to search the web. DataForSEO API Reference: https://docs.dataforseo.com/v3/serp/google/organic/live/advanced/?bash """ + from typing import Any, Dict, List, Literal, Optional, Union import httpx diff --git a/litellm/llms/deepinfra/chat/transformation.py b/litellm/llms/deepinfra/chat/transformation.py index c36b490abca..a6bd8b4934f 100644 --- a/litellm/llms/deepinfra/chat/transformation.py +++ b/litellm/llms/deepinfra/chat/transformation.py @@ -161,8 +161,7 @@ class DeepInfraConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -170,8 +169,7 @@ class DeepInfraConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/deepseek/chat/transformation.py b/litellm/llms/deepseek/chat/transformation.py index d38ec4d67dd..5cd8d119542 100644 --- a/litellm/llms/deepseek/chat/transformation.py +++ b/litellm/llms/deepseek/chat/transformation.py @@ -65,8 +65,7 @@ class DeepSeekChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -74,8 +73,7 @@ class DeepSeekChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/deprecated_providers/aleph_alpha.py b/litellm/llms/deprecated_providers/aleph_alpha.py index 4cfede2a1b9..81ad1346414 100644 --- a/litellm/llms/deprecated_providers/aleph_alpha.py +++ b/litellm/llms/deprecated_providers/aleph_alpha.py @@ -77,9 +77,9 @@ class AlephAlphaConfig: - `control_log_additive` (boolean; default value: true): Method of applying control to attention scores. """ - maximum_tokens: Optional[ - int - ] = litellm.max_tokens # aleph alpha requires max tokens + maximum_tokens: Optional[int] = ( + litellm.max_tokens + ) # aleph alpha requires max tokens minimum_tokens: Optional[int] = None echo: Optional[bool] = None temperature: Optional[int] = None diff --git a/litellm/llms/docker_model_runner/chat/transformation.py b/litellm/llms/docker_model_runner/chat/transformation.py index 4b81502bf81..dc03c80f154 100644 --- a/litellm/llms/docker_model_runner/chat/transformation.py +++ b/litellm/llms/docker_model_runner/chat/transformation.py @@ -26,8 +26,7 @@ class DockerModelRunnerChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -35,8 +34,7 @@ class DockerModelRunnerChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/duckduckgo/search/__init__.py b/litellm/llms/duckduckgo/search/__init__.py index c0019637838..7ae8f7b397a 100644 --- a/litellm/llms/duckduckgo/search/__init__.py +++ b/litellm/llms/duckduckgo/search/__init__.py @@ -1,6 +1,7 @@ """ DuckDuckGo Search API module. """ + from litellm.llms.duckduckgo.search.transformation import DuckDuckGoSearchConfig __all__ = ["DuckDuckGoSearchConfig"] diff --git a/litellm/llms/duckduckgo/search/transformation.py b/litellm/llms/duckduckgo/search/transformation.py index c754338153a..e8eda3a37ab 100644 --- a/litellm/llms/duckduckgo/search/transformation.py +++ b/litellm/llms/duckduckgo/search/transformation.py @@ -3,6 +3,7 @@ Calls DuckDuckGo's Instant Answer API to search the web. DuckDuckGo API Reference: https://duckduckgo.com/api """ + from typing import Dict, List, Literal, Optional, TypedDict, Union from urllib.parse import urlencode diff --git a/litellm/llms/exa_ai/search/__init__.py b/litellm/llms/exa_ai/search/__init__.py index db1f0804646..80bc10043bb 100644 --- a/litellm/llms/exa_ai/search/__init__.py +++ b/litellm/llms/exa_ai/search/__init__.py @@ -1,6 +1,7 @@ """ Exa AI Search API module. """ + from litellm.llms.exa_ai.search.transformation import ExaAISearchConfig __all__ = ["ExaAISearchConfig"] diff --git a/litellm/llms/exa_ai/search/transformation.py b/litellm/llms/exa_ai/search/transformation.py index fb352f3f93e..7a34ededa6b 100644 --- a/litellm/llms/exa_ai/search/transformation.py +++ b/litellm/llms/exa_ai/search/transformation.py @@ -3,6 +3,7 @@ Calls Exa AI's /search endpoint to search the web. Exa AI API Reference: https://docs.exa.ai/reference/search """ + from typing import Dict, List, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/firecrawl/__init__.py b/litellm/llms/firecrawl/__init__.py index b43d2da3214..ef8414689a4 100644 --- a/litellm/llms/firecrawl/__init__.py +++ b/litellm/llms/firecrawl/__init__.py @@ -1,6 +1,7 @@ """ Firecrawl API integration module. """ + from litellm.llms.firecrawl.search.transformation import FirecrawlSearchConfig __all__ = ["FirecrawlSearchConfig"] diff --git a/litellm/llms/firecrawl/search/__init__.py b/litellm/llms/firecrawl/search/__init__.py index 46619d05b63..5b28e6a5068 100644 --- a/litellm/llms/firecrawl/search/__init__.py +++ b/litellm/llms/firecrawl/search/__init__.py @@ -1,6 +1,7 @@ """ Firecrawl Search API module. """ + from litellm.llms.firecrawl.search.transformation import FirecrawlSearchConfig __all__ = ["FirecrawlSearchConfig"] diff --git a/litellm/llms/firecrawl/search/transformation.py b/litellm/llms/firecrawl/search/transformation.py index 71136e1d3b3..18cf1d28c4d 100644 --- a/litellm/llms/firecrawl/search/transformation.py +++ b/litellm/llms/firecrawl/search/transformation.py @@ -3,6 +3,7 @@ Calls Firecrawl's /search endpoint to search the web. Firecrawl API Reference: https://docs.firecrawl.dev/api-reference/endpoint/search """ + from typing import Dict, List, Optional, TypedDict, Union import httpx @@ -184,9 +185,7 @@ class FirecrawlSearchConfig(BaseSearchConfig): if isinstance(data, list): # Self-hosted Firecrawl (v1) format: data is a flat list of results for result in data: - snippet = ( - result.get("markdown") or result.get("description", "") - ) + snippet = result.get("markdown") or result.get("description", "") search_result = SearchResult( title=result.get("title", ""), url=result.get("url", ""), diff --git a/litellm/llms/fireworks_ai/chat/transformation.py b/litellm/llms/fireworks_ai/chat/transformation.py index 6b654ebdfd3..ed6d167a118 100644 --- a/litellm/llms/fireworks_ai/chat/transformation.py +++ b/litellm/llms/fireworks_ai/chat/transformation.py @@ -392,11 +392,11 @@ class FireworksAIConfig(OpenAIGPTConfig): ## FIREWORKS AI sends tool calls in the content field instead of tool_calls for choice in response.choices: - cast( - Choices, choice - ).message = self._handle_message_content_with_tool_calls( - message=cast(Choices, choice).message, - tool_calls=optional_params.get("tools", None), + cast(Choices, choice).message = ( + self._handle_message_content_with_tool_calls( + message=cast(Choices, choice).message, + tool_calls=optional_params.get("tools", None), + ) ) response._hidden_params = {"additional_headers": additional_headers} diff --git a/litellm/llms/gemini/files/transformation.py b/litellm/llms/gemini/files/transformation.py index a29ed66e63d..171d1f020a2 100644 --- a/litellm/llms/gemini/files/transformation.py +++ b/litellm/llms/gemini/files/transformation.py @@ -3,6 +3,7 @@ Supports writing files to Google AI Studio Files API. For vertex ai, check out the vertex_ai/files/handler.py file. """ + import time from typing import Any, List, Literal, Optional from urllib.parse import urlparse @@ -300,9 +301,11 @@ class GoogleAIStudioFilesHandler(GeminiModelInfo, BaseFilesConfig): object="file", purpose="user_data", status=status, - status_details=str(response_json.get("error", "")) - if gemini_state == "FAILED" - else None, + status_details=( + str(response_json.get("error", "")) + if gemini_state == "FAILED" + else None + ), ) except Exception as e: verbose_logger.exception(f"Error parsing file retrieve response: {str(e)}") diff --git a/litellm/llms/gemini/image_edit/transformation.py b/litellm/llms/gemini/image_edit/transformation.py index 5d9b1255d09..d46733e04b2 100644 --- a/litellm/llms/gemini/image_edit/transformation.py +++ b/litellm/llms/gemini/image_edit/transformation.py @@ -111,9 +111,9 @@ class GeminiImageEditConfig(BaseImageEditConfig): # Move aspectRatio into imageConfig inside generationConfig if "imageConfig" not in generation_config: generation_config["imageConfig"] = {} - generation_config["imageConfig"][ - "aspectRatio" - ] = image_edit_optional_request_params["aspectRatio"] + generation_config["imageConfig"]["aspectRatio"] = ( + image_edit_optional_request_params["aspectRatio"] + ) if generation_config: request_body["generationConfig"] = generation_config diff --git a/litellm/llms/gemini/image_generation/transformation.py b/litellm/llms/gemini/image_generation/transformation.py index b094fc133d7..9c4cd008b8c 100644 --- a/litellm/llms/gemini/image_generation/transformation.py +++ b/litellm/llms/gemini/image_generation/transformation.py @@ -245,11 +245,11 @@ class GoogleImageGenConfig(BaseImageGenerationConfig): ImageObject( b64_json=inline_data["data"], url=None, - provider_specific_fields={ - "thought_signature": thought_sig - } - if thought_sig - else None, + provider_specific_fields=( + {"thought_signature": thought_sig} + if thought_sig + else None + ), ) ) diff --git a/litellm/llms/gemini/realtime/transformation.py b/litellm/llms/gemini/realtime/transformation.py index 2bb7bcd8b4f..c04f5725cf9 100644 --- a/litellm/llms/gemini/realtime/transformation.py +++ b/litellm/llms/gemini/realtime/transformation.py @@ -186,10 +186,10 @@ class GeminiRealtimeConfig(BaseRealtimeConfig): ) vertex_gemini_config = VertexGeminiConfig() - optional_params["generationConfig"][ - "tools" - ] = vertex_gemini_config._map_function( - value=value, optional_params=optional_params + optional_params["generationConfig"]["tools"] = ( + vertex_gemini_config._map_function( + value=value, optional_params=optional_params + ) ) elif key == "input_audio_transcription" and value is not None: optional_params["inputAudioTranscription"] = {} @@ -201,10 +201,10 @@ class GeminiRealtimeConfig(BaseRealtimeConfig): if ( len(transformed_audio_activity_config) > 0 ): # if the config is not empty, add it to the optional params - optional_params[ - "realtimeInputConfig" - ] = BidiGenerateContentRealtimeInputConfig( - automaticActivityDetection=transformed_audio_activity_config + optional_params["realtimeInputConfig"] = ( + BidiGenerateContentRealtimeInputConfig( + automaticActivityDetection=transformed_audio_activity_config + ) ) if len(optional_params["generationConfig"]) == 0: optional_params.pop("generationConfig") @@ -864,9 +864,9 @@ class GeminiRealtimeConfig(BaseRealtimeConfig): "session_configuration_request" ] current_item_chunks = realtime_response_transform_input["current_item_chunks"] - current_delta_type: Optional[ - ALL_DELTA_TYPES - ] = realtime_response_transform_input["current_delta_type"] + current_delta_type: Optional[ALL_DELTA_TYPES] = ( + realtime_response_transform_input["current_delta_type"] + ) returned_message: List[OpenAIRealtimeEvents] = [] # Handle transcription events that arrive independently from model diff --git a/litellm/llms/github_copilot/common_utils.py b/litellm/llms/github_copilot/common_utils.py index d3169e3ca94..a9944df51a4 100644 --- a/litellm/llms/github_copilot/common_utils.py +++ b/litellm/llms/github_copilot/common_utils.py @@ -1,6 +1,7 @@ """ Constants for Copilot integration """ + from typing import Optional, Union from uuid import uuid4 diff --git a/litellm/llms/github_copilot/embedding/transformation.py b/litellm/llms/github_copilot/embedding/transformation.py index fa7bd4e3223..5fc6970342a 100644 --- a/litellm/llms/github_copilot/embedding/transformation.py +++ b/litellm/llms/github_copilot/embedding/transformation.py @@ -6,6 +6,7 @@ This module provides the configuration for GitHub Copilot's Embedding API. Implementation based on analysis of the copilot-api project by caozhiyuan: https://github.com/caozhiyuan/copilot-api """ + from typing import TYPE_CHECKING, Any, Optional import httpx diff --git a/litellm/llms/github_copilot/responses/transformation.py b/litellm/llms/github_copilot/responses/transformation.py index 46efc124b1d..d97b56db395 100644 --- a/litellm/llms/github_copilot/responses/transformation.py +++ b/litellm/llms/github_copilot/responses/transformation.py @@ -7,6 +7,7 @@ which is required for models like gpt-5.1-codex that only support the /responses Implementation based on analysis of the copilot-api project by caozhiyuan: https://github.com/caozhiyuan/copilot-api """ + from typing import TYPE_CHECKING, Any, Dict, Optional, Union from litellm._logging import verbose_logger diff --git a/litellm/llms/google_pse/search/__init__.py b/litellm/llms/google_pse/search/__init__.py index 0fcfff82c38..0d2acafb798 100644 --- a/litellm/llms/google_pse/search/__init__.py +++ b/litellm/llms/google_pse/search/__init__.py @@ -1,6 +1,7 @@ """ Google Programmable Search Engine (PSE) API module. """ + from litellm.llms.google_pse.search.transformation import GooglePSESearchConfig __all__ = ["GooglePSESearchConfig"] diff --git a/litellm/llms/google_pse/search/transformation.py b/litellm/llms/google_pse/search/transformation.py index 2fabbc5d16e..a8aa109cbf0 100644 --- a/litellm/llms/google_pse/search/transformation.py +++ b/litellm/llms/google_pse/search/transformation.py @@ -3,6 +3,7 @@ Calls Google Programmable Search Engine (PSE) API to search the web. Google PSE API Reference: https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list """ + from typing import Dict, List, Literal, Optional, TypedDict, Union import httpx @@ -42,10 +43,14 @@ class GooglePSESearchRequest(_GooglePSESearchRequestRequired, total=False): hq: str # Optional - append query terms to query imgSize: str # Optional - returns images of specified size imgType: str # Optional - returns images of specified type - linkSite: str # Optional - specifies all search results should contain a link to a URL + linkSite: ( + str # Optional - specifies all search results should contain a link to a URL + ) lr: str # Optional - language restrict (e.g., 'lang_en', 'lang_es') orTerms: str # Optional - provides additional search terms - relatedSite: str # Optional - specifies all search results should be pages related to URL + relatedSite: ( + str # Optional - specifies all search results should be pages related to URL + ) rights: str # Optional - filters based on licensing safe: str # Optional - search safety level ('active', 'off') searchType: str # Optional - specifies search type ('image') diff --git a/litellm/llms/groq/chat/transformation.py b/litellm/llms/groq/chat/transformation.py index 34ea7b03dd9..d07da006f2d 100644 --- a/litellm/llms/groq/chat/transformation.py +++ b/litellm/llms/groq/chat/transformation.py @@ -1,6 +1,7 @@ """ Translate from OpenAI's `/v1/chat/completions` to Groq's `/v1/chat/completions` """ + from typing import ( Any, Coroutine, @@ -115,8 +116,7 @@ class GroqChatConfig(OpenAILikeChatConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -124,8 +124,7 @@ class GroqChatConfig(OpenAILikeChatConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False @@ -293,10 +292,10 @@ class GroqChatConfig(OpenAILikeChatConfig): json_mode=json_mode, ) - mapped_service_tier: Literal[ - "auto", "default", "flex" - ] = self._map_groq_service_tier( - original_service_tier=getattr(model_response, "service_tier") + mapped_service_tier: Literal["auto", "default", "flex"] = ( + self._map_groq_service_tier( + original_service_tier=getattr(model_response, "service_tier") + ) ) setattr(model_response, "service_tier", mapped_service_tier) return model_response diff --git a/litellm/llms/heroku/chat/transformation.py b/litellm/llms/heroku/chat/transformation.py index d95e953636f..fb4cc361189 100644 --- a/litellm/llms/heroku/chat/transformation.py +++ b/litellm/llms/heroku/chat/transformation.py @@ -3,6 +3,7 @@ Heroku Chat Completions API this is OpenAI compatible - no translation needed / occurs """ + import os from typing import Optional, List, Tuple, Union, Coroutine, Any, Literal, overload @@ -22,8 +23,7 @@ class HerokuChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -31,8 +31,7 @@ class HerokuChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/hosted_vllm/chat/transformation.py b/litellm/llms/hosted_vllm/chat/transformation.py index 05db1544a2b..b5a8b25beba 100644 --- a/litellm/llms/hosted_vllm/chat/transformation.py +++ b/litellm/llms/hosted_vllm/chat/transformation.py @@ -121,8 +121,7 @@ class HostedVLLMChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -130,8 +129,7 @@ class HostedVLLMChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False @@ -146,9 +144,14 @@ class HostedVLLMChatConfig(OpenAIGPTConfig): thinking_blocks = message.pop("thinking_blocks", None) # type: ignore if thinking_blocks: new_content: list = [ - {"type": block["type"], "thinking": block.get("thinking", "")} - if block.get("type") == "thinking" - else {"type": block["type"], "data": block.get("data", "")} + ( + { + "type": block["type"], + "thinking": block.get("thinking", ""), + } + if block.get("type") == "thinking" + else {"type": block["type"], "data": block.get("data", "")} + ) for block in thinking_blocks ] existing_content = message.get("content") diff --git a/litellm/llms/huggingface/embedding/transformation.py b/litellm/llms/huggingface/embedding/transformation.py index 03088d6e151..88d42cfcdcc 100644 --- a/litellm/llms/huggingface/embedding/transformation.py +++ b/litellm/llms/huggingface/embedding/transformation.py @@ -40,17 +40,17 @@ class HuggingFaceEmbeddingConfig(BaseConfig): Reference: https://huggingface.github.io/text-generation-inference/#/Text%20Generation%20Inference/compat_generate """ - hf_task: Optional[ - hf_tasks - ] = None # litellm-specific param, used to know the api spec to use when calling huggingface api + hf_task: Optional[hf_tasks] = ( + None # litellm-specific param, used to know the api spec to use when calling huggingface api + ) best_of: Optional[int] = None decoder_input_details: Optional[bool] = None details: Optional[bool] = True # enables returning logprobs + best of max_new_tokens: Optional[int] = None repetition_penalty: Optional[float] = None - return_full_text: Optional[ - bool - ] = False # by default don't return the input as part of the output + return_full_text: Optional[bool] = ( + False # by default don't return the input as part of the output + ) seed: Optional[int] = None temperature: Optional[float] = None top_k: Optional[int] = None @@ -120,9 +120,9 @@ class HuggingFaceEmbeddingConfig(BaseConfig): optional_params["top_p"] = value if param == "n": optional_params["best_of"] = value - optional_params[ - "do_sample" - ] = True # Need to sample if you want best of for hf inference endpoints + optional_params["do_sample"] = ( + True # Need to sample if you want best of for hf inference endpoints + ) if param == "stream": optional_params["stream"] = value if param == "stop": @@ -363,9 +363,9 @@ class HuggingFaceEmbeddingConfig(BaseConfig): "content-type": "application/json", } if api_key is not None: - default_headers[ - "Authorization" - ] = f"Bearer {api_key}" # Huggingface Inference Endpoint default is to accept bearer tokens + default_headers["Authorization"] = ( + f"Bearer {api_key}" # Huggingface Inference Endpoint default is to accept bearer tokens + ) headers = {**headers, **default_headers} return headers diff --git a/litellm/llms/lemonade/chat/transformation.py b/litellm/llms/lemonade/chat/transformation.py index a9039388a49..168d51a16d8 100644 --- a/litellm/llms/lemonade/chat/transformation.py +++ b/litellm/llms/lemonade/chat/transformation.py @@ -1,6 +1,7 @@ """ Translate from OpenAI's `/v1/chat/completions` to Lemonade's `/v1/chat/completions` """ + from typing import Any, List, Optional, Tuple, Union import httpx diff --git a/litellm/llms/lemonade/cost_calculator.py b/litellm/llms/lemonade/cost_calculator.py index 2042f6d0d4d..74d62da8759 100644 --- a/litellm/llms/lemonade/cost_calculator.py +++ b/litellm/llms/lemonade/cost_calculator.py @@ -4,6 +4,7 @@ Cost calculation for Lemonade LLM provider. Since Lemonade is a local/self-hosted service, all costs default to 0. This prevents cost calculation errors when using models not in model_prices_and_context_window.json """ + from typing import Tuple from litellm.types.utils import Usage diff --git a/litellm/llms/linkup/__init__.py b/litellm/llms/linkup/__init__.py index c761584b07c..dd242fc5660 100644 --- a/litellm/llms/linkup/__init__.py +++ b/litellm/llms/linkup/__init__.py @@ -1,6 +1,7 @@ """ Linkup API integration module. """ + from litellm.llms.linkup.search.transformation import LinkupSearchConfig __all__ = ["LinkupSearchConfig"] diff --git a/litellm/llms/linkup/search/__init__.py b/litellm/llms/linkup/search/__init__.py index 667c4630238..a8f2c04350c 100644 --- a/litellm/llms/linkup/search/__init__.py +++ b/litellm/llms/linkup/search/__init__.py @@ -1,6 +1,7 @@ """ Linkup Search API module. """ + from litellm.llms.linkup.search.transformation import LinkupSearchConfig __all__ = ["LinkupSearchConfig"] diff --git a/litellm/llms/linkup/search/transformation.py b/litellm/llms/linkup/search/transformation.py index 0554b8ab341..2b17d5642ac 100644 --- a/litellm/llms/linkup/search/transformation.py +++ b/litellm/llms/linkup/search/transformation.py @@ -3,6 +3,7 @@ Calls Linkup's /search endpoint to search the web. Linkup API Reference: https://docs.linkup.so/pages/documentation/api-reference/endpoint/post-search """ + from typing import Dict, List, Literal, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/minimax/chat/transformation.py b/litellm/llms/minimax/chat/transformation.py index 4095e57a8ae..69f228160f6 100644 --- a/litellm/llms/minimax/chat/transformation.py +++ b/litellm/llms/minimax/chat/transformation.py @@ -1,6 +1,7 @@ """ MiniMax OpenAI transformation config - extends OpenAI chat config for MiniMax's OpenAI-compatible API """ + from typing import List, Optional, Tuple import litellm diff --git a/litellm/llms/minimax/messages/transformation.py b/litellm/llms/minimax/messages/transformation.py index 13ed6ad3863..3190a5f5412 100644 --- a/litellm/llms/minimax/messages/transformation.py +++ b/litellm/llms/minimax/messages/transformation.py @@ -1,6 +1,7 @@ """ MiniMax Anthropic transformation config - extends AnthropicConfig for MiniMax's Anthropic-compatible API """ + from typing import Optional import litellm diff --git a/litellm/llms/mistral/chat/transformation.py b/litellm/llms/mistral/chat/transformation.py index 23fbe467fc8..f1ad3708236 100644 --- a/litellm/llms/mistral/chat/transformation.py +++ b/litellm/llms/mistral/chat/transformation.py @@ -344,9 +344,9 @@ class MistralConfig(OpenAIGPTConfig): # Handle both string and list content, preserving original format if isinstance(existing_content, str): # String content - prepend reasoning prompt - new_content: Union[ - str, list - ] = f"{reasoning_prompt}\n\n{existing_content}" + new_content: Union[str, list] = ( + f"{reasoning_prompt}\n\n{existing_content}" + ) elif isinstance(existing_content, list): # List content - prepend reasoning prompt as text block new_content = [ diff --git a/litellm/llms/mistral/ocr/transformation.py b/litellm/llms/mistral/ocr/transformation.py index 3d5e8763027..752df4f349e 100644 --- a/litellm/llms/mistral/ocr/transformation.py +++ b/litellm/llms/mistral/ocr/transformation.py @@ -1,6 +1,7 @@ """ Mistral OCR transformation implementation. """ + from typing import Any, Dict, Optional import httpx diff --git a/litellm/llms/moonshot/chat/transformation.py b/litellm/llms/moonshot/chat/transformation.py index e4d7b5f033b..4eb00fd81d6 100644 --- a/litellm/llms/moonshot/chat/transformation.py +++ b/litellm/llms/moonshot/chat/transformation.py @@ -19,8 +19,7 @@ class MoonshotChatConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -28,8 +27,7 @@ class MoonshotChatConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/nvidia_nim/chat/transformation.py b/litellm/llms/nvidia_nim/chat/transformation.py index e687229949b..b8f8b04eb53 100644 --- a/litellm/llms/nvidia_nim/chat/transformation.py +++ b/litellm/llms/nvidia_nim/chat/transformation.py @@ -7,6 +7,7 @@ This file only contains param mapping logic API calling is done using the OpenAI SDK with an api_base """ + from litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig diff --git a/litellm/llms/oci/chat/transformation.py b/litellm/llms/oci/chat/transformation.py index 79cd1c00606..62104e921a4 100644 --- a/litellm/llms/oci/chat/transformation.py +++ b/litellm/llms/oci/chat/transformation.py @@ -461,9 +461,7 @@ class OCIChatConfig(BaseConfig): private_key = ( load_private_key_from_str(oci_key_content) if oci_key_content - else load_private_key_from_file(oci_key_file) - if oci_key_file - else None + else load_private_key_from_file(oci_key_file) if oci_key_file else None ) if private_key is None: diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.py index 6a03325e6c7..32981776753 100644 --- a/litellm/llms/ollama/completion/transformation.py +++ b/litellm/llms/ollama/completion/transformation.py @@ -93,9 +93,9 @@ class OllamaConfig(BaseConfig): repeat_penalty: Optional[float] = None temperature: Optional[float] = None seed: Optional[int] = None - stop: Optional[ - list - ] = None # stop is a list based on this - https://github.com/ollama/ollama/pull/442 + stop: Optional[list] = ( + None # stop is a list based on this - https://github.com/ollama/ollama/pull/442 + ) tfs_z: Optional[float] = None num_predict: Optional[int] = None top_k: Optional[int] = None diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py index c12c6e6ba09..6b7ec4dfb1c 100644 --- a/litellm/llms/openai/chat/gpt_transformation.py +++ b/litellm/llms/openai/chat/gpt_transformation.py @@ -370,10 +370,10 @@ class OpenAIGPTConfig(BaseLLMModelInfo, BaseConfig): List[OpenAIMessageContentListBlock], message_content ) for i, content_item in enumerate(message_content_types): - message_content_types[ - i - ] = await self._async_transform_content_item( - cast(OpenAIMessageContentListBlock, content_item), + message_content_types[i] = ( + await self._async_transform_content_item( + cast(OpenAIMessageContentListBlock, content_item), + ) ) return messages diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py index f854cdb13d0..0ce9d57a65f 100644 --- a/litellm/llms/openai/chat/guardrail_translation/handler.py +++ b/litellm/llms/openai/chat/guardrail_translation/handler.py @@ -86,9 +86,9 @@ class OpenAIChatCompletionsHandler(BaseTranslation): if tool_calls_to_check: inputs["tool_calls"] = tool_calls_to_check # type: ignore if messages: - inputs[ - "structured_messages" - ] = messages # pass the openai /chat/completions messages to the guardrail, as-is + inputs["structured_messages"] = ( + messages # pass the openai /chat/completions messages to the guardrail, as-is + ) # Pass tools (function definitions) to the guardrail tools = data.get("tools") if tools: diff --git a/litellm/llms/openai/chat/o_series_transformation.py b/litellm/llms/openai/chat/o_series_transformation.py index fe8aec9bc2b..02ae2cc9750 100644 --- a/litellm/llms/openai/chat/o_series_transformation.py +++ b/litellm/llms/openai/chat/o_series_transformation.py @@ -141,8 +141,7 @@ class OpenAIOSeriesConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -150,8 +149,7 @@ class OpenAIOSeriesConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/openai/common_utils.py b/litellm/llms/openai/common_utils.py index 35723ccd637..c13a976c1b9 100644 --- a/litellm/llms/openai/common_utils.py +++ b/litellm/llms/openai/common_utils.py @@ -227,9 +227,9 @@ class BaseOpenAILLM: return httpx.AsyncClient( verify=ssl_config, transport=AsyncHTTPHandler._create_async_transport( - ssl_context=ssl_config - if isinstance(ssl_config, ssl.SSLContext) - else None, + ssl_context=( + ssl_config if isinstance(ssl_config, ssl.SSLContext) else None + ), ssl_verify=ssl_config if isinstance(ssl_config, bool) else None, shared_session=shared_session, ), diff --git a/litellm/llms/openai/completion/transformation.py b/litellm/llms/openai/completion/transformation.py index 44a4949d455..77dc0b54fe0 100644 --- a/litellm/llms/openai/completion/transformation.py +++ b/litellm/llms/openai/completion/transformation.py @@ -111,9 +111,9 @@ class OpenAITextCompletionConfig(BaseTextCompletionConfig, OpenAIGPTConfig): if "model" in response_object: model_response_object.model = response_object["model"] - model_response_object._hidden_params[ - "original_response" - ] = response_object # track original response, if users make a litellm.text_completion() request, we can return the original response + model_response_object._hidden_params["original_response"] = ( + response_object # track original response, if users make a litellm.text_completion() request, we can return the original response + ) return model_response_object except Exception as e: raise e diff --git a/litellm/llms/openai/fine_tuning/handler.py b/litellm/llms/openai/fine_tuning/handler.py index c065325254e..ca93622d9de 100644 --- a/litellm/llms/openai/fine_tuning/handler.py +++ b/litellm/llms/openai/fine_tuning/handler.py @@ -77,7 +77,14 @@ class OpenAIFineTuningAPI: _is_async: bool = False, api_version: Optional[str] = None, litellm_params: Optional[dict] = None, - ) -> Optional[Union[OpenAI, AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI,]]: + ) -> Optional[ + Union[ + OpenAI, + AsyncOpenAI, + AzureOpenAI, + AsyncAzureOpenAI, + ] + ]: received_args = locals() openai_client: Optional[ Union[OpenAI, AsyncOpenAI, AzureOpenAI, AsyncAzureOpenAI] diff --git a/litellm/llms/openai/openai.py b/litellm/llms/openai/openai.py index be542677480..2ac4974bb08 100644 --- a/litellm/llms/openai/openai.py +++ b/litellm/llms/openai/openai.py @@ -562,9 +562,9 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM): kwargs_with_provider = ( litellm_params.copy() if litellm_params else {} ) - kwargs_with_provider[ - "custom_llm_provider" - ] = custom_llm_provider + kwargs_with_provider["custom_llm_provider"] = ( + custom_llm_provider + ) # For OpenAI Chat Completions, use the chat completion agentic loop method agentic_response = ( diff --git a/litellm/llms/openai/transcriptions/whisper_transformation.py b/litellm/llms/openai/transcriptions/whisper_transformation.py index 1a7f47ae56e..fa507e1bc26 100644 --- a/litellm/llms/openai/transcriptions/whisper_transformation.py +++ b/litellm/llms/openai/transcriptions/whisper_transformation.py @@ -110,9 +110,9 @@ class OpenAIWhisperAudioTranscriptionConfig(BaseAudioTranscriptionConfig): if "response_format" not in data or ( data["response_format"] == "text" or data["response_format"] == "json" ): - data[ - "response_format" - ] = "verbose_json" # ensures 'duration' is received - used for cost calculation + data["response_format"] = ( + "verbose_json" # ensures 'duration' is received - used for cost calculation + ) return AudioTranscriptionRequestData( data=data, diff --git a/litellm/llms/openai_like/dynamic_config.py b/litellm/llms/openai_like/dynamic_config.py index 3d66556e522..fac453447fa 100644 --- a/litellm/llms/openai_like/dynamic_config.py +++ b/litellm/llms/openai_like/dynamic_config.py @@ -28,8 +28,7 @@ def create_config_class(provider: SimpleProviderConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -37,8 +36,7 @@ def create_config_class(provider: SimpleProviderConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/openrouter/chat/transformation.py b/litellm/llms/openrouter/chat/transformation.py index 86e63fd0c41..0d7850e8c74 100644 --- a/litellm/llms/openrouter/chat/transformation.py +++ b/litellm/llms/openrouter/chat/transformation.py @@ -70,9 +70,9 @@ class OpenrouterConfig(OpenAIGPTConfig): extra_body["models"] = models if route is not None: extra_body["route"] = route - mapped_openai_params[ - "extra_body" - ] = extra_body # openai client supports `extra_body` param + mapped_openai_params["extra_body"] = ( + extra_body # openai client supports `extra_body` param + ) return mapped_openai_params def _supports_cache_control_in_content(self, model: str) -> bool: diff --git a/litellm/llms/openrouter/embedding/transformation.py b/litellm/llms/openrouter/embedding/transformation.py index d1d0e911d16..8b836e8e5d2 100644 --- a/litellm/llms/openrouter/embedding/transformation.py +++ b/litellm/llms/openrouter/embedding/transformation.py @@ -6,6 +6,7 @@ OpenRouter is OpenAI-compatible and supports embeddings via the /v1/embeddings e Docs: https://openrouter.ai/docs """ + from typing import TYPE_CHECKING, Any, Optional import httpx diff --git a/litellm/llms/openrouter/image_edit/transformation.py b/litellm/llms/openrouter/image_edit/transformation.py index 9e5e313aad0..fcf066dd5ac 100644 --- a/litellm/llms/openrouter/image_edit/transformation.py +++ b/litellm/llms/openrouter/image_edit/transformation.py @@ -97,9 +97,9 @@ class OpenRouterImageEditConfig(BaseImageEditConfig): if key == "size": if "image_config" not in mapped_params: mapped_params["image_config"] = {} - mapped_params["image_config"][ - "aspect_ratio" - ] = self._map_size_to_aspect_ratio(cast(str, value)) + mapped_params["image_config"]["aspect_ratio"] = ( + self._map_size_to_aspect_ratio(cast(str, value)) + ) elif key == "quality": image_size = self._map_quality_to_image_size(cast(str, value)) if image_size: diff --git a/litellm/llms/ovhcloud/chat/transformation.py b/litellm/llms/ovhcloud/chat/transformation.py index 1416b782f17..342ad700e00 100644 --- a/litellm/llms/ovhcloud/chat/transformation.py +++ b/litellm/llms/ovhcloud/chat/transformation.py @@ -4,6 +4,7 @@ Support for OVHCloud AI Endpoints `/v1/chat/completions` endpoint. Our unified API follows the OpenAI standard. More information on our website: https://endpoints.ai.cloud.ovh.net """ + from typing import Optional, Union, List import httpx diff --git a/litellm/llms/ovhcloud/embedding/transformation.py b/litellm/llms/ovhcloud/embedding/transformation.py index 38e88da125f..6b5c43e2d06 100644 --- a/litellm/llms/ovhcloud/embedding/transformation.py +++ b/litellm/llms/ovhcloud/embedding/transformation.py @@ -2,6 +2,7 @@ This is OpenAI compatible - no transformation is applied """ + from typing import List, Optional, Union import httpx diff --git a/litellm/llms/parallel_ai/search/__init__.py b/litellm/llms/parallel_ai/search/__init__.py index b96914f13dd..23c4b9751d7 100644 --- a/litellm/llms/parallel_ai/search/__init__.py +++ b/litellm/llms/parallel_ai/search/__init__.py @@ -1,6 +1,7 @@ """ Parallel AI Search API module. """ + from litellm.llms.parallel_ai.search.transformation import ParallelAISearchConfig __all__ = ["ParallelAISearchConfig"] diff --git a/litellm/llms/parallel_ai/search/transformation.py b/litellm/llms/parallel_ai/search/transformation.py index e19bc5400d1..12d570f1733 100644 --- a/litellm/llms/parallel_ai/search/transformation.py +++ b/litellm/llms/parallel_ai/search/transformation.py @@ -3,6 +3,7 @@ Calls Parallel AI's /search endpoint to search the web. Parallel AI API Reference: https://docs.parallel.ai/api-reference/search-and-extract-api-beta/search """ + from typing import Dict, List, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/perplexity/search/transformation.py b/litellm/llms/perplexity/search/transformation.py index f89d5565498..ea96f87957c 100644 --- a/litellm/llms/perplexity/search/transformation.py +++ b/litellm/llms/perplexity/search/transformation.py @@ -1,6 +1,7 @@ """ Calls Perplexity's /search endpoint to search the web. """ + from typing import Dict, List, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/petals/completion/transformation.py b/litellm/llms/petals/completion/transformation.py index 24910cba8f3..d50afc4625a 100644 --- a/litellm/llms/petals/completion/transformation.py +++ b/litellm/llms/petals/completion/transformation.py @@ -37,9 +37,9 @@ class PetalsConfig(BaseConfig): """ max_length: Optional[int] = None - max_new_tokens: Optional[ - int - ] = litellm.max_tokens # petals requires max tokens to be set + max_new_tokens: Optional[int] = ( + litellm.max_tokens + ) # petals requires max tokens to be set do_sample: Optional[bool] = None temperature: Optional[float] = None top_k: Optional[int] = None diff --git a/litellm/llms/predibase/chat/transformation.py b/litellm/llms/predibase/chat/transformation.py index 9fbb9d6c9e2..0569318062f 100644 --- a/litellm/llms/predibase/chat/transformation.py +++ b/litellm/llms/predibase/chat/transformation.py @@ -31,9 +31,9 @@ class PredibaseConfig(BaseConfig): DEFAULT_MAX_TOKENS # openai default - requests hang if max_new_tokens not given ) repetition_penalty: Optional[float] = None - return_full_text: Optional[ - bool - ] = False # by default don't return the input as part of the output + return_full_text: Optional[bool] = ( + False # by default don't return the input as part of the output + ) seed: Optional[int] = None stop: Optional[List[str]] = None temperature: Optional[float] = None @@ -100,9 +100,9 @@ class PredibaseConfig(BaseConfig): optional_params["top_p"] = value if param == "n": optional_params["best_of"] = value - optional_params[ - "do_sample" - ] = True # Need to sample if you want best of for hf inference endpoints + optional_params["do_sample"] = ( + True # Need to sample if you want best of for hf inference endpoints + ) if param == "stream": optional_params["stream"] = value if param == "stop": diff --git a/litellm/llms/runwayml/text_to_speech/__init__.py b/litellm/llms/runwayml/text_to_speech/__init__.py index 98337a8321a..cf6e9071bf0 100644 --- a/litellm/llms/runwayml/text_to_speech/__init__.py +++ b/litellm/llms/runwayml/text_to_speech/__init__.py @@ -1,4 +1,5 @@ """RunwayML Text-to-Speech implementation.""" + from .transformation import RunwayMLTextToSpeechConfig __all__ = ["RunwayMLTextToSpeechConfig"] diff --git a/litellm/llms/runwayml/text_to_speech/transformation.py b/litellm/llms/runwayml/text_to_speech/transformation.py index dfcb92bc68b..314a538f7c5 100644 --- a/litellm/llms/runwayml/text_to_speech/transformation.py +++ b/litellm/llms/runwayml/text_to_speech/transformation.py @@ -3,6 +3,7 @@ RunwayML Text-to-Speech transformation Maps OpenAI TTS spec to RunwayML Text-to-Speech API """ + import asyncio import time from typing import TYPE_CHECKING, Any, Coroutine, Dict, Optional, Tuple, Union diff --git a/litellm/llms/sagemaker/completion/transformation.py b/litellm/llms/sagemaker/completion/transformation.py index dd7cb603905..3e4e2460cdb 100644 --- a/litellm/llms/sagemaker/completion/transformation.py +++ b/litellm/llms/sagemaker/completion/transformation.py @@ -100,9 +100,9 @@ class SagemakerConfig(BaseConfig): optional_params["top_p"] = value if param == "n": optional_params["best_of"] = value - optional_params[ - "do_sample" - ] = True # Need to sample if you want best of for hf inference endpoints + optional_params["do_sample"] = ( + True # Need to sample if you want best of for hf inference endpoints + ) if param == "stream": optional_params["stream"] = value if param == "stop": diff --git a/litellm/llms/sambanova/chat.py b/litellm/llms/sambanova/chat.py index 3c4003f72e9..2120256f918 100644 --- a/litellm/llms/sambanova/chat.py +++ b/litellm/llms/sambanova/chat.py @@ -100,8 +100,7 @@ class SambanovaConfig(OpenAIGPTConfig): @overload def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: Literal[True] - ) -> Coroutine[Any, Any, List[AllMessageValues]]: - ... + ) -> Coroutine[Any, Any, List[AllMessageValues]]: ... @overload def _transform_messages( @@ -109,8 +108,7 @@ class SambanovaConfig(OpenAIGPTConfig): messages: List[AllMessageValues], model: str, is_async: Literal[False] = False, - ) -> List[AllMessageValues]: - ... + ) -> List[AllMessageValues]: ... def _transform_messages( self, messages: List[AllMessageValues], model: str, is_async: bool = False diff --git a/litellm/llms/sambanova/embedding/transformation.py b/litellm/llms/sambanova/embedding/transformation.py index eca44c7c039..5c88188b84e 100644 --- a/litellm/llms/sambanova/embedding/transformation.py +++ b/litellm/llms/sambanova/embedding/transformation.py @@ -2,6 +2,7 @@ This is OpenAI compatible - no transformation is applied """ + from typing import List, Optional, Union import httpx diff --git a/litellm/llms/sap/chat/transformation.py b/litellm/llms/sap/chat/transformation.py index 7f6bab4a1d5..0d6d387122d 100755 --- a/litellm/llms/sap/chat/transformation.py +++ b/litellm/llms/sap/chat/transformation.py @@ -1,6 +1,7 @@ """ Translate from OpenAI's `/v1/chat/completions` to SAP Generative AI Hub's Orchestration Service`v2/completion` """ + from typing import ( List, Optional, diff --git a/litellm/llms/searchapi/search/__init__.py b/litellm/llms/searchapi/search/__init__.py index 783238c9f73..19a2e652416 100644 --- a/litellm/llms/searchapi/search/__init__.py +++ b/litellm/llms/searchapi/search/__init__.py @@ -1,4 +1,5 @@ """SearchAPI.io search integration for LiteLLM.""" + from litellm.llms.searchapi.search.transformation import SearchAPIConfig __all__ = ["SearchAPIConfig"] diff --git a/litellm/llms/searchapi/search/transformation.py b/litellm/llms/searchapi/search/transformation.py index 92b2814018d..c04e1377f9c 100644 --- a/litellm/llms/searchapi/search/transformation.py +++ b/litellm/llms/searchapi/search/transformation.py @@ -3,6 +3,7 @@ Calls SearchAPI.io's Google Search API endpoint. SearchAPI.io API Reference: https://www.searchapi.io/docs/google """ + from typing import Dict, List, Literal, Optional, TypedDict, Union, cast from urllib.parse import urlencode diff --git a/litellm/llms/searxng/__init__.py b/litellm/llms/searxng/__init__.py index f7ad1978c76..320cebb06f2 100644 --- a/litellm/llms/searxng/__init__.py +++ b/litellm/llms/searxng/__init__.py @@ -1,6 +1,7 @@ """ SearXNG API integration module. """ + from litellm.llms.searxng.search.transformation import SearXNGSearchConfig __all__ = ["SearXNGSearchConfig"] diff --git a/litellm/llms/searxng/search/__init__.py b/litellm/llms/searxng/search/__init__.py index 88ac5dc629b..b52b323a2eb 100644 --- a/litellm/llms/searxng/search/__init__.py +++ b/litellm/llms/searxng/search/__init__.py @@ -1,6 +1,7 @@ """ SearXNG Search API module. """ + from litellm.llms.searxng.search.transformation import SearXNGSearchConfig __all__ = ["SearXNGSearchConfig"] diff --git a/litellm/llms/searxng/search/transformation.py b/litellm/llms/searxng/search/transformation.py index bbd3b765010..ee6f3895721 100644 --- a/litellm/llms/searxng/search/transformation.py +++ b/litellm/llms/searxng/search/transformation.py @@ -3,6 +3,7 @@ Calls SearXNG's /search endpoint to search the web. SearXNG API Reference: https://docs.searxng.org/dev/search_api.html """ + from typing import Dict, List, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/serper/search/__init__.py b/litellm/llms/serper/search/__init__.py index cdb4bd4b53f..3bf59ee8d6b 100644 --- a/litellm/llms/serper/search/__init__.py +++ b/litellm/llms/serper/search/__init__.py @@ -1,6 +1,7 @@ """ Serper Search API module. """ + from litellm.llms.serper.search.transformation import SerperSearchConfig __all__ = ["SerperSearchConfig"] diff --git a/litellm/llms/serper/search/transformation.py b/litellm/llms/serper/search/transformation.py index 34e726dc77d..0daccbe652b 100644 --- a/litellm/llms/serper/search/transformation.py +++ b/litellm/llms/serper/search/transformation.py @@ -3,6 +3,7 @@ Calls Serper's /search endpoint to search Google. Serper API Reference: https://serper.dev """ + from typing import Dict, List, Optional, TypedDict, Union import httpx diff --git a/litellm/llms/stability/image_generation/transformation.py b/litellm/llms/stability/image_generation/transformation.py index ac63548bf56..c8c2a16fcd1 100644 --- a/litellm/llms/stability/image_generation/transformation.py +++ b/litellm/llms/stability/image_generation/transformation.py @@ -80,9 +80,9 @@ class StabilityImageGenerationConfig(BaseImageGenerationConfig): if k in supported_params: # Map size to aspect_ratio if k == "size" and v in OPENAI_SIZE_TO_STABILITY_ASPECT_RATIO: - optional_params[ - "aspect_ratio" - ] = OPENAI_SIZE_TO_STABILITY_ASPECT_RATIO[v] + optional_params["aspect_ratio"] = ( + OPENAI_SIZE_TO_STABILITY_ASPECT_RATIO[v] + ) elif k == "n": # Store n for later, but don't pass to Stability optional_params["_n"] = v diff --git a/litellm/llms/tavily/search/__init__.py b/litellm/llms/tavily/search/__init__.py index 6e3fe1163c7..38ca5b60ae0 100644 --- a/litellm/llms/tavily/search/__init__.py +++ b/litellm/llms/tavily/search/__init__.py @@ -1,6 +1,7 @@ """ Tavily Search API module. """ + from litellm.llms.tavily.search.transformation import TavilySearchConfig __all__ = ["TavilySearchConfig"] diff --git a/litellm/llms/tavily/search/transformation.py b/litellm/llms/tavily/search/transformation.py index 1228433b539..ec96db96f36 100644 --- a/litellm/llms/tavily/search/transformation.py +++ b/litellm/llms/tavily/search/transformation.py @@ -3,6 +3,7 @@ Calls Tavily's /search endpoint to search the web. Tavily API Reference: https://docs.tavily.com/documentation/api-reference/endpoint/search """ + from typing import Dict, List, Optional, TypedDict, Union import httpx @@ -32,7 +33,9 @@ class TavilySearchRequest(_TavilySearchRequestRequired, total=False): include_domains: List[str] # Optional - list of domains to include (max 300) exclude_domains: List[str] # Optional - list of domains to exclude (max 150) topic: str # Optional - category of search ('general', 'news', 'finance'), default 'general' - search_depth: str # Optional - depth of search ('basic', 'advanced'), default 'basic' + search_depth: ( + str # Optional - depth of search ('basic', 'advanced'), default 'basic' + ) include_answer: Union[bool, str] # Optional - include LLM-generated answer include_raw_content: Union[bool, str] # Optional - include raw HTML content include_images: bool # Optional - perform image search diff --git a/litellm/llms/vercel_ai_gateway/chat/transformation.py b/litellm/llms/vercel_ai_gateway/chat/transformation.py index 81a1688b909..fda1c4a77cb 100644 --- a/litellm/llms/vercel_ai_gateway/chat/transformation.py +++ b/litellm/llms/vercel_ai_gateway/chat/transformation.py @@ -63,9 +63,9 @@ class VercelAIGatewayConfig(OpenAIGPTConfig): if provider_options is not None: extra_body["providerOptions"] = provider_options - mapped_openai_params[ - "extra_body" - ] = extra_body # openai client supports `extra_body` param + mapped_openai_params["extra_body"] = ( + extra_body # openai client supports `extra_body` param + ) return mapped_openai_params def transform_request( diff --git a/litellm/llms/vertex_ai/batches/handler.py b/litellm/llms/vertex_ai/batches/handler.py index 2cb02942061..028e02eb0ca 100644 --- a/litellm/llms/vertex_ai/batches/handler.py +++ b/litellm/llms/vertex_ai/batches/handler.py @@ -73,8 +73,10 @@ class VertexAIBatchPrediction(VertexLLM): "Authorization": f"Bearer {access_token}", } - vertex_batch_request: VertexAIBatchPredictionJob = VertexAIBatchTransformation.transform_openai_batch_request_to_vertex_ai_batch_request( - request=create_batch_data + vertex_batch_request: VertexAIBatchPredictionJob = ( + VertexAIBatchTransformation.transform_openai_batch_request_to_vertex_ai_batch_request( + request=create_batch_data + ) ) if _is_async is True: diff --git a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py index b677cf3b1ec..7316f81c9f7 100644 --- a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py +++ b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py @@ -93,9 +93,9 @@ class ContextCachingEndpoints(VertexBase): model=model, vertex_project=vertex_project, vertex_location=vertex_location, - vertex_api_version="v1beta1" - if custom_llm_provider == "vertex_ai_beta" - else "v1", + vertex_api_version=( + "v1beta1" if custom_llm_provider == "vertex_ai_beta" else "v1" + ), ) def check_cache( diff --git a/litellm/llms/vertex_ai/cost_calculator.py b/litellm/llms/vertex_ai/cost_calculator.py index 4ca3d29e7d2..9fa57f6bf96 100644 --- a/litellm/llms/vertex_ai/cost_calculator.py +++ b/litellm/llms/vertex_ai/cost_calculator.py @@ -215,7 +215,9 @@ def _handle_128k_pricing( ): completion_cost = completion_tokens * output_cost_per_token_above_128k_tokens else: - completion_cost = completion_tokens * (model_info["output_cost_per_token"] or 0.0) + completion_cost = completion_tokens * ( + model_info["output_cost_per_token"] or 0.0 + ) return prompt_cost, completion_cost diff --git a/litellm/llms/vertex_ai/fine_tuning/handler.py b/litellm/llms/vertex_ai/fine_tuning/handler.py index 77891e245cd..a5971de0e94 100644 --- a/litellm/llms/vertex_ai/fine_tuning/handler.py +++ b/litellm/llms/vertex_ai/fine_tuning/handler.py @@ -65,9 +65,9 @@ class VertexFineTuningAPI(VertexLLM): ) if create_fine_tuning_job_data.validation_file: - supervised_tuning_spec[ - "validation_dataset" - ] = create_fine_tuning_job_data.validation_file + supervised_tuning_spec["validation_dataset"] = ( + create_fine_tuning_job_data.validation_file + ) _vertex_hyperparameters = ( self._transform_openai_hyperparameters_to_vertex_hyperparameters( @@ -349,9 +349,9 @@ class VertexFineTuningAPI(VertexLLM): elif "cachedContents" in request_route: _model = request_data.get("model") if _model is not None and "/publishers/google/models/" not in _model: - request_data[ - "model" - ] = f"projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{_model}" + request_data["model"] = ( + f"projects/{vertex_project}/locations/{vertex_location}/publishers/google/models/{_model}" + ) url = f"{base_url}/v1beta1/projects/{vertex_project}/locations/{vertex_location}{request_route}" else: diff --git a/litellm/llms/vertex_ai/gemini/transformation.py b/litellm/llms/vertex_ai/gemini/transformation.py index 7945c44d44c..7d1206395b1 100644 --- a/litellm/llms/vertex_ai/gemini/transformation.py +++ b/litellm/llms/vertex_ai/gemini/transformation.py @@ -3,6 +3,7 @@ Transformation logic from OpenAI format to Gemini format. Why separate file? Make it easy to see how transformation works """ + import json import os from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union, cast diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py index 36f51c5b2f5..13896d136c8 100644 --- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py +++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py @@ -500,9 +500,9 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): value = _remove_strict_from_schema(value) for tool in value: - openai_function_object: Optional[ - ChatCompletionToolParamFunctionChunk - ] = None + openai_function_object: Optional[ChatCompletionToolParamFunctionChunk] = ( + None + ) if "function" in tool: # tools list _openai_function_object = ChatCompletionToolParamFunctionChunk( # type: ignore **tool["function"] @@ -634,15 +634,15 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): _tools_list.append(search_tool) if googleSearchRetrieval is not None: retrieval_tool = Tools() - retrieval_tool[ - VertexToolName.GOOGLE_SEARCH_RETRIEVAL.value - ] = googleSearchRetrieval + retrieval_tool[VertexToolName.GOOGLE_SEARCH_RETRIEVAL.value] = ( + googleSearchRetrieval + ) _tools_list.append(retrieval_tool) if enterpriseWebSearch is not None: enterprise_tool = Tools() - enterprise_tool[ - VertexToolName.ENTERPRISE_WEB_SEARCH.value - ] = enterpriseWebSearch + enterprise_tool[VertexToolName.ENTERPRISE_WEB_SEARCH.value] = ( + enterpriseWebSearch + ) _tools_list.append(enterprise_tool) if code_execution is not None: code_tool = Tools() @@ -1089,16 +1089,16 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): param_description="thinking_budget", ) if VertexGeminiConfig._is_gemini_3_or_newer(model): - optional_params[ - "thinkingConfig" - ] = VertexGeminiConfig._map_reasoning_effort_to_thinking_level( - effort_value, model + optional_params["thinkingConfig"] = ( + VertexGeminiConfig._map_reasoning_effort_to_thinking_level( + effort_value, model + ) ) else: - optional_params[ - "thinkingConfig" - ] = VertexGeminiConfig._map_reasoning_effort_to_thinking_budget( - effort_value, model + optional_params["thinkingConfig"] = ( + VertexGeminiConfig._map_reasoning_effort_to_thinking_budget( + effort_value, model + ) ) elif param == "thinking": # Validate no conflict with thinking_level @@ -1107,11 +1107,11 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): param_name="thinking", param_description="thinking_budget", ) - optional_params[ - "thinkingConfig" - ] = VertexGeminiConfig._map_thinking_param( - cast(AnthropicThinkingParam, value), - model=model, + optional_params["thinkingConfig"] = ( + VertexGeminiConfig._map_thinking_param( + cast(AnthropicThinkingParam, value), + model=model, + ) ) elif param == "modalities" and isinstance(value, list): response_modalities = self.map_response_modalities(value) @@ -1533,10 +1533,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): _tool_response_chunk["provider_specific_fields"] = { # type: ignore "thought_signature": thought_signature } - _tool_response_chunk[ - "id" - ] = _encode_tool_call_id_with_signature( - _tool_response_chunk["id"] or "", thought_signature + _tool_response_chunk["id"] = ( + _encode_tool_call_id_with_signature( + _tool_response_chunk["id"] or "", thought_signature + ) ) _tools.append(_tool_response_chunk) cumulative_tool_call_idx += 1 @@ -2383,28 +2383,28 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig): ## ADD METADATA TO RESPONSE ## setattr(model_response, "vertex_ai_grounding_metadata", grounding_metadata) - model_response._hidden_params[ - "vertex_ai_grounding_metadata" - ] = grounding_metadata + model_response._hidden_params["vertex_ai_grounding_metadata"] = ( + grounding_metadata + ) setattr( model_response, "vertex_ai_url_context_metadata", url_context_metadata ) - model_response._hidden_params[ - "vertex_ai_url_context_metadata" - ] = url_context_metadata + model_response._hidden_params["vertex_ai_url_context_metadata"] = ( + url_context_metadata + ) setattr(model_response, "vertex_ai_safety_results", safety_ratings) - model_response._hidden_params[ - "vertex_ai_safety_results" - ] = safety_ratings # older approach - maintaining to prevent regressions + model_response._hidden_params["vertex_ai_safety_results"] = ( + safety_ratings # older approach - maintaining to prevent regressions + ) ## ADD CITATION METADATA ## setattr(model_response, "vertex_ai_citation_metadata", citation_metadata) - model_response._hidden_params[ - "vertex_ai_citation_metadata" - ] = citation_metadata # older approach - maintaining to prevent regressions + model_response._hidden_params["vertex_ai_citation_metadata"] = ( + citation_metadata # older approach - maintaining to prevent regressions + ) ## ADD TRAFFIC TYPE ## traffic_type = completion_response.get("usageMetadata", {}).get( diff --git a/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py b/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py index 98e02743bd2..f4bda8d1bed 100644 --- a/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py +++ b/litellm/llms/vertex_ai/image_generation/vertex_gemini_transformation.py @@ -313,11 +313,11 @@ class VertexAIGeminiImageGenerationConfig(BaseImageGenerationConfig, VertexLLM): ImageObject( b64_json=inline_data["data"], url=None, - provider_specific_fields={ - "thought_signature": thought_sig - } - if thought_sig - else None, + provider_specific_fields=( + {"thought_signature": thought_sig} + if thought_sig + else None + ), ) ) diff --git a/litellm/llms/vertex_ai/ocr/__init__.py b/litellm/llms/vertex_ai/ocr/__init__.py index 15da24f3089..915fbd49030 100644 --- a/litellm/llms/vertex_ai/ocr/__init__.py +++ b/litellm/llms/vertex_ai/ocr/__init__.py @@ -1,4 +1,5 @@ """Vertex AI OCR module.""" + from .transformation import VertexAIOCRConfig __all__ = ["VertexAIOCRConfig"] diff --git a/litellm/llms/vertex_ai/ocr/deepseek_transformation.py b/litellm/llms/vertex_ai/ocr/deepseek_transformation.py index 953bb51fd1c..516ee03ba55 100644 --- a/litellm/llms/vertex_ai/ocr/deepseek_transformation.py +++ b/litellm/llms/vertex_ai/ocr/deepseek_transformation.py @@ -1,6 +1,7 @@ """ Vertex AI DeepSeek OCR transformation implementation. """ + import json from typing import TYPE_CHECKING, Any, Dict, Optional @@ -314,9 +315,11 @@ class VertexAIDeepSeekOCRConfig(BaseOCRConfig): "pages": [ { "index": 0, - "markdown": content - if isinstance(content, str) - else json.dumps(content), + "markdown": ( + content + if isinstance(content, str) + else json.dumps(content) + ), } ], "model": ocr_data.get("model", model), diff --git a/litellm/llms/vertex_ai/ocr/transformation.py b/litellm/llms/vertex_ai/ocr/transformation.py index 6fe88459ea2..cbf15803132 100644 --- a/litellm/llms/vertex_ai/ocr/transformation.py +++ b/litellm/llms/vertex_ai/ocr/transformation.py @@ -1,6 +1,7 @@ """ Vertex AI Mistral OCR transformation implementation. """ + from typing import Dict, Optional from litellm._logging import verbose_logger diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py index 5d94cd42129..f523f814d5f 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py @@ -5,6 +5,7 @@ This handler provides token counting for partner models hosted on Vertex AI. Unlike Gemini models which use Google's token counting API, partner models use their respective publisher-specific count-tokens endpoints. """ + from typing import Any, Dict, Optional from litellm.llms.custom_httpx.http_handler import get_async_httpx_client diff --git a/litellm/llms/vertex_ai/vertex_embeddings/embedding_handler.py b/litellm/llms/vertex_ai/vertex_embeddings/embedding_handler.py index 5fffd983c24..18c5ec3d839 100644 --- a/litellm/llms/vertex_ai/vertex_embeddings/embedding_handler.py +++ b/litellm/llms/vertex_ai/vertex_embeddings/embedding_handler.py @@ -90,8 +90,10 @@ class VertexEmbedding(VertexBase): use_psc_endpoint_format=use_psc_endpoint_format, ) headers = self.set_headers(auth_header=auth_header, extra_headers=extra_headers) - vertex_request: VertexEmbeddingRequest = litellm.vertexAITextEmbeddingConfig.transform_openai_request_to_vertex_embedding_request( - input=input, optional_params=optional_params, model=model + vertex_request: VertexEmbeddingRequest = ( + litellm.vertexAITextEmbeddingConfig.transform_openai_request_to_vertex_embedding_request( + input=input, optional_params=optional_params, model=model + ) ) _client_params = {} @@ -184,8 +186,10 @@ class VertexEmbedding(VertexBase): use_psc_endpoint_format=use_psc_endpoint_format, ) headers = self.set_headers(auth_header=auth_header, extra_headers=extra_headers) - vertex_request: VertexEmbeddingRequest = litellm.vertexAITextEmbeddingConfig.transform_openai_request_to_vertex_embedding_request( - input=input, optional_params=optional_params, model=model + vertex_request: VertexEmbeddingRequest = ( + litellm.vertexAITextEmbeddingConfig.transform_openai_request_to_vertex_embedding_request( + input=input, optional_params=optional_params, model=model + ) ) _async_client_params = {} diff --git a/litellm/llms/voyage/embedding/transformation_contextual.py b/litellm/llms/voyage/embedding/transformation_contextual.py index 4df2fa4ba31..40328062e09 100644 --- a/litellm/llms/voyage/embedding/transformation_contextual.py +++ b/litellm/llms/voyage/embedding/transformation_contextual.py @@ -2,6 +2,7 @@ This module is used to transform the request and response for the Voyage contextualized embeddings API. This would be used for all the contextualized embeddings models in Voyage. """ + from typing import List, Optional, Union import httpx diff --git a/litellm/ocr/__init__.py b/litellm/ocr/__init__.py index a20b0ef6cad..e97497b2db7 100644 --- a/litellm/ocr/__init__.py +++ b/litellm/ocr/__init__.py @@ -1,4 +1,5 @@ """OCR module for LiteLLM.""" + from .main import aocr, ocr __all__ = ["ocr", "aocr"] diff --git a/litellm/ocr/main.py b/litellm/ocr/main.py index d90a931b59a..5d73ddc8972 100644 --- a/litellm/ocr/main.py +++ b/litellm/ocr/main.py @@ -1,6 +1,7 @@ """ Main OCR function for LiteLLM. """ + import asyncio import base64 import contextvars @@ -262,11 +263,11 @@ def ocr( api_base = dynamic_api_base # Get provider config - ocr_provider_config: Optional[ - BaseOCRConfig - ] = ProviderConfigManager.get_provider_ocr_config( - model=model, - provider=litellm.LlmProviders(custom_llm_provider), + ocr_provider_config: Optional[BaseOCRConfig] = ( + ProviderConfigManager.get_provider_ocr_config( + model=model, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if ocr_provider_config is None: diff --git a/litellm/proxy/_experimental/mcp_server/db.py b/litellm/proxy/_experimental/mcp_server/db.py index fbef33c32ed..de265f1304c 100644 --- a/litellm/proxy/_experimental/mcp_server/db.py +++ b/litellm/proxy/_experimental/mcp_server/db.py @@ -188,12 +188,12 @@ async def get_mcp_server( """ Returns the matching mcp server from the db iff exists """ - mcp_server: Optional[ - LiteLLM_MCPServerTable - ] = await prisma_client.db.litellm_mcpservertable.find_unique( - where={ - "server_id": server_id, - } + mcp_server: Optional[LiteLLM_MCPServerTable] = ( + await prisma_client.db.litellm_mcpservertable.find_unique( + where={ + "server_id": server_id, + } + ) ) return mcp_server @@ -204,12 +204,12 @@ async def get_mcp_servers( """ Returns the matching mcp servers from the db with the server_ids """ - _mcp_servers: List[ - LiteLLM_MCPServerTable - ] = await prisma_client.db.litellm_mcpservertable.find_many( - where={ - "server_id": {"in": server_ids}, - } + _mcp_servers: List[LiteLLM_MCPServerTable] = ( + await prisma_client.db.litellm_mcpservertable.find_many( + where={ + "server_id": {"in": server_ids}, + } + ) ) final_mcp_servers: List[LiteLLM_MCPServerTable] = [] for _mcp_server in _mcp_servers: diff --git a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py index 07309eb57f2..e5a8211f94d 100644 --- a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py +++ b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py @@ -541,9 +541,9 @@ def _build_oauth_protected_resource_response( ) ], "resource": resource_url, - "scopes_supported": mcp_server.scopes - if mcp_server and mcp_server.scopes - else [], + "scopes_supported": ( + mcp_server.scopes if mcp_server and mcp_server.scopes else [] + ), } @@ -653,16 +653,18 @@ def _build_oauth_authorization_server_response( "authorization_endpoint": authorization_endpoint, "token_endpoint": token_endpoint, "response_types_supported": ["code"], - "scopes_supported": mcp_server.scopes - if mcp_server and mcp_server.scopes - else [], + "scopes_supported": ( + mcp_server.scopes if mcp_server and mcp_server.scopes else [] + ), "grant_types_supported": ["authorization_code", "refresh_token"], "code_challenge_methods_supported": ["S256"], "token_endpoint_auth_methods_supported": ["client_secret_post"], # Claude expects a registration endpoint, even if we just fake it - "registration_endpoint": f"{request_base_url}/{mcp_server_name}/register" - if mcp_server_name - else f"{request_base_url}/register", + "registration_endpoint": ( + f"{request_base_url}/{mcp_server_name}/register" + if mcp_server_name + else f"{request_base_url}/register" + ), } diff --git a/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py b/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py index 0bafd7da265..0e32bfd7026 100644 --- a/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py +++ b/litellm/proxy/_experimental/mcp_server/semantic_tool_filter.py @@ -3,6 +3,7 @@ Semantic MCP Tool Filtering using semantic-router Filters MCP tools semantically for /chat/completions and /responses endpoints. """ + from typing import TYPE_CHECKING, Any, Dict, List, Optional from litellm._logging import verbose_logger diff --git a/litellm/proxy/_experimental/mcp_server/utils.py b/litellm/proxy/_experimental/mcp_server/utils.py index 8189f212bcb..66d9fde0bb6 100644 --- a/litellm/proxy/_experimental/mcp_server/utils.py +++ b/litellm/proxy/_experimental/mcp_server/utils.py @@ -1,6 +1,7 @@ """ MCP Server Utilities """ + from typing import Any, Dict, Mapping, Optional, Tuple import os diff --git a/litellm/proxy/agent_endpoints/agent_registry.py b/litellm/proxy/agent_endpoints/agent_registry.py index 436de8b0aff..e69d16e0eac 100644 --- a/litellm/proxy/agent_endpoints/agent_registry.py +++ b/litellm/proxy/agent_endpoints/agent_registry.py @@ -171,13 +171,13 @@ class AgentRegistry: created_agent_dict = created_agent.model_dump() if created_agent.object_permission is not None: try: - created_agent_dict[ - "object_permission" - ] = created_agent.object_permission.model_dump() + created_agent_dict["object_permission"] = ( + created_agent.object_permission.model_dump() + ) except Exception: - created_agent_dict[ - "object_permission" - ] = created_agent.object_permission.dict() + created_agent_dict["object_permission"] = ( + created_agent.object_permission.dict() + ) return AgentResponse(**created_agent_dict) # type: ignore except Exception as e: raise Exception(f"Error adding agent to DB: {str(e)}") @@ -283,13 +283,13 @@ class AgentRegistry: patched_agent_dict = patched_agent.model_dump() if patched_agent.object_permission is not None: try: - patched_agent_dict[ - "object_permission" - ] = patched_agent.object_permission.model_dump() + patched_agent_dict["object_permission"] = ( + patched_agent.object_permission.model_dump() + ) except Exception: - patched_agent_dict[ - "object_permission" - ] = patched_agent.object_permission.dict() + patched_agent_dict["object_permission"] = ( + patched_agent.object_permission.dict() + ) return AgentResponse(**patched_agent_dict) # type: ignore except Exception as e: raise Exception(f"Error patching agent in DB: {str(e)}") @@ -384,13 +384,13 @@ class AgentRegistry: updated_agent_dict = updated_agent.model_dump() if updated_agent.object_permission is not None: try: - updated_agent_dict[ - "object_permission" - ] = updated_agent.object_permission.model_dump() + updated_agent_dict["object_permission"] = ( + updated_agent.object_permission.model_dump() + ) except Exception: - updated_agent_dict[ - "object_permission" - ] = updated_agent.object_permission.dict() + updated_agent_dict["object_permission"] = ( + updated_agent.object_permission.dict() + ) return AgentResponse(**updated_agent_dict) # type: ignore except Exception as e: raise Exception(f"Error updating agent in DB: {str(e)}") @@ -414,9 +414,9 @@ class AgentRegistry: # object_permission is eagerly loaded via include above if agent.object_permission is not None: try: - agent_dict[ - "object_permission" - ] = agent.object_permission.model_dump() + agent_dict["object_permission"] = ( + agent.object_permission.model_dump() + ) except Exception: agent_dict["object_permission"] = agent.object_permission.dict() agents.append(agent_dict) diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py index 6e5d4562b55..af15b8a11c1 100644 --- a/litellm/proxy/agent_endpoints/endpoints.py +++ b/litellm/proxy/agent_endpoints/endpoints.py @@ -177,10 +177,9 @@ async def get_agents( for agent in returned_agents: if agent.litellm_params is None: agent.litellm_params = {} - agent.litellm_params[ - "is_public" - ] = litellm.public_agent_groups is not None and ( - agent.agent_id in litellm.public_agent_groups + agent.litellm_params["is_public"] = ( + litellm.public_agent_groups is not None + and (agent.agent_id in litellm.public_agent_groups) ) if health_check: @@ -378,13 +377,13 @@ async def get_agent_by_id( agent_dict = agent_row.model_dump() if agent_row.object_permission is not None: try: - agent_dict[ - "object_permission" - ] = agent_row.object_permission.model_dump() + agent_dict["object_permission"] = ( + agent_row.object_permission.model_dump() + ) except Exception: - agent_dict[ - "object_permission" - ] = agent_row.object_permission.dict() + agent_dict["object_permission"] = ( + agent_row.object_permission.dict() + ) agent = AgentResponse(**agent_dict) # type: ignore else: # Agent found in memory — refresh spend from DB diff --git a/litellm/proxy/agent_endpoints/model_list_helpers.py b/litellm/proxy/agent_endpoints/model_list_helpers.py index 37308b92f78..b88c602ac34 100644 --- a/litellm/proxy/agent_endpoints/model_list_helpers.py +++ b/litellm/proxy/agent_endpoints/model_list_helpers.py @@ -3,6 +3,7 @@ Helper functions for appending A2A agents to model lists. Used by proxy model endpoints to make agents appear in UI alongside models. """ + from typing import List from litellm._logging import verbose_proxy_logger diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 68bde8434a6..b709609ac23 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -196,9 +196,7 @@ def _is_model_cost_zero( return True -def _is_cost_explicitly_configured( - model: str, llm_router: "Router" -) -> bool: +def _is_cost_explicitly_configured(model: str, llm_router: "Router") -> bool: """ Check if any deployment in the model group has cost fields explicitly set in its litellm.model_cost entry. @@ -215,10 +213,7 @@ def _is_cost_explicitly_configured( if model_id is None: continue raw_entry = litellm.model_cost.get(model_id, {}) - if ( - "input_cost_per_token" in raw_entry - or "output_cost_per_token" in raw_entry - ): + if "input_cost_per_token" in raw_entry or "output_cost_per_token" in raw_entry: return True return False @@ -456,9 +451,9 @@ async def common_checks( # noqa: PLR0915 model=_model, team_object=team_object, llm_router=llm_router, - team_model_aliases=valid_token.team_model_aliases - if valid_token - else None, + team_model_aliases=( + valid_token.team_model_aliases if valid_token else None + ), ): raise ProxyException( message=f"Team not allowed to access model. Team={team_object.team_id}, Model={_model}. Allowed team models = {team_object.models}", diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py index 046c39a9101..3ab65797841 100644 --- a/litellm/proxy/auth/user_api_key_auth.py +++ b/litellm/proxy/auth/user_api_key_auth.py @@ -708,11 +708,8 @@ async def _user_api_key_auth_builder( # noqa: PLR0915 ) # Routing uses unverified JWT claims only to choose auth path. # Final authentication is enforced by the selected validator. - route_jwt_to_oauth2 = ( - is_jwt - and _should_route_jwt_to_oauth2_override( - token=api_key, jwt_handler=jwt_handler - ) + route_jwt_to_oauth2 = is_jwt and _should_route_jwt_to_oauth2_override( + token=api_key, jwt_handler=jwt_handler ) if not is_jwt or route_jwt_to_oauth2: # return UserAPIKeyAuth object @@ -746,10 +743,7 @@ async def _user_api_key_auth_builder( # noqa: PLR0915 if jwt_handler.litellm_jwtauth.virtual_key_claim_field is not None: # Decode JWT to get claims without running full auth_builder jwt_claims: Optional[dict] - if ( - jwt_handler.litellm_jwtauth.oidc_userinfo_enabled - and not is_jwt - ): + if jwt_handler.litellm_jwtauth.oidc_userinfo_enabled and not is_jwt: jwt_claims = await jwt_handler.get_oidc_userinfo(token=api_key) else: jwt_claims = await jwt_handler.auth_jwt(token=api_key) @@ -984,9 +978,9 @@ async def _user_api_key_auth_builder( # noqa: PLR0915 route=route, ) if _end_user_object is not None: - end_user_params[ - "allowed_model_region" - ] = _end_user_object.allowed_model_region + end_user_params["allowed_model_region"] = ( + _end_user_object.allowed_model_region + ) if _end_user_object.litellm_budget_table is not None: _apply_budget_limits_to_end_user_params( end_user_params=end_user_params, @@ -1540,9 +1534,9 @@ async def _user_api_key_auth_builder( # noqa: PLR0915 if _end_user_object is not None: valid_token_dict.update(end_user_params) - valid_token_dict[ - "end_user_object_permission" - ] = _end_user_object.object_permission + valid_token_dict["end_user_object_permission"] = ( + _end_user_object.object_permission + ) # check if token is from litellm-ui, litellm ui makes keys to allow users to login with sso. These keys can only be used for LiteLLM UI functions # sso/login, ui/login, /key functions and /user functions @@ -1805,12 +1799,9 @@ async def _enforce_key_and_fallback_model_access( if config != {}: model_list = config.get("model_list", []) new_model_list = model_list - verbose_proxy_logger.debug( - f"\n new llm router model list {new_model_list}" - ) + verbose_proxy_logger.debug(f"\n new llm router model list {new_model_list}") elif ( - isinstance(valid_token.models, list) - and "all-team-models" in valid_token.models + isinstance(valid_token.models, list) and "all-team-models" in valid_token.models ): pass else: diff --git a/litellm/proxy/client/cli/commands/models.py b/litellm/proxy/client/cli/commands/models.py index 8acafbd88ab..387979a69a0 100644 --- a/litellm/proxy/client/cli/commands/models.py +++ b/litellm/proxy/client/cli/commands/models.py @@ -129,9 +129,11 @@ def list_models(ctx: click.Context, output_format: Literal["table", "json"]) -> table.add_row( str(model.get("id", "")), str(model.get("object", "model")), - format_timestamp(created) - if isinstance(created, int) - else format_iso_datetime_str(created), + ( + format_timestamp(created) + if isinstance(created, int) + else format_iso_datetime_str(created) + ), str(model.get("owned_by", "")), ) diff --git a/litellm/proxy/client/cli/main.py b/litellm/proxy/client/cli/main.py index 744acf38382..22de5a78614 100644 --- a/litellm/proxy/client/cli/main.py +++ b/litellm/proxy/client/cli/main.py @@ -45,14 +45,16 @@ def print_version(base_url: str, api_key: Optional[str]): expose_value=False, help="Show the LiteLLM Proxy CLI and server version and exit.", callback=lambda ctx, param, value: ( - print_version( - ctx.params.get("base_url") or "http://localhost:4000", - ctx.params.get("api_key"), + ( + print_version( + ctx.params.get("base_url") or "http://localhost:4000", + ctx.params.get("api_key"), + ) + or ctx.exit() ) - or ctx.exit() - ) - if value and not ctx.resilient_parsing - else None, + if value and not ctx.resilient_parsing + else None + ), ) @click.option( "--base-url", diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index 09200c96841..ebffe9d8802 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -818,9 +818,11 @@ class ProxyBaseLLMRequestProcessing: "Request received by LiteLLM: payload too large to log (%d bytes, limit %d). Keys: %s", len(_payload_str), MAX_PAYLOAD_SIZE_FOR_DEBUG_LOG, - list(self.data.keys()) - if isinstance(self.data, dict) - else type(self.data).__name__, + ( + list(self.data.keys()) + if isinstance(self.data, dict) + else type(self.data).__name__ + ), ) else: verbose_proxy_logger.debug( @@ -1080,9 +1082,9 @@ class ProxyBaseLLMRequestProcessing: # aliasing/routing, but the OpenAI-compatible response `model` field should reflect # what the client sent. if requested_model_from_client: - self.data[ - "_litellm_client_requested_model" - ] = requested_model_from_client + self.data["_litellm_client_requested_model"] = ( + requested_model_from_client + ) # Streaming: attach a closure that fires after all guardrail # end-of-stream blocks complete. CSW.__anext__ stores the @@ -1676,7 +1678,9 @@ class ProxyBaseLLMRequestProcessing: verbose_proxy_logger.debug("inside generator") try: str_so_far = "" - async for chunk in proxy_logging_obj.async_post_call_streaming_iterator_hook( + async for ( + chunk + ) in proxy_logging_obj.async_post_call_streaming_iterator_hook( user_api_key_dict=user_api_key_dict, response=response, request_data=request_data, @@ -1904,9 +1908,9 @@ class ProxyBaseLLMRequestProcessing: # Add cache-related fields to **params (handled by Usage.__init__) if cache_creation_input_tokens is not None: - usage_kwargs[ - "cache_creation_input_tokens" - ] = cache_creation_input_tokens + usage_kwargs["cache_creation_input_tokens"] = ( + cache_creation_input_tokens + ) if cache_read_input_tokens is not None: usage_kwargs["cache_read_input_tokens"] = cache_read_input_tokens diff --git a/litellm/proxy/common_utils/cache_coordinator.py b/litellm/proxy/common_utils/cache_coordinator.py index ccc73c5e6d8..24da9450ab8 100644 --- a/litellm/proxy/common_utils/cache_coordinator.py +++ b/litellm/proxy/common_utils/cache_coordinator.py @@ -22,11 +22,9 @@ T = TypeVar("T") class AsyncCacheProtocol(Protocol): """Protocol for cache backends used by EventDrivenCacheCoordinator.""" - async def async_get_cache(self, key: str, **kwargs: Any) -> Any: - ... + async def async_get_cache(self, key: str, **kwargs: Any) -> Any: ... - async def async_set_cache(self, key: str, value: Any, **kwargs: Any) -> Any: - ... + async def async_set_cache(self, key: str, value: Any, **kwargs: Any) -> Any: ... class EventDrivenCacheCoordinator: diff --git a/litellm/proxy/common_utils/callback_utils.py b/litellm/proxy/common_utils/callback_utils.py index 9ecae363ed7..a206be87a11 100644 --- a/litellm/proxy/common_utils/callback_utils.py +++ b/litellm/proxy/common_utils/callback_utils.py @@ -362,17 +362,17 @@ def get_remaining_tokens_and_requests_from_request_data(data: Dict) -> Dict[str, remaining_requests_variable_name = f"litellm-key-remaining-requests-{model_group}" remaining_requests = _metadata.get(remaining_requests_variable_name, None) if remaining_requests: - headers[ - f"x-litellm-key-remaining-requests-{h11_model_group_name}" - ] = remaining_requests + headers[f"x-litellm-key-remaining-requests-{h11_model_group_name}"] = ( + remaining_requests + ) # Remaining Tokens remaining_tokens_variable_name = f"litellm-key-remaining-tokens-{model_group}" remaining_tokens = _metadata.get(remaining_tokens_variable_name, None) if remaining_tokens: - headers[ - f"x-litellm-key-remaining-tokens-{h11_model_group_name}" - ] = remaining_tokens + headers[f"x-litellm-key-remaining-tokens-{h11_model_group_name}"] = ( + remaining_tokens + ) return headers @@ -472,9 +472,9 @@ def add_guardrail_response_to_standard_logging_object( ): if litellm_logging_obj is None: return - standard_logging_object: Optional[ - StandardLoggingPayload - ] = litellm_logging_obj.model_call_details.get("standard_logging_object") + standard_logging_object: Optional[StandardLoggingPayload] = ( + litellm_logging_obj.model_call_details.get("standard_logging_object") + ) if standard_logging_object is None: return guardrail_information = standard_logging_object.get("guardrail_information", []) diff --git a/litellm/proxy/common_utils/debug_utils.py b/litellm/proxy/common_utils/debug_utils.py index 6f7038377bd..99eeeda1c86 100644 --- a/litellm/proxy/common_utils/debug_utils.py +++ b/litellm/proxy/common_utils/debug_utils.py @@ -245,9 +245,9 @@ async def get_memory_summary( health_status = "healthy" except ImportError: - process_memory[ - "error" - ] = "Install psutil for memory monitoring: pip install psutil" + process_memory["error"] = ( + "Install psutil for memory monitoring: pip install psutil" + ) except Exception as e: process_memory["error"] = str(e) @@ -301,9 +301,9 @@ async def get_memory_summary( # Add warning if garbage collection issues detected if uncollectable > 0: - gc_info[ - "warning" - ] = f"{uncollectable} uncollectable objects (possible memory leak)" + gc_info["warning"] = ( + f"{uncollectable} uncollectable objects (possible memory leak)" + ) return { "worker_pid": os.getpid(), @@ -369,9 +369,11 @@ def _get_uncollectable_objects_info() -> Dict[str, Any]: return { "count": len(uncollectable), "sample_types": [type(obj).__name__ for obj in uncollectable[:10]], - "warning": "If count > 0, you may have reference cycles preventing garbage collection" - if len(uncollectable) > 0 - else None, + "warning": ( + "If count > 0, you may have reference cycles preventing garbage collection" + if len(uncollectable) > 0 + else None + ), } @@ -441,12 +443,16 @@ def _get_cache_memory_stats( if hasattr(redis_usage_cache.redis_client, "connection_pool"): pool_info = redis_usage_cache.redis_client.connection_pool # type: ignore cache_stats["redis_usage_cache"]["connection_pool"] = { - "max_connections": pool_info.max_connections - if hasattr(pool_info, "max_connections") - else None, - "connection_class": pool_info.connection_class.__name__ - if hasattr(pool_info, "connection_class") - else None, + "max_connections": ( + pool_info.max_connections + if hasattr(pool_info, "max_connections") + else None + ), + "connection_class": ( + pool_info.connection_class.__name__ + if hasattr(pool_info, "connection_class") + else None + ), } except Exception as e: verbose_proxy_logger.debug(f"Error getting Redis pool info: {e}") @@ -561,9 +567,11 @@ def _get_process_memory_info( "description": "Percentage of total system RAM being used", }, "open_file_handles": { - "count": process.num_fds() - if hasattr(process, "num_fds") - else "N/A (Windows)", + "count": ( + process.num_fds() + if hasattr(process, "num_fds") + else "N/A (Windows)" + ), "description": "Number of open file descriptors/handles", }, "threads": { diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py index 1dd25262127..88c3a6be9da 100644 --- a/litellm/proxy/common_utils/http_parsing_utils.py +++ b/litellm/proxy/common_utils/http_parsing_utils.py @@ -197,10 +197,10 @@ def check_file_size_under_limit( if llm_router is not None and request_data["model"] in router_model_names: try: - deployment: Optional[ - Deployment - ] = llm_router.get_deployment_by_model_group_name( - model_group_name=request_data["model"] + deployment: Optional[Deployment] = ( + llm_router.get_deployment_by_model_group_name( + model_group_name=request_data["model"] + ) ) if ( deployment diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py index 241b66bc0ae..8017448ae13 100644 --- a/litellm/proxy/db/db_spend_update_writer.py +++ b/litellm/proxy/db/db_spend_update_writer.py @@ -28,7 +28,10 @@ from typing import ( import litellm from litellm._logging import verbose_proxy_logger from litellm.caching import DualCache, RedisCache -from litellm.constants import DB_SPEND_UPDATE_JOB_NAME,DB_DAILY_TAG_SPEND_UPDATE_JOB_NAME +from litellm.constants import ( + DB_SPEND_UPDATE_JOB_NAME, + DB_DAILY_TAG_SPEND_UPDATE_JOB_NAME, +) from litellm.litellm_core_utils.safe_json_loads import safe_json_loads from litellm.proxy._types import ( DB_CONNECTION_ERROR_TYPES, @@ -1011,7 +1014,7 @@ class DBSpendUpdateWriter: proxy_logging_obj=proxy_logging_obj, daily_spend_transactions=daily_agent_spend_update_transactions, ) - + ################## Tool Registry Upserts ################## await self._flush_tool_discovery_queue(prisma_client=prisma_client) @@ -1059,7 +1062,9 @@ class DBSpendUpdateWriter: ): verbose_proxy_logger.debug("acquired lock for daily tag spend updates") try: - daily_tag_spend_update_transactions = await self.redis_update_buffer.get_all_daily_tag_spend_update_transactions_from_redis_buffer() + daily_tag_spend_update_transactions = ( + await self.redis_update_buffer.get_all_daily_tag_spend_update_transactions_from_redis_buffer() + ) if daily_tag_spend_update_transactions: await DBSpendUpdateWriter.update_daily_tag_spend( @@ -1659,14 +1664,14 @@ class DBSpendUpdateWriter: # Add cache-related fields if they exist if "cache_read_input_tokens" in transaction: - common_data[ - "cache_read_input_tokens" - ] = transaction.get("cache_read_input_tokens", 0) + common_data["cache_read_input_tokens"] = ( + transaction.get("cache_read_input_tokens", 0) + ) if "cache_creation_input_tokens" in transaction: - common_data[ - "cache_creation_input_tokens" - ] = transaction.get( - "cache_creation_input_tokens", 0 + common_data["cache_creation_input_tokens"] = ( + transaction.get( + "cache_creation_input_tokens", 0 + ) ) if entity_type == "tag" and "request_id" in transaction: diff --git a/litellm/proxy/db/db_transaction_queue/base_update_queue.py b/litellm/proxy/db/db_transaction_queue/base_update_queue.py index e37200c02e9..7f1a7474690 100644 --- a/litellm/proxy/db/db_transaction_queue/base_update_queue.py +++ b/litellm/proxy/db/db_transaction_queue/base_update_queue.py @@ -1,6 +1,7 @@ """ Base class for in memory buffer for database transactions """ + import asyncio from typing import Optional diff --git a/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py b/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py index 75e9b9580b6..f47b694d44e 100644 --- a/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py +++ b/litellm/proxy/db/db_transaction_queue/daily_spend_update_queue.py @@ -54,9 +54,9 @@ class DailySpendUpdateQueue(BaseUpdateQueue): def __init__(self): super().__init__() - self.update_queue: asyncio.Queue[ - Dict[str, BaseDailySpendTransaction] - ] = asyncio.Queue(maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE) + self.update_queue: asyncio.Queue[Dict[str, BaseDailySpendTransaction]] = ( + asyncio.Queue(maxsize=LITELLM_ASYNCIO_QUEUE_MAXSIZE) + ) async def add_update(self, update: Dict[str, BaseDailySpendTransaction]): """Enqueue an update.""" @@ -73,9 +73,9 @@ class DailySpendUpdateQueue(BaseUpdateQueue): Combine all updates in the queue into a single update. This is used to reduce the size of the in-memory queue. """ - updates: List[ - Dict[str, BaseDailySpendTransaction] - ] = await self.flush_all_updates_from_in_memory_queue() + updates: List[Dict[str, BaseDailySpendTransaction]] = ( + await self.flush_all_updates_from_in_memory_queue() + ) aggregated_updates = self.get_aggregated_daily_spend_update_transactions( updates ) diff --git a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py index bdca867081c..b8537c2be9e 100644 --- a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py +++ b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py @@ -71,9 +71,9 @@ class RedisUpdateBuffer: """ from litellm.proxy.proxy_server import general_settings - _use_redis_transaction_buffer: Optional[ - Union[bool, str] - ] = general_settings.get("use_redis_transaction_buffer", False) + _use_redis_transaction_buffer: Optional[Union[bool, str]] = ( + general_settings.get("use_redis_transaction_buffer", False) + ) if isinstance(_use_redis_transaction_buffer, str): _use_redis_transaction_buffer = str_to_bool(_use_redis_transaction_buffer) if _use_redis_transaction_buffer is None: diff --git a/litellm/proxy/db/db_transaction_queue/spend_update_queue.py b/litellm/proxy/db/db_transaction_queue/spend_update_queue.py index 727e8dc1d5a..8100a1e8a12 100644 --- a/litellm/proxy/db/db_transaction_queue/spend_update_queue.py +++ b/litellm/proxy/db/db_transaction_queue/spend_update_queue.py @@ -53,9 +53,9 @@ class SpendUpdateQueue(BaseUpdateQueue): async def aggregate_queue_updates(self): """Concatenate all updates in the queue to reduce the size of in-memory queue""" - updates: List[ - SpendUpdateQueueItem - ] = await self.flush_all_updates_from_in_memory_queue() + updates: List[SpendUpdateQueueItem] = ( + await self.flush_all_updates_from_in_memory_queue() + ) aggregated_updates = self._get_aggregated_spend_update_queue_item(updates) for update in aggregated_updates: await self.update_queue.put(update) diff --git a/litellm/proxy/fine_tuning_endpoints/endpoints.py b/litellm/proxy/fine_tuning_endpoints/endpoints.py index ff6300a4fa0..17a7c09321a 100644 --- a/litellm/proxy/fine_tuning_endpoints/endpoints.py +++ b/litellm/proxy/fine_tuning_endpoints/endpoints.py @@ -306,9 +306,9 @@ async def retrieve_fine_tuning_job( **data, ), ) - response._hidden_params[ - "unified_finetuning_job_id" - ] = unified_finetuning_job_id + response._hidden_params["unified_finetuning_job_id"] = ( + unified_finetuning_job_id + ) elif custom_llm_provider: # get configs for custom_llm_provider llm_provider_config = get_fine_tuning_provider_config( @@ -595,9 +595,9 @@ async def cancel_fine_tuning_job( **data, ), ) - response._hidden_params[ - "unified_finetuning_job_id" - ] = unified_finetuning_job_id + response._hidden_params["unified_finetuning_job_id"] = ( + unified_finetuning_job_id + ) else: # get configs for custom_llm_provider llm_provider_config = get_fine_tuning_provider_config( diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index 422bdc13780..f126b38afe0 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -567,7 +567,9 @@ class GuardrailSubmissionItem(BaseModel): guardrail_name: str status: str # pending_review | active | rejected team_id: Optional[str] = None - team_guardrail: bool = False # True when submitted via team (team_id set); use to distinguish team vs regular guardrails + team_guardrail: bool = ( + False # True when submitted via team (team_id set); use to distinguish team vs regular guardrails + ) litellm_params: Optional[Dict[str, Any]] = None guardrail_info: Optional[Dict[str, Any]] = None submitted_by_user_id: Optional[str] = None @@ -674,9 +676,9 @@ async def register_guardrail( guardrail_info = dict(request.guardrail_info or {}) guardrail_info["submitted_by_user_id"] = user_api_key_dict.user_id guardrail_info["submitted_by_email"] = user_api_key_dict.user_email - guardrail_info[ - "team_guardrail" - ] = True # Mark as team submission for filtering/display + guardrail_info["team_guardrail"] = ( + True # Mark as team submission for filtering/display + ) guardrail_info_str = safe_dumps(guardrail_info) try: @@ -1871,9 +1873,9 @@ async def get_provider_specific_params(): lakera_v2_fields = _get_fields_from_model(LakeraV2GuardrailConfigModel) tool_permission_fields = _get_fields_from_model(ToolPermissionGuardrailConfigModel) - tool_permission_fields[ - "ui_friendly_name" - ] = ToolPermissionGuardrailConfigModel.ui_friendly_name() + tool_permission_fields["ui_friendly_name"] = ( + ToolPermissionGuardrailConfigModel.ui_friendly_name() + ) # Return the provider-specific parameters provider_params = { @@ -2146,10 +2148,10 @@ async def apply_guardrail( from litellm.proxy.utils import handle_exception_on_proxy try: - active_guardrail: Optional[ - CustomGuardrail - ] = GUARDRAIL_REGISTRY.get_initialized_guardrail_callback( - guardrail_name=request.guardrail_name + active_guardrail: Optional[CustomGuardrail] = ( + GUARDRAIL_REGISTRY.get_initialized_guardrail_callback( + guardrail_name=request.guardrail_name + ) ) if active_guardrail is None: raise HTTPException( diff --git a/litellm/proxy/guardrails/guardrail_hooks/akto/akto.py b/litellm/proxy/guardrails/guardrail_hooks/akto/akto.py index 5058ee348db..ece311666c5 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/akto/akto.py +++ b/litellm/proxy/guardrails/guardrail_hooks/akto/akto.py @@ -91,9 +91,9 @@ class AktoGuardrail(CustomGuardrail): "akto_api_key is required. Set AKTO_API_KEY or pass it in litellm_params." ) - self.unreachable_fallback: Literal[ - "fail_closed", "fail_open" - ] = unreachable_fallback + self.unreachable_fallback: Literal["fail_closed", "fail_open"] = ( + unreachable_fallback + ) self.guardrail_timeout = guardrail_timeout or DEFAULT_GUARDRAIL_TIMEOUT self.akto_account_id = akto_account_id or os.environ.get( "AKTO_ACCOUNT_ID", "1000000" diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index 8ef188bb23c..2772446a2b3 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -796,9 +796,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): ######################################################### ########## 1. Make the Bedrock API request ########## ######################################################### - bedrock_guardrail_response: Optional[ - Union[BedrockGuardrailResponse, str] - ] = None + bedrock_guardrail_response: Optional[Union[BedrockGuardrailResponse, str]] = ( + None + ) try: bedrock_guardrail_response = await self.make_bedrock_api_request( source="INPUT", messages=filtered_messages, request_data=data @@ -868,9 +868,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): ######################################################### ########## 1. Make the Bedrock API request ########## ######################################################### - bedrock_guardrail_response: Optional[ - Union[BedrockGuardrailResponse, str] - ] = None + bedrock_guardrail_response: Optional[Union[BedrockGuardrailResponse, str]] = ( + None + ) try: bedrock_guardrail_response = await self.make_bedrock_api_request( source="INPUT", messages=filtered_messages, request_data=data diff --git a/litellm/proxy/guardrails/guardrail_hooks/block_code_execution/block_code_execution.py b/litellm/proxy/guardrails/guardrail_hooks/block_code_execution/block_code_execution.py index efd781681a8..49c3dc00cd3 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/block_code_execution/block_code_execution.py +++ b/litellm/proxy/guardrails/guardrail_hooks/block_code_execution/block_code_execution.py @@ -347,9 +347,9 @@ class BlockCodeExecutionGuardrail(CustomGuardrail): **kwargs: Any, ) -> None: # Normalize to type expected by CustomGuardrail - _event_hook: Optional[ - Union[GuardrailEventHooks, List[GuardrailEventHooks]] - ] = None + _event_hook: Optional[Union[GuardrailEventHooks, List[GuardrailEventHooks]]] = ( + None + ) if event_hook is not None: if isinstance(event_hook, list): _event_hook = [ diff --git a/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py b/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py index 18720845085..790ee31f2e0 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py +++ b/litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py @@ -219,9 +219,9 @@ class GenericGuardrailAPI(CustomGuardrail): additional_provider_specific_params or {} ) - self.unreachable_fallback: Literal[ - "fail_closed", "fail_open" - ] = unreachable_fallback + self.unreachable_fallback: Literal["fail_closed", "fail_open"] = ( + unreachable_fallback + ) # Set supported event hooks if "supported_event_hooks" not in kwargs: diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py index 28f0d830f12..ff802223f21 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai.py @@ -150,9 +150,9 @@ class lakeraAI_Moderation(CustomGuardrail): text = "" _json_data: str = "" if "messages" in data and isinstance(data["messages"], list): - prompt_injection_obj: Optional[ - GuardrailItem - ] = litellm.guardrail_name_config_map.get("prompt_injection") + prompt_injection_obj: Optional[GuardrailItem] = ( + litellm.guardrail_name_config_map.get("prompt_injection") + ) if prompt_injection_obj is not None: enabled_roles = prompt_injection_obj.enabled_roles else: diff --git a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py index 6b917bc794c..7aa26435ba6 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py +++ b/litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py @@ -393,9 +393,9 @@ class LakeraAIGuardrail(CustomGuardrail): for idx, msg in enumerate(assistant_messages): if idx < len(choice_indices): choice_idx = choice_indices[idx] - response_dict["choices"][choice_idx]["message"][ - "content" - ] = msg.get("content", "") + response_dict["choices"][choice_idx]["message"]["content"] = ( + msg.get("content", "") + ) add_guardrail_to_applied_guardrails_header( request_data=data, guardrail_name=self.guardrail_name ) diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/competitor_intent/airline.py b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/competitor_intent/airline.py index 90b45262c4c..9ab5b9c1d5b 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/competitor_intent/airline.py +++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/competitor_intent/airline.py @@ -159,9 +159,9 @@ class AirlineCompetitorIntentChecker(BaseCompetitorIntentChecker): if not merged.get("explicit_competitor_marker"): merged["explicit_competitor_marker"] = AIRLINE_EXPLICIT_COMPETITOR_MARKER if not merged.get("explicit_other_meaning_marker"): - merged[ - "explicit_other_meaning_marker" - ] = AIRLINE_EXPLICIT_OTHER_MEANING_MARKER + merged["explicit_other_meaning_marker"] = ( + AIRLINE_EXPLICIT_OTHER_MEANING_MARKER + ) if not merged.get("domain_words"): merged["domain_words"] = ["airline", "airlines", "carrier"] if not merged.get("competitors"): diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py index e4da1c1ae77..10b17a09ef9 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py +++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py @@ -212,17 +212,17 @@ class ContentFilterGuardrail(CustomGuardrail): self.image_model = image_model # Store loaded categories self.loaded_categories: Dict[str, CategoryConfig] = {} - self.category_keywords: Dict[ - str, Tuple[str, str, ContentFilterAction] - ] = {} # keyword -> (category, severity, action) + self.category_keywords: Dict[str, Tuple[str, str, ContentFilterAction]] = ( + {} + ) # keyword -> (category, severity, action) # Always-block keywords are checked after exceptions (exceptions take precedence) self.always_block_category_keywords: Dict[ str, Tuple[str, str, ContentFilterAction] ] = {} # Store conditional categories (identifier_words + block_words) - self.conditional_categories: Dict[ - str, Dict[str, Any] - ] = {} # category_name -> {identifier_words, block_words, action, severity} + self.conditional_categories: Dict[str, Dict[str, Any]] = ( + {} + ) # category_name -> {identifier_words, block_words, action, severity} # Competitor intent checker (optional; airline uses major_airlines.json, generic requires competitors) self._competitor_intent_checker: Optional[BaseCompetitorIntentChecker] = None diff --git a/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py b/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py index 3250e0bb7cf..d00e77daa07 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py +++ b/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py @@ -297,9 +297,7 @@ class ModelArmorGuardrail(CustomGuardrail, VertexBase): filters = ( list(filter_results.values()) if isinstance(filter_results, dict) - else filter_results - if isinstance(filter_results, list) - else [] + else filter_results if isinstance(filter_results, list) else [] ) # Prefer sanitized text from deidentifyResult if present @@ -753,7 +751,9 @@ class ModelArmorGuardrail(CustomGuardrail, VertexBase): # returns a proper JSON error response with the correct status code. # (Raising from a generator hits create_response's generic except → 500.) detail = ( - e.detail if isinstance(e.detail, dict) else {"message": str(e.detail)} + e.detail + if isinstance(e.detail, dict) + else {"message": str(e.detail)} ) error_value = detail.get("error", detail) if isinstance(error_value, dict): diff --git a/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py b/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py index 2545693b937..bbffc70ddbf 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py +++ b/litellm/proxy/guardrails/guardrail_hooks/panw_prisma_airs/panw_prisma_airs.py @@ -295,10 +295,10 @@ class PanwPrismaAirsHandler(CustomGuardrail): panw_metadata = { "app_user": ( - metadata.get("app_user") or metadata.get("user") or "litellm_user" - ) - if metadata - else "litellm_user", + (metadata.get("app_user") or metadata.get("user") or "litellm_user") + if metadata + else "litellm_user" + ), "ai_model": metadata.get("model", "unknown") if metadata else "unknown", "app_name": app_name_value, "source": "litellm_builtin_guardrail", @@ -1088,9 +1088,11 @@ class PanwPrismaAirsHandler(CustomGuardrail): guardrail_provider=self._PROVIDER_NAME, guardrail_json_response=scan_result, request_data=data, - guardrail_status="success" - if scan_result.get("action") == "allow" - else "guardrail_intervened", + guardrail_status=( + "success" + if scan_result.get("action") == "allow" + else "guardrail_intervened" + ), start_time=start_time.timestamp(), end_time=end_time.timestamp(), duration=(end_time - start_time).total_seconds(), @@ -1226,9 +1228,11 @@ class PanwPrismaAirsHandler(CustomGuardrail): guardrail_provider=self._PROVIDER_NAME, guardrail_json_response=scan_result, request_data=data, - guardrail_status="success" - if scan_result.get("action") == "allow" - else "guardrail_intervened", + guardrail_status=( + "success" + if scan_result.get("action") == "allow" + else "guardrail_intervened" + ), start_time=start_time.timestamp(), end_time=end_time.timestamp(), duration=(end_time - start_time).total_seconds(), @@ -1449,9 +1453,11 @@ class PanwPrismaAirsHandler(CustomGuardrail): guardrail_provider=self._PROVIDER_NAME, guardrail_json_response=scan_result, request_data=request_data, - guardrail_status="success" - if scan_result.get("action") == "allow" - else "guardrail_intervened", + guardrail_status=( + "success" + if scan_result.get("action") == "allow" + else "guardrail_intervened" + ), start_time=start_time.timestamp(), end_time=end_time.timestamp(), duration=(end_time - start_time).total_seconds(), diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index 0f4ebbd4880..41348597e6f 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -729,9 +729,9 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): if messages is None: return data tasks = [] - task_mappings: List[ - Tuple[int, Optional[int]] - ] = [] # Track (message_index, content_index) for each task + task_mappings: List[Tuple[int, Optional[int]]] = ( + [] + ) # Track (message_index, content_index) for each task for msg_idx, m in enumerate(messages): content = m.get("content", None) @@ -832,9 +832,9 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): ): # /chat/completions requests messages: Optional[List] = kwargs.get("messages", None) tasks = [] - task_mappings: List[ - Tuple[int, Optional[int]] - ] = [] # Track (message_index, content_index) for each task + task_mappings: List[Tuple[int, Optional[int]]] = ( + [] + ) # Track (message_index, content_index) for each task if messages is None: return kwargs, result diff --git a/litellm/proxy/guardrails/tool_name_extraction.py b/litellm/proxy/guardrails/tool_name_extraction.py index c554c4fc9ac..fb1c0d72ee7 100644 --- a/litellm/proxy/guardrails/tool_name_extraction.py +++ b/litellm/proxy/guardrails/tool_name_extraction.py @@ -40,12 +40,12 @@ def _extract_mcp_tool_names(data: dict) -> List[str]: def _register_standalone_extractors() -> None: if STANDALONE_EXTRACTORS: return - STANDALONE_EXTRACTORS[ - CallTypes.generate_content.value - ] = _extract_generate_content_tool_names - STANDALONE_EXTRACTORS[ - CallTypes.agenerate_content.value - ] = _extract_generate_content_tool_names + STANDALONE_EXTRACTORS[CallTypes.generate_content.value] = ( + _extract_generate_content_tool_names + ) + STANDALONE_EXTRACTORS[CallTypes.agenerate_content.value] = ( + _extract_generate_content_tool_names + ) STANDALONE_EXTRACTORS[CallTypes.call_mcp_tool.value] = _extract_mcp_tool_names diff --git a/litellm/proxy/health_check.py b/litellm/proxy/health_check.py index 5d1bcf31f84..e5bd1f2e2a9 100644 --- a/litellm/proxy/health_check.py +++ b/litellm/proxy/health_check.py @@ -242,7 +242,9 @@ async def _perform_health_check( cleaned["model_id"] = _model_id if isinstance(is_healthy, Exception): exceptions_by_model_id[_model_id] = is_healthy - cleaned["exception_status"] = getattr(is_healthy, "status_code", 500) + cleaned["exception_status"] = getattr( + is_healthy, "status_code", 500 + ) unhealthy_endpoints.append(cleaned) return healthy_endpoints, unhealthy_endpoints, exceptions_by_model_id diff --git a/litellm/proxy/hooks/batch_rate_limiter.py b/litellm/proxy/hooks/batch_rate_limiter.py index ba8a4672cae..06b7d857896 100644 --- a/litellm/proxy/hooks/batch_rate_limiter.py +++ b/litellm/proxy/hooks/batch_rate_limiter.py @@ -194,9 +194,7 @@ class _PROXY_BatchRateLimiter(CustomLogger): required_capacity = ( batch_usage.request_count if rate_limit_type == "requests" - else batch_usage.total_tokens - if rate_limit_type == "tokens" - else 0 + else batch_usage.total_tokens if rate_limit_type == "tokens" else 0 ) if required_capacity > limit_remaining: diff --git a/litellm/proxy/hooks/dynamic_rate_limiter.py b/litellm/proxy/hooks/dynamic_rate_limiter.py index 14fde51210d..f1c1d487cc1 100644 --- a/litellm/proxy/hooks/dynamic_rate_limiter.py +++ b/litellm/proxy/hooks/dynamic_rate_limiter.py @@ -103,9 +103,9 @@ class _PROXY_DynamicRateLimitHandler(CustomLogger): """ try: # Get model info first for conversion - model_group_info: Optional[ - ModelGroupInfo - ] = self.llm_router.get_model_group_info(model_group=model) + model_group_info: Optional[ModelGroupInfo] = ( + self.llm_router.get_model_group_info(model_group=model) + ) weight: float = 1 if ( @@ -277,16 +277,16 @@ class _PROXY_DynamicRateLimitHandler(CustomLogger): ) = await self.check_available_usage( model=model_info["model_name"], priority=key_priority ) - response._hidden_params[ - "additional_headers" - ] = { # Add additional response headers - easier debugging - "x-litellm-model_group": model_info["model_name"], - "x-ratelimit-remaining-litellm-project-tokens": available_tpm, - "x-ratelimit-remaining-litellm-project-requests": available_rpm, - "x-ratelimit-remaining-model-tokens": model_tpm, - "x-ratelimit-remaining-model-requests": model_rpm, - "x-ratelimit-current-active-projects": active_projects, - } + response._hidden_params["additional_headers"] = ( + { # Add additional response headers - easier debugging + "x-litellm-model_group": model_info["model_name"], + "x-ratelimit-remaining-litellm-project-tokens": available_tpm, + "x-ratelimit-remaining-litellm-project-requests": available_rpm, + "x-ratelimit-remaining-model-tokens": model_tpm, + "x-ratelimit-remaining-model-requests": model_rpm, + "x-ratelimit-current-active-projects": active_projects, + } + ) return response return await super().async_post_call_success_hook( diff --git a/litellm/proxy/hooks/dynamic_rate_limiter_v3.py b/litellm/proxy/hooks/dynamic_rate_limiter_v3.py index 5a1d6bec5d5..72483d29cdc 100644 --- a/litellm/proxy/hooks/dynamic_rate_limiter_v3.py +++ b/litellm/proxy/hooks/dynamic_rate_limiter_v3.py @@ -322,9 +322,9 @@ class _PROXY_DynamicRateLimitHandlerV3(CustomLogger): return descriptors # Get model group info - model_group_info: Optional[ - ModelGroupInfo - ] = self.llm_router.get_model_group_info(model_group=model) + model_group_info: Optional[ModelGroupInfo] = ( + self.llm_router.get_model_group_info(model_group=model) + ) if model_group_info is None: return descriptors @@ -597,9 +597,9 @@ class _PROXY_DynamicRateLimitHandlerV3(CustomLogger): ) # Get model configuration - model_group_info: Optional[ - ModelGroupInfo - ] = self.llm_router.get_model_group_info(model_group=model) + model_group_info: Optional[ModelGroupInfo] = ( + self.llm_router.get_model_group_info(model_group=model) + ) if model_group_info is None: verbose_proxy_logger.debug( f"No model group info for {model}, allowing request" diff --git a/litellm/proxy/hooks/key_management_event_hooks.py b/litellm/proxy/hooks/key_management_event_hooks.py index 2d61203ad51..5cdd9ddb4bd 100644 --- a/litellm/proxy/hooks/key_management_event_hooks.py +++ b/litellm/proxy/hooks/key_management_event_hooks.py @@ -364,10 +364,10 @@ class KeyManagementEventHooks: if key.key_alias is not None: team_id = getattr(key, "team_id", None) if team_id not in team_settings_cache: - team_settings_cache[ - team_id - ] = await KeyManagementEventHooks._get_secret_manager_optional_params( - team_id + team_settings_cache[team_id] = ( + await KeyManagementEventHooks._get_secret_manager_optional_params( + team_id + ) ) optional_params = team_settings_cache[team_id] await litellm.secret_manager_client.async_delete_secret( diff --git a/litellm/proxy/hooks/litellm_skills/main.py b/litellm/proxy/hooks/litellm_skills/main.py index 83e419bc23c..7c6bfbd6b2b 100644 --- a/litellm/proxy/hooks/litellm_skills/main.py +++ b/litellm/proxy/hooks/litellm_skills/main.py @@ -439,9 +439,11 @@ class SkillsInjectionHook(CustomLogger): { "id": tc.id, "name": tc.function.name, - "input": json.loads(tc.function.arguments) - if tc.function.arguments - else {}, + "input": ( + json.loads(tc.function.arguments) + if tc.function.arguments + else {} + ), } ) diff --git a/litellm/proxy/hooks/mcp_semantic_filter/__init__.py b/litellm/proxy/hooks/mcp_semantic_filter/__init__.py index 36d357d560f..c9ef11c8b0a 100644 --- a/litellm/proxy/hooks/mcp_semantic_filter/__init__.py +++ b/litellm/proxy/hooks/mcp_semantic_filter/__init__.py @@ -4,6 +4,7 @@ MCP Semantic Tool Filter Hook Semantic filtering for MCP tools to reduce context window size and improve tool selection accuracy. """ + from litellm.proxy.hooks.mcp_semantic_filter.hook import SemanticToolFilterHook __all__ = ["SemanticToolFilterHook"] diff --git a/litellm/proxy/hooks/mcp_semantic_filter/hook.py b/litellm/proxy/hooks/mcp_semantic_filter/hook.py index 4075641d63b..6343faaa965 100644 --- a/litellm/proxy/hooks/mcp_semantic_filter/hook.py +++ b/litellm/proxy/hooks/mcp_semantic_filter/hook.py @@ -4,6 +4,7 @@ Semantic Tool Filter Hook Pre-call hook that filters MCP tools semantically before LLM inference. Reduces context window size and improves tool selection accuracy. """ + from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union from litellm._logging import verbose_proxy_logger diff --git a/litellm/proxy/hooks/parallel_request_limiter.py b/litellm/proxy/hooks/parallel_request_limiter.py index b26d8336191..43c5fc68723 100644 --- a/litellm/proxy/hooks/parallel_request_limiter.py +++ b/litellm/proxy/hooks/parallel_request_limiter.py @@ -202,9 +202,7 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger): if rpm_limit is None: rpm_limit = sys.maxsize - values_to_update_in_cache: List[ - Tuple[Any, Any] - ] = ( + values_to_update_in_cache: List[Tuple[Any, Any]] = ( [] ) # values that need to get updated in cache, will run a batch_set_cache after this function @@ -703,9 +701,9 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger): async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time): try: self.print_verbose("Inside Max Parallel Request Failure Hook") - litellm_parent_otel_span: Union[ - Span, None - ] = _get_parent_otel_span_from_kwargs(kwargs=kwargs) + litellm_parent_otel_span: Union[Span, None] = ( + _get_parent_otel_span_from_kwargs(kwargs=kwargs) + ) _metadata = kwargs["litellm_params"].get("metadata", {}) or {} global_max_parallel_requests = _metadata.get( "global_max_parallel_requests", None @@ -832,11 +830,11 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger): current_minute = datetime.now().strftime("%M") precise_minute = f"{current_date}-{current_hour}-{current_minute}" request_count_api_key = f"{api_key}::{precise_minute}::request_count" - current: Optional[ - CurrentItemRateLimit - ] = await self.internal_usage_cache.async_get_cache( - key=request_count_api_key, - litellm_parent_otel_span=user_api_key_dict.parent_otel_span, + current: Optional[CurrentItemRateLimit] = ( + await self.internal_usage_cache.async_get_cache( + key=request_count_api_key, + litellm_parent_otel_span=user_api_key_dict.parent_otel_span, + ) ) key_remaining_rpm_limit: Optional[int] = None @@ -868,15 +866,15 @@ class _PROXY_MaxParallelRequestsHandler(CustomLogger): _additional_headers = _hidden_params.get("additional_headers", {}) or {} if key_remaining_rpm_limit is not None: - _additional_headers[ - "x-ratelimit-remaining-requests" - ] = key_remaining_rpm_limit + _additional_headers["x-ratelimit-remaining-requests"] = ( + key_remaining_rpm_limit + ) if key_rpm_limit is not None: _additional_headers["x-ratelimit-limit-requests"] = key_rpm_limit if key_remaining_tpm_limit is not None: - _additional_headers[ - "x-ratelimit-remaining-tokens" - ] = key_remaining_tpm_limit + _additional_headers["x-ratelimit-remaining-tokens"] = ( + key_remaining_tpm_limit + ) if key_tpm_limit is not None: _additional_headers["x-ratelimit-limit-tokens"] = key_tpm_limit diff --git a/litellm/proxy/hooks/parallel_request_limiter_v3.py b/litellm/proxy/hooks/parallel_request_limiter_v3.py index 5aaac088dc2..bbc268a0c4f 100644 --- a/litellm/proxy/hooks/parallel_request_limiter_v3.py +++ b/litellm/proxy/hooks/parallel_request_limiter_v3.py @@ -1682,9 +1682,9 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger): from litellm.types.caching import RedisPipelineIncrementOperation try: - litellm_parent_otel_span: Union[ - Span, None - ] = _get_parent_otel_span_from_kwargs(kwargs) + litellm_parent_otel_span: Union[Span, None] = ( + _get_parent_otel_span_from_kwargs(kwargs) + ) # Get metadata from standard_logging_object - this correctly handles both # 'metadata' and 'litellm_metadata' fields from litellm_params standard_logging_object = kwargs.get("standard_logging_object") or {} diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index 46000f4fe6e..ea9c92fec6c 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -55,11 +55,11 @@ class _ProxyDBLogger(CustomLogger): ) _metadata["user_api_key"] = user_api_key_dict.api_key _metadata["status"] = "failure" - _metadata[ - "error_information" - ] = StandardLoggingPayloadSetup.get_error_information( - original_exception=original_exception, - traceback_str=traceback_str, + _metadata["error_information"] = ( + StandardLoggingPayloadSetup.get_error_information( + original_exception=original_exception, + traceback_str=traceback_str, + ) ) _metadata = await _ProxyDBLogger._enrich_failure_metadata_with_key_info( diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py index 3ed96c163af..9a51efffb40 100644 --- a/litellm/proxy/litellm_pre_call_utils.py +++ b/litellm/proxy/litellm_pre_call_utils.py @@ -42,6 +42,8 @@ def _sanitize_for_log(value: Any) -> str: text = repr(value) # Strip CR/LF characters commonly used for log injection return text.replace("\r", "").replace("\n", "") + + from litellm.router import Router from litellm.secret_managers.main import get_secret_bool from litellm.types.llms.anthropic import ANTHROPIC_API_HEADERS @@ -219,12 +221,12 @@ def _get_dynamic_logging_metadata( user_api_key_dict: UserAPIKeyAuth, proxy_config: ProxyConfig ) -> Optional[TeamCallbackMetadata]: callback_settings_obj: Optional[TeamCallbackMetadata] = None - key_dynamic_logging_settings: Optional[ - dict - ] = KeyAndTeamLoggingSettings.get_key_dynamic_logging_settings(user_api_key_dict) - team_dynamic_logging_settings: Optional[ - dict - ] = KeyAndTeamLoggingSettings.get_team_dynamic_logging_settings(user_api_key_dict) + key_dynamic_logging_settings: Optional[dict] = ( + KeyAndTeamLoggingSettings.get_key_dynamic_logging_settings(user_api_key_dict) + ) + team_dynamic_logging_settings: Optional[dict] = ( + KeyAndTeamLoggingSettings.get_team_dynamic_logging_settings(user_api_key_dict) + ) ######################################################################################### # Key-based callbacks ######################################################################################### @@ -777,11 +779,11 @@ class LiteLLMProxyRequestSetup: ## KEY-LEVEL SPEND LOGS / TAGS if "tags" in key_metadata and key_metadata["tags"] is not None: - data[_metadata_variable_name][ - "tags" - ] = LiteLLMProxyRequestSetup._merge_tags( - request_tags=data[_metadata_variable_name].get("tags"), - tags_to_add=key_metadata["tags"], + data[_metadata_variable_name]["tags"] = ( + LiteLLMProxyRequestSetup._merge_tags( + request_tags=data[_metadata_variable_name].get("tags"), + tags_to_add=key_metadata["tags"], + ) ) if "disable_global_guardrails" in key_metadata and isinstance( key_metadata["disable_global_guardrails"], bool @@ -1077,9 +1079,9 @@ async def add_litellm_data_to_request( # noqa: PLR0915 data[_metadata_variable_name]["litellm_api_version"] = version if general_settings is not None: - data[_metadata_variable_name][ - "global_max_parallel_requests" - ] = general_settings.get("global_max_parallel_requests", None) + data[_metadata_variable_name]["global_max_parallel_requests"] = ( + general_settings.get("global_max_parallel_requests", None) + ) ### KEY-LEVEL Controls key_metadata = user_api_key_dict.metadata @@ -1693,7 +1695,9 @@ async def move_guardrails_to_metadata( ) # Only check policy engine if no local config (avoid import + registry lookup) - if not (has_key_config or has_team_config or has_project_config or has_request_config): + if not ( + has_key_config or has_team_config or has_project_config or has_request_config + ): from litellm.proxy.policy_engine.policy_registry import get_policy_registry if not get_policy_registry().is_initialized(): diff --git a/litellm/proxy/management_endpoints/callback_management_endpoints.py b/litellm/proxy/management_endpoints/callback_management_endpoints.py index 9132d3fe1d7..f9781f3634c 100644 --- a/litellm/proxy/management_endpoints/callback_management_endpoints.py +++ b/litellm/proxy/management_endpoints/callback_management_endpoints.py @@ -1,6 +1,7 @@ """ Endpoints for managing callbacks """ + import json import os diff --git a/litellm/proxy/management_endpoints/common_daily_activity.py b/litellm/proxy/management_endpoints/common_daily_activity.py index 011d2f7485d..ac66adc26f3 100644 --- a/litellm/proxy/management_endpoints/common_daily_activity.py +++ b/litellm/proxy/management_endpoints/common_daily_activity.py @@ -105,24 +105,26 @@ def update_breakdown_metrics( # Update API key breakdown for this model if record.api_key not in breakdown.models[record.model].api_key_breakdown: - breakdown.models[record.model].api_key_breakdown[ - record.api_key - ] = KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=KeyMetadata( - key_alias=api_key_metadata.get(record.api_key, {}).get( - "key_alias", None + breakdown.models[record.model].api_key_breakdown[record.api_key] = ( + KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=KeyMetadata( + key_alias=api_key_metadata.get(record.api_key, {}).get( + "key_alias", None + ), + team_id=api_key_metadata.get(record.api_key, {}).get( + "team_id", None + ), ), - team_id=api_key_metadata.get(record.api_key, {}).get( - "team_id", None - ), - ), + ) + ) + breakdown.models[record.model].api_key_breakdown[record.api_key].metrics = ( + update_metrics( + breakdown.models[record.model] + .api_key_breakdown[record.api_key] + .metrics, + record, ) - breakdown.models[record.model].api_key_breakdown[ - record.api_key - ].metrics = update_metrics( - breakdown.models[record.model].api_key_breakdown[record.api_key].metrics, - record, ) # Update model group breakdown @@ -218,22 +220,24 @@ def update_breakdown_metrics( # Update API key breakdown for this provider if record.api_key not in breakdown.providers[provider].api_key_breakdown: - breakdown.providers[provider].api_key_breakdown[ - record.api_key - ] = KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=KeyMetadata( - key_alias=api_key_metadata.get(record.api_key, {}).get( - "key_alias", None + breakdown.providers[provider].api_key_breakdown[record.api_key] = ( + KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=KeyMetadata( + key_alias=api_key_metadata.get(record.api_key, {}).get( + "key_alias", None + ), + team_id=api_key_metadata.get(record.api_key, {}).get( + "team_id", None + ), ), - team_id=api_key_metadata.get(record.api_key, {}).get("team_id", None), - ), + ) + ) + breakdown.providers[provider].api_key_breakdown[record.api_key].metrics = ( + update_metrics( + breakdown.providers[provider].api_key_breakdown[record.api_key].metrics, + record, ) - breakdown.providers[provider].api_key_breakdown[ - record.api_key - ].metrics = update_metrics( - breakdown.providers[provider].api_key_breakdown[record.api_key].metrics, - record, ) # Update endpoint breakdown @@ -249,18 +253,18 @@ def update_breakdown_metrics( # Update API key breakdown for this endpoint if record.api_key not in breakdown.endpoints[record.endpoint].api_key_breakdown: - breakdown.endpoints[record.endpoint].api_key_breakdown[ - record.api_key - ] = KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=KeyMetadata( - key_alias=api_key_metadata.get(record.api_key, {}).get( - "key_alias", None + breakdown.endpoints[record.endpoint].api_key_breakdown[record.api_key] = ( + KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=KeyMetadata( + key_alias=api_key_metadata.get(record.api_key, {}).get( + "key_alias", None + ), + team_id=api_key_metadata.get(record.api_key, {}).get( + "team_id", None + ), ), - team_id=api_key_metadata.get(record.api_key, {}).get( - "team_id", None - ), - ), + ) ) breakdown.endpoints[record.endpoint].api_key_breakdown[ record.api_key @@ -307,24 +311,26 @@ def update_breakdown_metrics( # Update API key breakdown for this entity if record.api_key not in breakdown.entities[entity_value].api_key_breakdown: - breakdown.entities[entity_value].api_key_breakdown[ - record.api_key - ] = KeyMetricWithMetadata( - metrics=SpendMetrics(), - metadata=KeyMetadata( - key_alias=api_key_metadata.get(record.api_key, {}).get( - "key_alias", None + breakdown.entities[entity_value].api_key_breakdown[record.api_key] = ( + KeyMetricWithMetadata( + metrics=SpendMetrics(), + metadata=KeyMetadata( + key_alias=api_key_metadata.get(record.api_key, {}).get( + "key_alias", None + ), + team_id=api_key_metadata.get(record.api_key, {}).get( + "team_id", None + ), ), - team_id=api_key_metadata.get(record.api_key, {}).get( - "team_id", None - ), - ), + ) + ) + breakdown.entities[entity_value].api_key_breakdown[record.api_key].metrics = ( + update_metrics( + breakdown.entities[entity_value] + .api_key_breakdown[record.api_key] + .metrics, + record, ) - breakdown.entities[entity_value].api_key_breakdown[ - record.api_key - ].metrics = update_metrics( - breakdown.entities[entity_value].api_key_breakdown[record.api_key].metrics, - record, ) return breakdown diff --git a/litellm/proxy/management_endpoints/cost_tracking_settings.py b/litellm/proxy/management_endpoints/cost_tracking_settings.py index bf24d8924de..b5ae8f93be6 100644 --- a/litellm/proxy/management_endpoints/cost_tracking_settings.py +++ b/litellm/proxy/management_endpoints/cost_tracking_settings.py @@ -69,9 +69,11 @@ def _resolve_model_for_cost_lookup(model: str) -> Tuple[str, Optional[str]]: custom_llm_provider = litellm_params.get("custom_llm_provider") return ( str(base_model), - str(custom_llm_provider) - if custom_llm_provider is not None - else None, + ( + str(custom_llm_provider) + if custom_llm_provider is not None + else None + ), ) resolved_model = litellm_params.get("model") @@ -83,9 +85,11 @@ def _resolve_model_for_cost_lookup(model: str) -> Tuple[str, Optional[str]]: custom_llm_provider = litellm_params.get("custom_llm_provider") return ( str(resolved_model), - str(custom_llm_provider) - if custom_llm_provider is not None - else None, + ( + str(custom_llm_provider) + if custom_llm_provider is not None + else None + ), ) except Exception as e: verbose_proxy_logger.debug( diff --git a/litellm/proxy/management_endpoints/customer_endpoints.py b/litellm/proxy/management_endpoints/customer_endpoints.py index 084c2f47d0f..4889f0b7f80 100644 --- a/litellm/proxy/management_endpoints/customer_endpoints.py +++ b/litellm/proxy/management_endpoints/customer_endpoints.py @@ -626,9 +626,9 @@ async def update_end_user( ) ) - update_end_user_table_data[ - "budget_id" - ] = budget_table_data_record.budget_id + update_end_user_table_data["budget_id"] = ( + budget_table_data_record.budget_id + ) else: ## Update existing budget ## budget_table_data_record = ( diff --git a/litellm/proxy/management_endpoints/fallback_management_endpoints.py b/litellm/proxy/management_endpoints/fallback_management_endpoints.py index f91b95acd6c..ffb12111d82 100644 --- a/litellm/proxy/management_endpoints/fallback_management_endpoints.py +++ b/litellm/proxy/management_endpoints/fallback_management_endpoints.py @@ -7,6 +7,7 @@ POST /fallback - Create or update fallbacks for a specific model GET /fallback/{model} - Get fallbacks for a specific model DELETE /fallback/{model} - Delete fallbacks for a specific model """ + # pyright: reportMissingImports=false import json diff --git a/litellm/proxy/management_endpoints/internal_user_endpoints.py b/litellm/proxy/management_endpoints/internal_user_endpoints.py index 646779e6f81..0515be5d601 100644 --- a/litellm/proxy/management_endpoints/internal_user_endpoints.py +++ b/litellm/proxy/management_endpoints/internal_user_endpoints.py @@ -81,9 +81,9 @@ def _update_internal_new_user_params(data_json: dict, data: NewUserRequest) -> d auto_create_key = data_json.pop("auto_create_key", True) if auto_create_key is False: - data_json[ - "table_name" - ] = "user" # only create a user, don't create key if 'auto_create_key' set to False + data_json["table_name"] = ( + "user" # only create a user, don't create key if 'auto_create_key' set to False + ) if litellm.default_internal_user_params and ( data.user_role != LitellmUserRoles.PROXY_ADMIN.value @@ -1103,9 +1103,9 @@ def _update_internal_user_params( "budget_duration" not in non_default_values ): # applies internal user limits, if user role updated if is_internal_user and litellm.internal_user_budget_duration is not None: - non_default_values[ - "budget_duration" - ] = litellm.internal_user_budget_duration + non_default_values["budget_duration"] = ( + litellm.internal_user_budget_duration + ) from litellm.proxy.common_utils.timezone_utils import get_budget_reset_time non_default_values["budget_reset_at"] = get_budget_reset_time( @@ -2366,13 +2366,13 @@ async def ui_view_users( } # Query users with pagination and filters - users: Optional[ - List[BaseModel] - ] = await prisma_client.db.litellm_usertable.find_many( - where=where_conditions, - skip=skip, - take=page_size, - order={"created_at": "desc"}, + users: Optional[List[BaseModel]] = ( + await prisma_client.db.litellm_usertable.find_many( + where=where_conditions, + skip=skip, + take=page_size, + order={"created_at": "desc"}, + ) ) if not users: diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 00d8ce182ec..056ed6b8e43 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -502,9 +502,7 @@ def _enforce_upperbound_key_params( for elem in data: key, value = elem - upperbound_value = getattr( - litellm.upperbound_key_generate_params, key, None - ) + upperbound_value = getattr(litellm.upperbound_key_generate_params, key, None) if upperbound_value is not None: if value is None: if fill_defaults: @@ -524,9 +522,7 @@ def _enforce_upperbound_key_params( }, ) elif key in ["budget_duration", "duration"]: - upperbound_duration = duration_in_seconds( - duration=upperbound_value - ) + upperbound_duration = duration_in_seconds(duration=upperbound_value) if value == "-1": user_duration = float("inf") else: @@ -744,9 +740,9 @@ async def _common_key_generation_helper( # noqa: PLR0915 request_type="key", **data_json, table_name="key" ) - response[ - "soft_budget" - ] = data.soft_budget # include the user-input soft budget in the response + response["soft_budget"] = ( + data.soft_budget + ) # include the user-input soft budget in the response response = GenerateKeyResponse(**response) @@ -1759,9 +1755,7 @@ async def _process_single_key_update( decision = result.get("decision", True) message = result.get("message", "Authentication Failed - Custom Auth Rule") if not decision: - raise HTTPException( - status_code=status.HTTP_403_FORBIDDEN, detail=message - ) + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=message) # Enforce upperbound key params on update (don't fill defaults) _enforce_upperbound_key_params(update_key_request, fill_defaults=False) @@ -3222,10 +3216,10 @@ async def delete_verification_tokens( try: if prisma_client: tokens = [_hash_token_if_needed(token=key) for key in tokens] - _keys_being_deleted: List[ - LiteLLM_VerificationToken - ] = await prisma_client.db.litellm_verificationtoken.find_many( - where={"token": {"in": tokens}} + _keys_being_deleted: List[LiteLLM_VerificationToken] = ( + await prisma_client.db.litellm_verificationtoken.find_many( + where={"token": {"in": tokens}} + ) ) if len(_keys_being_deleted) == 0: @@ -3425,9 +3419,9 @@ async def _rotate_master_key( # noqa: PLR0915 from litellm.proxy.proxy_server import proxy_config try: - models: Optional[ - List - ] = await prisma_client.db.litellm_proxymodeltable.find_many() + models: Optional[List] = ( + await prisma_client.db.litellm_proxymodeltable.find_many() + ) except Exception: models = None # 2. process model table @@ -4067,11 +4061,11 @@ async def validate_key_list_check( param="user_id", code=status.HTTP_403_FORBIDDEN, ) - complete_user_info_db_obj: Optional[ - BaseModel - ] = await prisma_client.db.litellm_usertable.find_unique( - where={"user_id": user_api_key_dict.user_id}, - include={"organization_memberships": True}, + complete_user_info_db_obj: Optional[BaseModel] = ( + await prisma_client.db.litellm_usertable.find_unique( + where={"user_id": user_api_key_dict.user_id}, + include={"organization_memberships": True}, + ) ) if complete_user_info_db_obj is None: @@ -4154,10 +4148,10 @@ async def _fetch_user_team_objects( if complete_user_info is None or not complete_user_info.teams: return [] - teams: Optional[ - List[BaseModel] - ] = await prisma_client.db.litellm_teamtable.find_many( - where={"team_id": {"in": complete_user_info.teams}} + teams: Optional[List[BaseModel]] = ( + await prisma_client.db.litellm_teamtable.find_many( + where={"team_id": {"in": complete_user_info.teams}} + ) ) if teams is None: return [] diff --git a/litellm/proxy/management_endpoints/organization_endpoints.py b/litellm/proxy/management_endpoints/organization_endpoints.py index edea0c79c96..ffce23e1844 100644 --- a/litellm/proxy/management_endpoints/organization_endpoints.py +++ b/litellm/proxy/management_endpoints/organization_endpoints.py @@ -726,20 +726,20 @@ async def info_organization(organization_id: str): if prisma_client is None: raise HTTPException(status_code=500, detail={"error": "No db connected"}) - response: Optional[ - LiteLLM_OrganizationTableWithMembers - ] = await prisma_client.db.litellm_organizationtable.find_unique( - where={"organization_id": organization_id}, - include={ - "litellm_budget_table": True, - "members": { - "include": { - "user": True, - } + response: Optional[LiteLLM_OrganizationTableWithMembers] = ( + await prisma_client.db.litellm_organizationtable.find_unique( + where={"organization_id": organization_id}, + include={ + "litellm_budget_table": True, + "members": { + "include": { + "user": True, + } + }, + "teams": True, + "object_permission": True, }, - "teams": True, - "object_permission": True, - }, + ) ) if response is None: @@ -1035,16 +1035,16 @@ async def organization_member_update( }, data={"budget_id": budget_id}, ) - final_organization_membership: Optional[ - BaseModel - ] = await prisma_client.db.litellm_organizationmembership.find_unique( - where={ - "user_id_organization_id": { - "user_id": data.user_id, - "organization_id": data.organization_id, - } - }, - include={"litellm_budget_table": True}, + final_organization_membership: Optional[BaseModel] = ( + await prisma_client.db.litellm_organizationmembership.find_unique( + where={ + "user_id_organization_id": { + "user_id": data.user_id, + "organization_id": data.organization_id, + } + }, + include={"litellm_budget_table": True}, + ) ) if final_organization_membership is None: diff --git a/litellm/proxy/management_endpoints/project_endpoints.py b/litellm/proxy/management_endpoints/project_endpoints.py index 8f48f9def78..f6ed7767c46 100644 --- a/litellm/proxy/management_endpoints/project_endpoints.py +++ b/litellm/proxy/management_endpoints/project_endpoints.py @@ -601,9 +601,11 @@ async def update_project( # noqa: PLR0915 user_api_key_dict=user_api_key_dict, team_id=existing_project.team_id, prisma_client=prisma_client, - team_object=LiteLLM_TeamTable(**team_obj_for_checks.model_dump()) - if team_obj_for_checks - else None, + team_object=( + LiteLLM_TeamTable(**team_obj_for_checks.model_dump()) + if team_obj_for_checks + else None + ), ) if not has_permission: @@ -662,9 +664,9 @@ async def update_project( # noqa: PLR0915 data=object_permission_data, ) ) - update_data[ - "object_permission_id" - ] = created_permission.object_permission_id + update_data["object_permission_id"] = ( + created_permission.object_permission_id + ) # Handle metadata fields for field in LiteLLM_ManagementEndpoint_MetadataFields: diff --git a/litellm/proxy/management_endpoints/scim/scim_v2.py b/litellm/proxy/management_endpoints/scim/scim_v2.py index 2d657d96c1b..4c472ed7f21 100644 --- a/litellm/proxy/management_endpoints/scim/scim_v2.py +++ b/litellm/proxy/management_endpoints/scim/scim_v2.py @@ -765,13 +765,13 @@ async def get_users( where_conditions["user_email"] = email # Get users from database - users: List[ - LiteLLM_UserTable - ] = await prisma_client.db.litellm_usertable.find_many( - where=where_conditions, - skip=(startIndex - 1), - take=count, - order={"created_at": "desc"}, + users: List[LiteLLM_UserTable] = ( + await prisma_client.db.litellm_usertable.find_many( + where=where_conditions, + skip=(startIndex - 1), + take=count, + order={"created_at": "desc"}, + ) ) # Get total count for pagination diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index e1534573789..5862e865e8c 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -1545,12 +1545,12 @@ async def update_team( # noqa: PLR0915 updated_kv["router_settings"] = safe_dumps(updated_kv["router_settings"]) updated_kv = prisma_client.jsonify_team_object(db_data=updated_kv) - team_row: Optional[ - LiteLLM_TeamTable - ] = await prisma_client.db.litellm_teamtable.update( - where={"team_id": data.team_id}, - data=updated_kv, - include={"litellm_model_table": True}, # type: ignore + team_row: Optional[LiteLLM_TeamTable] = ( + await prisma_client.db.litellm_teamtable.update( + where={"team_id": data.team_id}, + data=updated_kv, + include={"litellm_model_table": True}, # type: ignore + ) ) if team_row is None or team_row.team_id is None: @@ -2297,13 +2297,13 @@ async def team_member_delete( ) # Fetch keys before deletion to persist them - keys_to_delete: List[ - LiteLLM_VerificationToken - ] = await prisma_client.db.litellm_verificationtoken.find_many( - where={ - "user_id": {"in": list(user_ids_to_delete)}, - "team_id": data.team_id, - } + keys_to_delete: List[LiteLLM_VerificationToken] = ( + await prisma_client.db.litellm_verificationtoken.find_many( + where={ + "user_id": {"in": list(user_ids_to_delete)}, + "team_id": data.team_id, + } + ) ) if keys_to_delete: @@ -2687,10 +2687,10 @@ async def delete_team( team_rows: List[LiteLLM_TeamTable] = [] for team_id in data.team_ids: try: - team_row_base: Optional[ - BaseModel - ] = await prisma_client.db.litellm_teamtable.find_unique( - where={"team_id": team_id} + team_row_base: Optional[BaseModel] = ( + await prisma_client.db.litellm_teamtable.find_unique( + where={"team_id": team_id} + ) ) if team_row_base is None: raise Exception @@ -2749,10 +2749,10 @@ async def delete_team( _persist_deleted_verification_tokens, ) - keys_to_delete: List[ - LiteLLM_VerificationToken - ] = await prisma_client.db.litellm_verificationtoken.find_many( - where={"team_id": {"in": data.team_ids}} + keys_to_delete: List[LiteLLM_VerificationToken] = ( + await prisma_client.db.litellm_verificationtoken.find_many( + where={"team_id": {"in": data.team_ids}} + ) ) if keys_to_delete: @@ -2937,9 +2937,7 @@ async def _resolve_team_access_group_resources(_team_info: Any) -> None: info response by resolving inherited resources from its access groups.""" if not _team_info.access_group_ids: return - ag_lookup = await _batch_resolve_access_group_resources( - _team_info.access_group_ids - ) + ag_lookup = await _batch_resolve_access_group_resources(_team_info.access_group_ids) models, mcp_ids, agent_ids = set(), set(), set() for ag_id in _team_info.access_group_ids: if ag_id in ag_lookup: @@ -2991,11 +2989,11 @@ async def team_info( ) try: - team_info: Optional[ - BaseModel - ] = await prisma_client.db.litellm_teamtable.find_unique( - where={"team_id": team_id}, - include={"object_permission": True}, + team_info: Optional[BaseModel] = ( + await prisma_client.db.litellm_teamtable.find_unique( + where={"team_id": team_id}, + include={"object_permission": True}, + ) ) if team_info is None: raise Exception @@ -3447,9 +3445,7 @@ async def _enforce_list_team_v2_access( if organization_id and organization_id not in org_admin_org_ids: raise HTTPException( status_code=403, - detail={ - "error": "You can only view teams within your organizations." - }, + detail={"error": "You can only view teams within your organizations."}, ) verbose_proxy_logger.debug( "list_team_v2: org admin access for user=%s, org_ids=%s, user_id_filter=%s", @@ -3640,8 +3636,7 @@ async def list_team_v2( # Resolve resources inherited from access groups (single batch query) if not use_deleted_table: team_items_with_ag = [ - t for t in team_list - if isinstance(t, TeamListItem) and t.access_group_ids + t for t in team_list if isinstance(t, TeamListItem) and t.access_group_ids ] if team_items_with_ag: all_ag_ids = [ @@ -3652,7 +3647,7 @@ async def list_team_v2( ag_lookup = await _batch_resolve_access_group_resources(all_ag_ids) for team_item in team_items_with_ag: models, mcp_ids, agent_ids = set(), set(), set() - for ag_id in (team_item.access_group_ids or []): + for ag_id in team_item.access_group_ids or []: if ag_id in ag_lookup: models.update(ag_lookup[ag_id]["models"]) mcp_ids.update(ag_lookup[ag_id]["mcp_server_ids"]) diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py index 0bfe8eb75bc..46e7963da7c 100644 --- a/litellm/proxy/management_endpoints/ui_sso.py +++ b/litellm/proxy/management_endpoints/ui_sso.py @@ -722,9 +722,9 @@ async def _setup_role_mappings() -> Optional["RoleMappings"]: import ast try: - generic_user_role_mappings_data: Dict[ - LitellmUserRoles, List[str] - ] = ast.literal_eval(generic_role_mappings) + generic_user_role_mappings_data: Dict[LitellmUserRoles, List[str]] = ( + ast.literal_eval(generic_role_mappings) + ) if isinstance(generic_user_role_mappings_data, dict): from litellm.types.proxy.management_endpoints.ui_sso import RoleMappings @@ -881,9 +881,9 @@ async def get_generic_sso_response( verbose_proxy_logger.debug("calling generic_sso.verify_and_process") additional_generic_sso_headers_dict = _parse_generic_sso_headers() - code_verifier: Optional[ - str - ] = None # assigned inside try; initialized for type tracking + code_verifier: Optional[str] = ( + None # assigned inside try; initialized for type tracking + ) access_token_payload: Optional[dict] = None # decoded JWT access token claims try: @@ -1233,9 +1233,11 @@ async def _sync_user_role_from_jwt_role_map( user_info.user_role = mapped_role.value await user_api_key_cache.async_set_cache( key=user_info.user_id, - value=user_info.model_dump() - if hasattr(user_info, "model_dump") - else dict(user_info), + value=( + user_info.model_dump() + if hasattr(user_info, "model_dump") + else dict(user_info) + ), ) @@ -1261,9 +1263,9 @@ def apply_user_info_values_to_sso_user_defined_values( else: # SSO didn't provide a valid role, fall back to DB role or default if user_info is None or user_info.user_role is None: - user_defined_values[ - "user_role" - ] = LitellmUserRoles.INTERNAL_USER_VIEW_ONLY.value + user_defined_values["user_role"] = ( + LitellmUserRoles.INTERNAL_USER_VIEW_ONLY.value + ) verbose_proxy_logger.debug( "No SSO or DB role found, using default: INTERNAL_USER_VIEW_ONLY" ) @@ -1703,9 +1705,9 @@ async def insert_sso_user( if user_defined_values.get("max_budget") is None: user_defined_values["max_budget"] = litellm.max_internal_user_budget if user_defined_values.get("budget_duration") is None: - user_defined_values[ - "budget_duration" - ] = litellm.internal_user_budget_duration + user_defined_values["budget_duration"] = ( + litellm.internal_user_budget_duration + ) if user_defined_values["user_role"] is None: user_defined_values["user_role"] = LitellmUserRoles.INTERNAL_USER_VIEW_ONLY @@ -3348,9 +3350,9 @@ class MicrosoftSSOHandler: # if user is trying to get the raw sso response for debugging, return the raw sso response if return_raw_sso_response: - original_msft_result[ - MicrosoftSSOHandler.GRAPH_API_RESPONSE_KEY - ] = user_team_ids + original_msft_result[MicrosoftSSOHandler.GRAPH_API_RESPONSE_KEY] = ( + user_team_ids + ) original_msft_result["app_roles"] = app_roles return original_msft_result or {} @@ -3469,9 +3471,9 @@ class MicrosoftSSOHandler: # Fetch user membership from Microsoft Graph API all_group_ids = [] - next_link: Optional[ - str - ] = MicrosoftSSOHandler.graph_api_user_groups_endpoint + next_link: Optional[str] = ( + MicrosoftSSOHandler.graph_api_user_groups_endpoint + ) auth_headers = {"Authorization": f"Bearer {access_token}"} page_count = 0 diff --git a/litellm/proxy/management_endpoints/user_agent_analytics_endpoints.py b/litellm/proxy/management_endpoints/user_agent_analytics_endpoints.py index 9d3ecdba92f..872b6fa2250 100644 --- a/litellm/proxy/management_endpoints/user_agent_analytics_endpoints.py +++ b/litellm/proxy/management_endpoints/user_agent_analytics_endpoints.py @@ -35,9 +35,9 @@ class TagActiveUsersResponse(BaseModel): tag: str active_users: int date: str # The specific date or period identifier - period_start: Optional[ - str - ] = None # For WAU/MAU, this will be the start of the period + period_start: Optional[str] = ( + None # For WAU/MAU, this will be the start of the period + ) period_end: Optional[str] = None # For WAU/MAU, this will be the end of the period diff --git a/litellm/proxy/middleware/prometheus_auth_middleware.py b/litellm/proxy/middleware/prometheus_auth_middleware.py index 5915e4aa07d..6bdff59da52 100644 --- a/litellm/proxy/middleware/prometheus_auth_middleware.py +++ b/litellm/proxy/middleware/prometheus_auth_middleware.py @@ -1,6 +1,7 @@ """ Prometheus Auth Middleware - Pure ASGI implementation """ + import json from fastapi import Request diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.py index 973836b13d8..8e9fcb75197 100644 --- a/litellm/proxy/openai_files_endpoints/files_endpoints.py +++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py @@ -363,10 +363,10 @@ async def create_file( # noqa: PLR0915 expires_after: Optional[FileExpiresAfter] = None form_data_raw = await request.form() form_data_dict: Dict[str, Any] = dict(form_data_raw) - extracted_litellm_metadata: Optional[ - Dict[str, Any] - ] = extract_nested_form_metadata( - form_data=form_data_dict, prefix="litellm_metadata[" + extracted_litellm_metadata: Optional[Dict[str, Any]] = ( + extract_nested_form_metadata( + form_data=form_data_dict, prefix="litellm_metadata[" + ) ) expires_after_anchor = form_data_raw.get("expires_after[anchor]") expires_after_seconds_str = form_data_raw.get("expires_after[seconds]") diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py index fcb1e0b2e49..216eb61a9d1 100644 --- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py +++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py @@ -168,9 +168,9 @@ class AnthropicPassthroughLoggingHandler: litellm_model_response.model = model logging_obj.model_call_details["model"] = model if not logging_obj.model_call_details.get("custom_llm_provider"): - logging_obj.model_call_details[ - "custom_llm_provider" - ] = litellm.LlmProviders.ANTHROPIC.value + logging_obj.model_call_details["custom_llm_provider"] = ( + litellm.LlmProviders.ANTHROPIC.value + ) return kwargs except Exception as e: verbose_proxy_logger.exception( diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py index 38b2734bc26..29bbb37501f 100644 --- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py +++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/openai_passthrough_logging_handler.py @@ -367,9 +367,9 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): kwargs["custom_llm_provider"] = custom_llm_provider # Extract user information for tracking - passthrough_logging_payload: Optional[ - PassthroughStandardLoggingPayload - ] = kwargs.get("passthrough_logging_payload") + passthrough_logging_payload: Optional[PassthroughStandardLoggingPayload] = ( + kwargs.get("passthrough_logging_payload") + ) if passthrough_logging_payload: user = handler_instance._get_user_from_metadata( passthrough_logging_payload=passthrough_logging_payload, @@ -398,9 +398,7 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): endpoint_type = ( "chat_completions" if is_chat_completions - else "image_generation" - if is_image_generation - else "image_editing" + else "image_generation" if is_image_generation else "image_editing" ) verbose_proxy_logger.debug( f"OpenAI passthrough cost tracking - Endpoint: {endpoint_type}, Model: {model}, Cost: ${response_cost:.6f}" @@ -558,10 +556,10 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): } # Extract user information for tracking - passthrough_logging_payload: Optional[ - PassthroughStandardLoggingPayload - ] = litellm_logging_obj.model_call_details.get( - "passthrough_logging_payload" + passthrough_logging_payload: Optional[PassthroughStandardLoggingPayload] = ( + litellm_logging_obj.model_call_details.get( + "passthrough_logging_payload" + ) ) if passthrough_logging_payload: user = handler_instance._get_user_from_metadata( @@ -584,9 +582,9 @@ class OpenAIPassthroughLoggingHandler(BasePassthroughLoggingHandler): # Update logging object with cost information litellm_logging_obj.model_call_details["model"] = model - litellm_logging_obj.model_call_details[ - "custom_llm_provider" - ] = custom_llm_provider + litellm_logging_obj.model_call_details["custom_llm_provider"] = ( + custom_llm_provider + ) litellm_logging_obj.model_call_details["response_cost"] = response_cost verbose_proxy_logger.debug( diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py index 4f68c92b9d9..050e52aede0 100644 --- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py @@ -457,10 +457,10 @@ class HttpPassThroughEndpointHelpers(BasePassthroughUtils): for field_name, field_value in form_data.items(): if isinstance(field_value, (StarletteUploadFile, UploadFile)): - files[ - field_name - ] = await HttpPassThroughEndpointHelpers._build_request_files_from_upload_file( - upload_file=field_value + files[field_name] = ( + await HttpPassThroughEndpointHelpers._build_request_files_from_upload_file( + upload_file=field_value + ) ) else: form_data_dict[field_name] = field_value @@ -537,9 +537,9 @@ class HttpPassThroughEndpointHelpers(BasePassthroughUtils): "passthrough_logging_payload": passthrough_logging_payload, } - logging_obj.model_call_details[ - "passthrough_logging_payload" - ] = passthrough_logging_payload + logging_obj.model_call_details["passthrough_logging_payload"] = ( + passthrough_logging_payload + ) return kwargs @@ -1468,9 +1468,9 @@ async def websocket_passthrough_request( # noqa: PLR0915 ) if extracted_model: kwargs["model"] = extracted_model - kwargs[ - "custom_llm_provider" - ] = "vertex_ai-language-models" + kwargs["custom_llm_provider"] = ( + "vertex_ai-language-models" + ) # Update logging object with correct model logging_obj.model = extracted_model logging_obj.model_call_details[ @@ -1536,9 +1536,9 @@ async def websocket_passthrough_request( # noqa: PLR0915 # Update logging object with correct model logging_obj.model = extracted_model logging_obj.model_call_details["model"] = extracted_model - logging_obj.model_call_details[ - "custom_llm_provider" - ] = "vertex_ai_language_models" + logging_obj.model_call_details["custom_llm_provider"] = ( + "vertex_ai_language_models" + ) verbose_proxy_logger.debug( f"WebSocket passthrough ({endpoint}): Successfully extracted model '{extracted_model}' and set provider to 'vertex_ai' from server setup response" ) diff --git a/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py b/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py index ae2f8edc74f..a32659e45bd 100644 --- a/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py +++ b/litellm/proxy/pass_through_endpoints/passthrough_endpoint_router.py @@ -134,9 +134,9 @@ class PassthroughEndpointRouter: vertex_location=location, vertex_credentials=vertex_credentials, ) - self.deployment_key_to_vertex_credentials[ - deployment_key - ] = vertex_pass_through_credentials + self.deployment_key_to_vertex_credentials[deployment_key] = ( + vertex_pass_through_credentials + ) def _get_deployment_key( self, project_id: Optional[str], location: Optional[str] @@ -156,10 +156,10 @@ class PassthroughEndpointRouter: """ if litellm.vector_store_registry is None: return None - vector_store_to_run: Optional[ - LiteLLM_ManagedVectorStore - ] = litellm.vector_store_registry.get_litellm_managed_vector_store_from_registry( - vector_store_id=vector_store_id + vector_store_to_run: Optional[LiteLLM_ManagedVectorStore] = ( + litellm.vector_store_registry.get_litellm_managed_vector_store_from_registry( + vector_store_id=vector_store_id + ) ) return vector_store_to_run diff --git a/litellm/proxy/pass_through_endpoints/success_handler.py b/litellm/proxy/pass_through_endpoints/success_handler.py index 33819b888d0..c14378ce616 100644 --- a/litellm/proxy/pass_through_endpoints/success_handler.py +++ b/litellm/proxy/pass_through_endpoints/success_handler.py @@ -283,9 +283,9 @@ class PassThroughEndpointLogging: standard_logging_response_object = vertex_ai_live_handler_result["result"] kwargs = vertex_ai_live_handler_result["kwargs"] - return_dict[ - "standard_logging_response_object" - ] = standard_logging_response_object + return_dict["standard_logging_response_object"] = ( + standard_logging_response_object + ) return_dict["kwargs"] = kwargs return return_dict @@ -308,9 +308,9 @@ class PassThroughEndpointLogging: standard_logging_response_object: Optional[ PassThroughEndpointLoggingResultValues ] = None - logging_obj.model_call_details[ - "passthrough_logging_payload" - ] = passthrough_logging_payload + logging_obj.model_call_details["passthrough_logging_payload"] = ( + passthrough_logging_payload + ) if self.is_assemblyai_route(url_route): if ( AssemblyAIPassthroughLoggingHandler._should_log_request( @@ -487,8 +487,8 @@ class PassThroughEndpointLogging: kwargs["response_cost"] = passthrough_logging_payload.get( "cost_per_request" ) - logging_obj.model_call_details[ - "response_cost" - ] = passthrough_logging_payload.get("cost_per_request") + logging_obj.model_call_details["response_cost"] = ( + passthrough_logging_payload.get("cost_per_request") + ) return kwargs diff --git a/litellm/proxy/policy_engine/attachment_registry.py b/litellm/proxy/policy_engine/attachment_registry.py index 530e1fca1f5..6d1096d5ee9 100644 --- a/litellm/proxy/policy_engine/attachment_registry.py +++ b/litellm/proxy/policy_engine/attachment_registry.py @@ -464,13 +464,15 @@ class AttachmentRegistry: attachment = PolicyAttachment( policy=attachment_response.policy_name, scope=attachment_response.scope, - teams=attachment_response.teams - if attachment_response.teams - else None, + teams=( + attachment_response.teams if attachment_response.teams else None + ), keys=attachment_response.keys if attachment_response.keys else None, - models=attachment_response.models - if attachment_response.models - else None, + models=( + attachment_response.models + if attachment_response.models + else None + ), tags=attachment_response.tags if attachment_response.tags else None, ) self._attachments.append(attachment) diff --git a/litellm/proxy/policy_engine/init_policies.py b/litellm/proxy/policy_engine/init_policies.py index 3167a0fe8b3..d5529f5bfe1 100644 --- a/litellm/proxy/policy_engine/init_policies.py +++ b/litellm/proxy/policy_engine/init_policies.py @@ -264,9 +264,9 @@ def get_policies_summary() -> Dict[str, Any]: "description": policy.description if policy else None, "guardrails_add": policy.guardrails.get_add() if policy else [], "guardrails_remove": policy.guardrails.get_remove() if policy else [], - "condition": policy.condition.model_dump() - if policy and policy.condition - else None, + "condition": ( + policy.condition.model_dump() if policy and policy.condition else None + ), "resolved_guardrails": resolved_policy.guardrails, "inheritance_chain": resolved_policy.inheritance_chain, } diff --git a/litellm/proxy/prompts/prompt_registry.py b/litellm/proxy/prompts/prompt_registry.py index 58df60a42cb..25368c2a834 100644 --- a/litellm/proxy/prompts/prompt_registry.py +++ b/litellm/proxy/prompts/prompt_registry.py @@ -97,9 +97,9 @@ class InMemoryPromptRegistry: Prompt id to Prompt object mapping """ - self.prompt_id_to_custom_prompt: Dict[ - str, Optional[CustomPromptManagement] - ] = {} + self.prompt_id_to_custom_prompt: Dict[str, Optional[CustomPromptManagement]] = ( + {} + ) """ Guardrail id to CustomGuardrail object mapping """ diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 9738ae4f1a2..36e2262dee6 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -54,7 +54,7 @@ from litellm.constants import ( LITELLM_SETTINGS_SAFE_DB_OVERRIDES, LITELLM_UI_ALLOW_HEADERS, LITELLM_UI_SESSION_DURATION, - DAILY_TAG_SPEND_BATCH_MULTIPLIER + DAILY_TAG_SPEND_BATCH_MULTIPLIER, ) from litellm.litellm_core_utils.litellm_logging import ( _init_custom_logger_compatible_class, @@ -2161,9 +2161,11 @@ def run_ollama_serve(): with open(os.devnull, "w") as devnull: subprocess.Popen(command, stdout=devnull, stderr=devnull) except Exception as e: - verbose_proxy_logger.debug(f""" + verbose_proxy_logger.debug( + f""" LiteLLM Warning: proxy started with `ollama` model\n`ollama serve` failed with Exception{e}. \nEnsure you run `ollama serve` - """) + """ + ) def _get_process_rss_mb() -> Optional[float]: @@ -2316,9 +2318,13 @@ def _write_health_state_to_router_cache( exception_status = getattr(original_exception, "status_code", 500) - if llm_router.health_check_ignore_transient_errors and exception_status in ( - 429, - 408, + if ( + llm_router.health_check_ignore_transient_errors + and exception_status + in ( + 429, + 408, + ) ): continue @@ -6287,7 +6293,9 @@ class ProxyStartupEvent: ### UPDATE DAILY TAG SPEND (separate scheduler job with longer interval) ### ## Reduces QPS as there are more tags for a single request - tag_spend_update_interval = int(batch_writing_interval * DAILY_TAG_SPEND_BATCH_MULTIPLIER) + tag_spend_update_interval = int( + batch_writing_interval * DAILY_TAG_SPEND_BATCH_MULTIPLIER + ) from litellm.proxy.utils import update_daily_tag_spend scheduler.add_job( diff --git a/litellm/proxy/rag_endpoints/endpoints.py b/litellm/proxy/rag_endpoints/endpoints.py index 76136c12be5..95ca51612fc 100644 --- a/litellm/proxy/rag_endpoints/endpoints.py +++ b/litellm/proxy/rag_endpoints/endpoints.py @@ -180,9 +180,9 @@ async def _save_vector_store_to_db_from_rag_ingest( vector_store_name=vector_store_name, vector_store_description=vector_store_description, vector_store_metadata=initial_metadata, - litellm_params=provider_specific_params - if provider_specific_params - else None, + litellm_params=( + provider_specific_params if provider_specific_params else None + ), team_id=user_api_key_dict.team_id, user_id=user_api_key_dict.user_id, ) diff --git a/litellm/proxy/response_polling/__init__.py b/litellm/proxy/response_polling/__init__.py index b500354c373..7ece7099e27 100644 --- a/litellm/proxy/response_polling/__init__.py +++ b/litellm/proxy/response_polling/__init__.py @@ -1,6 +1,7 @@ """ Response Polling Module for Background Responses with Cache """ + from litellm.proxy.response_polling.background_streaming import ( background_streaming_task, ) diff --git a/litellm/proxy/response_polling/background_streaming.py b/litellm/proxy/response_polling/background_streaming.py index bcc98175773..03039d4f441 100644 --- a/litellm/proxy/response_polling/background_streaming.py +++ b/litellm/proxy/response_polling/background_streaming.py @@ -7,6 +7,7 @@ with partial results for polling. Follows OpenAI Response Streaming format: https://platform.openai.com/docs/api-reference/responses-streaming """ + import asyncio import json from typing import Any, Optional, cast @@ -118,9 +119,9 @@ async def background_streaming_task( # noqa: PLR0915 UPDATE_INTERVAL = 0.150 # 150ms batching interval # Track the terminal event from the stream (may not be "completed") - terminal_status: Optional[ - ResponsesAPIStatus - ] = None # Will be set by response.completed/failed/incomplete/cancelled + terminal_status: Optional[ResponsesAPIStatus] = ( + None # Will be set by response.completed/failed/incomplete/cancelled + ) terminal_error = None _event_to_status = { "response.completed": "completed", @@ -211,9 +212,9 @@ async def background_streaming_task( # noqa: PLR0915 if isinstance( content_list[content_index], dict ): - content_list[content_index][ - "text" - ] = accumulated_text[key] + content_list[content_index]["text"] = ( + accumulated_text[key] + ) state_dirty = True elif event_type == "response.content_part.done": diff --git a/litellm/proxy/response_polling/polling_handler.py b/litellm/proxy/response_polling/polling_handler.py index 71e97a46c62..739df3ce673 100644 --- a/litellm/proxy/response_polling/polling_handler.py +++ b/litellm/proxy/response_polling/polling_handler.py @@ -1,6 +1,7 @@ """ Response Polling Handler for Background Responses with Cache """ + import json from datetime import datetime, timezone from typing import Any, Dict, List, Optional diff --git a/litellm/proxy/search_endpoints/search_tool_management.py b/litellm/proxy/search_endpoints/search_tool_management.py index c46bbfddcac..725e83bf96d 100644 --- a/litellm/proxy/search_endpoints/search_tool_management.py +++ b/litellm/proxy/search_endpoints/search_tool_management.py @@ -1,6 +1,7 @@ """ CRUD ENDPOINTS FOR SEARCH TOOLS """ + from datetime import datetime from typing import Any, Dict, List, Union @@ -536,9 +537,9 @@ async def test_search_tool_connection(request: TestSearchToolConnectionRequest): "status": "success", "message": f"Successfully connected to {search_provider} search provider", "test_query": test_query, - "results_count": len(response.results) - if response and response.results - else 0, + "results_count": ( + len(response.results) if response and response.results else 0 + ), } except Exception as e: diff --git a/litellm/proxy/search_endpoints/search_tool_registry.py b/litellm/proxy/search_endpoints/search_tool_registry.py index e9eba1e1799..d4adc2573ea 100644 --- a/litellm/proxy/search_endpoints/search_tool_registry.py +++ b/litellm/proxy/search_endpoints/search_tool_registry.py @@ -1,6 +1,7 @@ """ Search Tool Registry for managing search tool configurations. """ + from datetime import datetime, timezone from typing import List, Optional diff --git a/litellm/proxy/spend_tracking/cold_storage_handler.py b/litellm/proxy/spend_tracking/cold_storage_handler.py index adbbc141234..57c41bafccd 100644 --- a/litellm/proxy/spend_tracking/cold_storage_handler.py +++ b/litellm/proxy/spend_tracking/cold_storage_handler.py @@ -3,6 +3,7 @@ This module is responsible for handling Getting/Setting the proxy server request It allows fetching a dict of the proxy server request from s3 or GCS bucket. """ + from typing import Optional import litellm @@ -32,19 +33,19 @@ class ColdStorageHandler: """ # select the custom logger to use for cold storage - custom_logger_name: Optional[ - _custom_logger_compatible_callbacks_literal - ] = self._select_custom_logger_for_cold_storage() + custom_logger_name: Optional[_custom_logger_compatible_callbacks_literal] = ( + self._select_custom_logger_for_cold_storage() + ) # if no custom logger name is configured, return None if custom_logger_name is None: return None # get the active/initialized custom logger - custom_logger: Optional[ - CustomLogger - ] = litellm.logging_callback_manager.get_active_custom_logger_for_callback_name( - custom_logger_name + custom_logger: Optional[CustomLogger] = ( + litellm.logging_callback_manager.get_active_custom_logger_for_callback_name( + custom_logger_name + ) ) # if no custom logger is found, return None diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 8ea93453ed3..b20b4a54e39 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -127,9 +127,9 @@ def _get_spend_logs_metadata( clean_metadata["applied_guardrails"] = applied_guardrails clean_metadata["batch_models"] = batch_models clean_metadata["mcp_tool_call_metadata"] = mcp_tool_call_metadata - clean_metadata[ - "vector_store_request_metadata" - ] = _get_vector_store_request_for_spend_logs_payload(vector_store_request_metadata) + clean_metadata["vector_store_request_metadata"] = ( + _get_vector_store_request_for_spend_logs_payload(vector_store_request_metadata) + ) clean_metadata["guardrail_information"] = guardrail_information clean_metadata["usage_object"] = usage_object clean_metadata["model_map_information"] = model_map_information diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index ec98cfd4d1e..cf33cdf572a 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -1898,9 +1898,9 @@ class ProxyLogging: normalized_call_type = CallTypes.aembedding.value if normalized_call_type is not None: litellm_logging_obj.call_type = normalized_call_type - litellm_logging_obj.model_call_details[ - "call_type" - ] = normalized_call_type + litellm_logging_obj.model_call_details["call_type"] = ( + normalized_call_type + ) # Pass-through endpoints are logged via the callback loop's # async_post_call_failure_hook — skip pre_call and failure handlers. if litellm_logging_obj.call_type == CallTypes.pass_through.value: @@ -4868,25 +4868,27 @@ async def update_daily_tag_spend( ): """ Separate scheduler job to commit daily tag spend updates. - + Runs at a longer interval (2.3x default) than the main update_spend job to reduce query contention for DailyTagSpend table. - + This is called by a dedicated scheduler job and does NOT process: - Regular spend updates (user, key, team, org) - End-user spend - Agent spend - Spend logs - + Only processes tag spend transactions from the daily_tag_spend_update_queue. - + Args: prisma_client: PrismaClient instance proxy_logging_obj: ProxyLogging instance for error handling """ n_retry_times = 3 try: - if proxy_logging_obj.db_spend_update_writer.redis_update_buffer._should_commit_spend_updates_to_redis(): + if ( + proxy_logging_obj.db_spend_update_writer.redis_update_buffer._should_commit_spend_updates_to_redis() + ): await proxy_logging_obj.db_spend_update_writer._commit_daily_tag_spend_to_db_with_redis( prisma_client=prisma_client, n_retry_times=n_retry_times, diff --git a/litellm/proxy/vector_store_endpoints/endpoints.py b/litellm/proxy/vector_store_endpoints/endpoints.py index d4594fb2fd0..b7e3d8de3d3 100644 --- a/litellm/proxy/vector_store_endpoints/endpoints.py +++ b/litellm/proxy/vector_store_endpoints/endpoints.py @@ -68,10 +68,10 @@ def _update_request_data_with_litellm_managed_vector_store_registry( HTTPException: If user doesn't have access to the vector store """ if litellm.vector_store_registry is not None: - vector_store_to_run: Optional[ - LiteLLM_ManagedVectorStore - ] = litellm.vector_store_registry.get_litellm_managed_vector_store_from_registry( - vector_store_id=vector_store_id + vector_store_to_run: Optional[LiteLLM_ManagedVectorStore] = ( + litellm.vector_store_registry.get_litellm_managed_vector_store_from_registry( + vector_store_id=vector_store_id + ) ) if vector_store_to_run is not None: # Check access control if user_api_key_dict is provided diff --git a/litellm/proxy/vertex_ai_endpoints/langfuse_endpoints.py b/litellm/proxy/vertex_ai_endpoints/langfuse_endpoints.py index 627618387d5..b6454bf077b 100644 --- a/litellm/proxy/vertex_ai_endpoints/langfuse_endpoints.py +++ b/litellm/proxy/vertex_ai_endpoints/langfuse_endpoints.py @@ -70,10 +70,10 @@ async def langfuse_proxy_route( request=request, api_key="Bearer {}".format(api_key) ) - callback_settings_obj: Optional[ - TeamCallbackMetadata - ] = _get_dynamic_logging_metadata( - user_api_key_dict=user_api_key_dict, proxy_config=proxy_config + callback_settings_obj: Optional[TeamCallbackMetadata] = ( + _get_dynamic_logging_metadata( + user_api_key_dict=user_api_key_dict, proxy_config=proxy_config + ) ) dynamic_langfuse_public_key: Optional[str] = None diff --git a/litellm/responses/litellm_completion_transformation/handler.py b/litellm/responses/litellm_completion_transformation/handler.py index 5faa8b587c9..f730a089624 100644 --- a/litellm/responses/litellm_completion_transformation/handler.py +++ b/litellm/responses/litellm_completion_transformation/handler.py @@ -38,14 +38,16 @@ class LiteLLMCompletionTransformationHandler: Any, Any, Union[ResponsesAPIResponse, BaseResponsesAPIStreamingIterator] ], ]: - litellm_completion_request: dict = LiteLLMCompletionResponsesConfig.transform_responses_api_request_to_chat_completion_request( - model=model, - input=input, - responses_api_request=responses_api_request, - custom_llm_provider=custom_llm_provider, - stream=stream, - extra_headers=extra_headers, - **kwargs, + litellm_completion_request: dict = ( + LiteLLMCompletionResponsesConfig.transform_responses_api_request_to_chat_completion_request( + model=model, + input=input, + responses_api_request=responses_api_request, + custom_llm_provider=custom_llm_provider, + stream=stream, + extra_headers=extra_headers, + **kwargs, + ) ) if _is_async: @@ -68,10 +70,12 @@ class LiteLLMCompletionTransformationHandler: ) if isinstance(litellm_completion_response, ModelResponse): - responses_api_response: ResponsesAPIResponse = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - chat_completion_response=litellm_completion_response, - request_input=input, - responses_api_request=responses_api_request, + responses_api_response: ResponsesAPIResponse = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + chat_completion_response=litellm_completion_response, + request_input=input, + responses_api_request=responses_api_request, + ) ) return responses_api_response @@ -116,10 +120,12 @@ class LiteLLMCompletionTransformationHandler: ) if isinstance(litellm_completion_response, ModelResponse): - responses_api_response: ResponsesAPIResponse = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( - chat_completion_response=litellm_completion_response, - request_input=request_input, - responses_api_request=responses_api_request, + responses_api_response: ResponsesAPIResponse = ( + LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + chat_completion_response=litellm_completion_response, + request_input=request_input, + responses_api_request=responses_api_request, + ) ) return responses_api_response diff --git a/litellm/responses/litellm_completion_transformation/session_handler.py b/litellm/responses/litellm_completion_transformation/session_handler.py index 45ab16b0d4a..71ff2eb7acf 100644 --- a/litellm/responses/litellm_completion_transformation/session_handler.py +++ b/litellm/responses/litellm_completion_transformation/session_handler.py @@ -43,10 +43,10 @@ class ResponsesSessionHandler: verbose_proxy_logger.debug( "inside get_chat_completion_message_history_for_previous_response_id" ) - all_spend_logs: List[ - SpendLogsPayload - ] = await ResponsesSessionHandler.get_all_spend_logs_for_previous_response_id( - previous_response_id + all_spend_logs: List[SpendLogsPayload] = ( + await ResponsesSessionHandler.get_all_spend_logs_for_previous_response_id( + previous_response_id + ) ) verbose_proxy_logger.debug( "found %s spend logs for this response id", len(all_spend_logs) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 2207acbb37a..9075373f1cf 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2123,9 +2123,9 @@ class LiteLLMCompletionResponsesConfig: hasattr(completion_details, "reasoning_tokens") and completion_details.reasoning_tokens is not None ): - output_details_dict[ - "reasoning_tokens" - ] = completion_details.reasoning_tokens + output_details_dict["reasoning_tokens"] = ( + completion_details.reasoning_tokens + ) else: output_details_dict["reasoning_tokens"] = 0 diff --git a/litellm/responses/main.py b/litellm/responses/main.py index c82574278ba..a91b4056d6e 100644 --- a/litellm/responses/main.py +++ b/litellm/responses/main.py @@ -1115,11 +1115,11 @@ def delete_responses( raise ValueError("custom_llm_provider is required but passed as None") # get provider config - responses_api_provider_config: Optional[ - BaseResponsesAPIConfig - ] = ProviderConfigManager.get_provider_responses_api_config( - model=None, - provider=custom_llm_provider, + responses_api_provider_config: Optional[BaseResponsesAPIConfig] = ( + ProviderConfigManager.get_provider_responses_api_config( + model=None, + provider=custom_llm_provider, + ) ) if responses_api_provider_config is None: @@ -1296,11 +1296,11 @@ def get_responses( raise ValueError("custom_llm_provider is required but passed as None") # get provider config - responses_api_provider_config: Optional[ - BaseResponsesAPIConfig - ] = ProviderConfigManager.get_provider_responses_api_config( - model=None, - provider=custom_llm_provider, + responses_api_provider_config: Optional[BaseResponsesAPIConfig] = ( + ProviderConfigManager.get_provider_responses_api_config( + model=None, + provider=custom_llm_provider, + ) ) if responses_api_provider_config is None: @@ -1454,11 +1454,11 @@ def list_input_items( if custom_llm_provider is None: raise ValueError("custom_llm_provider is required but passed as None") - responses_api_provider_config: Optional[ - BaseResponsesAPIConfig - ] = ProviderConfigManager.get_provider_responses_api_config( - model=None, - provider=custom_llm_provider, + responses_api_provider_config: Optional[BaseResponsesAPIConfig] = ( + ProviderConfigManager.get_provider_responses_api_config( + model=None, + provider=custom_llm_provider, + ) ) if responses_api_provider_config is None: @@ -1613,11 +1613,11 @@ def cancel_responses( raise ValueError("custom_llm_provider is required but passed as None") # get provider config - responses_api_provider_config: Optional[ - BaseResponsesAPIConfig - ] = ProviderConfigManager.get_provider_responses_api_config( - model=None, - provider=custom_llm_provider, + responses_api_provider_config: Optional[BaseResponsesAPIConfig] = ( + ProviderConfigManager.get_provider_responses_api_config( + model=None, + provider=custom_llm_provider, + ) ) if responses_api_provider_config is None: @@ -1801,11 +1801,11 @@ def compact_responses( raise ValueError("custom_llm_provider is required but passed as None") # get provider config - responses_api_provider_config: Optional[ - BaseResponsesAPIConfig - ] = ProviderConfigManager.get_provider_responses_api_config( - model=model, - provider=custom_llm_provider, + responses_api_provider_config: Optional[BaseResponsesAPIConfig] = ( + ProviderConfigManager.get_provider_responses_api_config( + model=model, + provider=custom_llm_provider, + ) ) if responses_api_provider_config is None: diff --git a/litellm/responses/mcp/litellm_proxy_mcp_handler.py b/litellm/responses/mcp/litellm_proxy_mcp_handler.py index b729cdb92f2..94cff6922b5 100644 --- a/litellm/responses/mcp/litellm_proxy_mcp_handler.py +++ b/litellm/responses/mcp/litellm_proxy_mcp_handler.py @@ -162,7 +162,9 @@ class LiteLLM_Proxy_MCP_Handler: mcp_servers=all_server_ids, mcp_tool_permissions=tool_permissions, ) - return user_api_key_auth.model_copy(update={"object_permission": updated_op}) + return user_api_key_auth.model_copy( + update={"object_permission": updated_op} + ) except Exception as _e: verbose_logger.debug(f"Could not apply toolset permissions: {_e}") return user_api_key_auth @@ -259,10 +261,12 @@ class LiteLLM_Proxy_MCP_Handler: # Apply all resolved toolsets at once (union), avoiding permission overwrite. if resolved_toolset_ids and user_api_key_auth is not None: - user_api_key_auth = await LiteLLM_Proxy_MCP_Handler._apply_toolset_permissions( - resolved_toolset_ids=resolved_toolset_ids, - resolved_mcp_servers=resolved_mcp_servers, - user_api_key_auth=user_api_key_auth, + user_api_key_auth = ( + await LiteLLM_Proxy_MCP_Handler._apply_toolset_permissions( + resolved_toolset_ids=resolved_toolset_ids, + resolved_mcp_servers=resolved_mcp_servers, + user_api_key_auth=user_api_key_auth, + ) ) # When toolsets were resolved we updated object_permission.mcp_servers to the diff --git a/litellm/responses/mcp/mcp_streaming_iterator.py b/litellm/responses/mcp/mcp_streaming_iterator.py index 7aed48c2f9a..42c46dff47c 100644 --- a/litellm/responses/mcp/mcp_streaming_iterator.py +++ b/litellm/responses/mcp/mcp_streaming_iterator.py @@ -273,9 +273,9 @@ class MCPEnhancedStreamingIterator(BaseResponsesAPIStreamingIterator): self.finished = False # Event queues and generation flags - self.mcp_discovery_events: List[ - ResponsesAPIStreamingResponse - ] = mcp_events # Pre-generated MCP discovery events + self.mcp_discovery_events: List[ResponsesAPIStreamingResponse] = ( + mcp_events # Pre-generated MCP discovery events + ) self.tool_execution_events: List[ResponsesAPIStreamingResponse] = [] self.mcp_discovery_generated = True # Events are already generated self.mcp_events = ( @@ -284,9 +284,9 @@ class MCPEnhancedStreamingIterator(BaseResponsesAPIStreamingIterator): self.tool_server_map = tool_server_map # Iterator references - self.base_iterator: Optional[ - Union[Any, ResponsesAPIResponse] - ] = base_iterator # Will be created when needed + self.base_iterator: Optional[Union[Any, ResponsesAPIResponse]] = ( + base_iterator # Will be created when needed + ) self.follow_up_iterator: Optional[Any] = None # Response collection for tool execution @@ -582,9 +582,9 @@ class MCPEnhancedStreamingIterator(BaseResponsesAPIStreamingIterator): # Use the pre-fetched all_tools from original_request_params (no re-processing needed) params_for_llm = {} for key, value in params.items(): - params_for_llm[ - key - ] = value # Copy all params as-is since tools are already processed + params_for_llm[key] = ( + value # Copy all params as-is since tools are already processed + ) tools_count = ( len(params_for_llm.get("tools", [])) diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 10a74a5b3c6..012e6b60746 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -188,10 +188,10 @@ class BaseResponsesAPIStreamingIterator: ) if usage_obj is not None: try: - cost: Optional[ - float - ] = self.logging_obj._response_cost_calculator( - result=response_obj + cost: Optional[float] = ( + self.logging_obj._response_cost_calculator( + result=response_obj + ) ) if cost is not None: setattr(usage_obj, "cost", cost) diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py index 11097864225..54f1816286b 100644 --- a/litellm/responses/utils.py +++ b/litellm/responses/utils.py @@ -346,10 +346,10 @@ class ResponsesAPIRequestUtils: if encrypted_content and isinstance(encrypted_content, str): # Always wrap encrypted_content with model_id for redundancy - item[ - "encrypted_content" - ] = ResponsesAPIRequestUtils._wrap_encrypted_content_with_model_id( - encrypted_content, model_id + item["encrypted_content"] = ( + ResponsesAPIRequestUtils._wrap_encrypted_content_with_model_id( + encrypted_content, model_id + ) ) # Also encode the ID if present if item_id and isinstance(item_id, str): diff --git a/litellm/router.py b/litellm/router.py index a58b3ce25e1..5ba1a2270b0 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -5315,9 +5315,9 @@ class Router: e, (litellm.ContextWindowExceededError, litellm.ContentPolicyViolationError), ) - _request_team_id: Optional[str] = ( - kwargs.get("metadata", {}) or {} - ).get("user_api_key_team_id") + _request_team_id: Optional[str] = (kwargs.get("metadata", {}) or {}).get( + "user_api_key_team_id" + ) all_deployments = self._get_all_deployments( model_name=original_model_group, team_id=_request_team_id ) diff --git a/litellm/router_strategy/auto_router/auto_router.py b/litellm/router_strategy/auto_router/auto_router.py index 6a786115193..4ead7225abc 100644 --- a/litellm/router_strategy/auto_router/auto_router.py +++ b/litellm/router_strategy/auto_router/auto_router.py @@ -1,6 +1,7 @@ """ Auto-Routing Strategy that works with a Semantic Router Config """ + from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union from litellm._logging import verbose_router_logger diff --git a/litellm/router_strategy/base_routing_strategy.py b/litellm/router_strategy/base_routing_strategy.py index 6e410ef14a3..885798d706c 100644 --- a/litellm/router_strategy/base_routing_strategy.py +++ b/litellm/router_strategy/base_routing_strategy.py @@ -25,9 +25,9 @@ class BaseRoutingStrategy(ABC): if should_batch_redis_writes: self.setup_sync_task(default_sync_interval) - self.in_memory_keys_to_update: set[ - str - ] = set() # Set with max size of 1000 keys + self.in_memory_keys_to_update: set[str] = ( + set() + ) # Set with max size of 1000 keys def setup_sync_task(self, default_sync_interval: Optional[Union[int, float]]): """Setup the sync task in a way that's compatible with FastAPI""" diff --git a/litellm/router_strategy/budget_limiter.py b/litellm/router_strategy/budget_limiter.py index 64dc5fe4741..94231d13df4 100644 --- a/litellm/router_strategy/budget_limiter.py +++ b/litellm/router_strategy/budget_limiter.py @@ -100,9 +100,9 @@ class RouterBudgetLimiting(CustomLogger): self.dual_cache = dual_cache self.redis_increment_operation_queue: List[RedisPipelineIncrementOperation] = [] asyncio.create_task(self.periodic_sync_in_memory_spend_with_redis()) - self.provider_budget_config: Optional[ - GenericBudgetConfigType - ] = provider_budget_config + self.provider_budget_config: Optional[GenericBudgetConfigType] = ( + provider_budget_config + ) self.deployment_budget_config: Optional[GenericBudgetConfigType] = None self.tag_budget_config: Optional[GenericBudgetConfigType] = None self._init_provider_budgets() diff --git a/litellm/router_strategy/complexity_router/complexity_router.py b/litellm/router_strategy/complexity_router/complexity_router.py index 29bed360fab..e51249b1cb1 100644 --- a/litellm/router_strategy/complexity_router/complexity_router.py +++ b/litellm/router_strategy/complexity_router/complexity_router.py @@ -8,6 +8,7 @@ No external API calls - all scoring is local and <1ms. Inspired by ClawRouter: https://github.com/BlockRunAI/ClawRouter """ + import re from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union diff --git a/litellm/router_strategy/complexity_router/evals/eval_complexity_router.py b/litellm/router_strategy/complexity_router/evals/eval_complexity_router.py index a361d95a0ac..939ecdd2d22 100644 --- a/litellm/router_strategy/complexity_router/evals/eval_complexity_router.py +++ b/litellm/router_strategy/complexity_router/evals/eval_complexity_router.py @@ -4,6 +4,7 @@ Evaluation suite for the ComplexityRouter. Tests the router's ability to correctly classify prompts into complexity tiers. Run with: python -m litellm.router_strategy.complexity_router.evals.eval_complexity_router """ + import os # Add parent to path for imports @@ -273,16 +274,20 @@ def run_eval() -> Tuple[int, int, List[dict]]: { "case": i, "description": case.description, - "prompt": case.prompt[:80] + "..." - if len(case.prompt) > 80 - else case.prompt, + "prompt": ( + case.prompt[:80] + "..." + if len(case.prompt) > 80 + else case.prompt + ), "expected": case.expected_tier.value, "actual": tier.value, "score": round(score, 3), "signals": signals, - "acceptable": [t.value for t in case.acceptable_tiers] - if case.acceptable_tiers - else None, + "acceptable": ( + [t.value for t in case.acceptable_tiers] + if case.acceptable_tiers + else None + ), } ) diff --git a/litellm/router_utils/common_utils.py b/litellm/router_utils/common_utils.py index 7530247ce75..bef42e23848 100644 --- a/litellm/router_utils/common_utils.py +++ b/litellm/router_utils/common_utils.py @@ -32,9 +32,9 @@ def add_model_file_id_mappings( model_file_id_mapping = {} if isinstance(healthy_deployments, list): for deployment, response in zip(healthy_deployments, responses): - model_file_id_mapping[ - deployment.get("model_info", {}).get("id") - ] = response.id + model_file_id_mapping[deployment.get("model_info", {}).get("id")] = ( + response.id + ) elif isinstance(healthy_deployments, dict): for model_id, file_id in healthy_deployments.items(): model_file_id_mapping[model_id] = file_id diff --git a/litellm/router_utils/cooldown_callbacks.py b/litellm/router_utils/cooldown_callbacks.py index 32777a1dd4d..343328dacf3 100644 --- a/litellm/router_utils/cooldown_callbacks.py +++ b/litellm/router_utils/cooldown_callbacks.py @@ -59,9 +59,9 @@ async def router_cooldown_event_callback( pass # get the prometheus logger from in memory loggers - prometheusLogger: Optional[ - PrometheusLogger - ] = _get_prometheus_logger_from_callbacks() + prometheusLogger: Optional[PrometheusLogger] = ( + _get_prometheus_logger_from_callbacks() + ) if prometheusLogger is not None: prometheusLogger.set_deployment_complete_outage( diff --git a/litellm/router_utils/pattern_match_deployments.py b/litellm/router_utils/pattern_match_deployments.py index 69d6ab9b6e2..17b453d6031 100644 --- a/litellm/router_utils/pattern_match_deployments.py +++ b/litellm/router_utils/pattern_match_deployments.py @@ -105,11 +105,13 @@ class PatternMatchRouter: new_deployments = [] for deployment in deployments: new_deployment = copy.deepcopy(deployment) - new_deployment["litellm_params"][ - "model" - ] = PatternMatchRouter.set_deployment_model_name( - matched_pattern=matched_pattern, - litellm_deployment_litellm_model=deployment["litellm_params"]["model"], + new_deployment["litellm_params"]["model"] = ( + PatternMatchRouter.set_deployment_model_name( + matched_pattern=matched_pattern, + litellm_deployment_litellm_model=deployment["litellm_params"][ + "model" + ], + ) ) new_deployments.append(new_deployment) diff --git a/litellm/router_utils/search_api_router.py b/litellm/router_utils/search_api_router.py index 491a25e58ef..a26aa7e71ee 100644 --- a/litellm/router_utils/search_api_router.py +++ b/litellm/router_utils/search_api_router.py @@ -121,9 +121,9 @@ class SearchAPIRouter: ) # Set up kwargs for the fallback system - kwargs[ - "model" - ] = search_tool_name # Use model field for compatibility with fallback system + kwargs["model"] = ( + search_tool_name # Use model field for compatibility with fallback system + ) kwargs["original_generic_function"] = original_function # Bind router_instance to the helper method using partial kwargs["original_function"] = partial( diff --git a/litellm/search/__init__.py b/litellm/search/__init__.py index a3ebb3d870b..51f311618e3 100644 --- a/litellm/search/__init__.py +++ b/litellm/search/__init__.py @@ -1,6 +1,7 @@ """ LiteLLM Search API module. """ + from litellm.search.cost_calculator import search_provider_cost_per_query from litellm.search.main import asearch, search diff --git a/litellm/search/cost_calculator.py b/litellm/search/cost_calculator.py index 9821c12ae49..841c003dff3 100644 --- a/litellm/search/cost_calculator.py +++ b/litellm/search/cost_calculator.py @@ -1,6 +1,7 @@ """ Cost calculation for search providers. """ + from typing import Optional, Tuple from litellm.utils import get_model_info diff --git a/litellm/search/main.py b/litellm/search/main.py index 6b2c837fd55..7711dee6e54 100644 --- a/litellm/search/main.py +++ b/litellm/search/main.py @@ -1,6 +1,7 @@ """ Main Search function for LiteLLM. """ + import asyncio import contextvars from functools import partial @@ -242,10 +243,10 @@ def search( raise ValueError("All items in query list must be strings") # Get provider config - search_provider_config: Optional[ - BaseSearchConfig - ] = ProviderConfigManager.get_provider_search_config( - provider=SearchProviders(search_provider), + search_provider_config: Optional[BaseSearchConfig] = ( + ProviderConfigManager.get_provider_search_config( + provider=SearchProviders(search_provider), + ) ) if search_provider_config is None: diff --git a/litellm/secret_managers/secret_manager_handler.py b/litellm/secret_managers/secret_manager_handler.py index 0b16f7e10ad..4ff94d18eff 100644 --- a/litellm/secret_managers/secret_manager_handler.py +++ b/litellm/secret_managers/secret_manager_handler.py @@ -3,6 +3,7 @@ Secret Manager Handler Handles retrieving secrets from different secret management systems. """ + import base64 import os from typing import Any, Optional @@ -162,9 +163,11 @@ def get_secret_from_manager( # noqa: PLR0915 if isinstance(client, CustomSecretManager): secret = client.sync_read_secret( secret_name=secret_name, - optional_params=key_management_settings.model_dump() - if key_management_settings - else None, + optional_params=( + key_management_settings.model_dump() + if key_management_settings + else None + ), ) if secret is None: raise ValueError( diff --git a/litellm/setup_wizard.py b/litellm/setup_wizard.py index 3718655b318..77054fa14cf 100644 --- a/litellm/setup_wizard.py +++ b/litellm/setup_wizard.py @@ -433,9 +433,9 @@ class SetupWizard: f" {blue('❯')} Azure deployment name {grey('(e.g. my-gpt4o)')}: " ) if deployment: - env_vars[ - f"_LITELLM_AZURE_DEPLOYMENT_{p['id'].upper()}" - ] = deployment + env_vars[f"_LITELLM_AZURE_DEPLOYMENT_{p['id'].upper()}"] = ( + deployment + ) # Store the key returned by validation — may be a re-entered replacement env_vars[p["env_key"]] = SetupWizard._validate_and_report(p, key) diff --git a/litellm/skills/main.py b/litellm/skills/main.py index c6ef6f28fb6..3ff0f52c641 100644 --- a/litellm/skills/main.py +++ b/litellm/skills/main.py @@ -173,10 +173,10 @@ def create_skill( ) # Get provider config for external providers (Anthropic, etc.) - skills_api_provider_config: Optional[ - BaseSkillsAPIConfig - ] = ProviderConfigManager.get_provider_skills_api_config( - provider=litellm.LlmProviders(custom_llm_provider), + skills_api_provider_config: Optional[BaseSkillsAPIConfig] = ( + ProviderConfigManager.get_provider_skills_api_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if skills_api_provider_config is None: @@ -354,10 +354,10 @@ def list_skills( ) # Get provider config for external providers (Anthropic, etc.) - skills_api_provider_config: Optional[ - BaseSkillsAPIConfig - ] = ProviderConfigManager.get_provider_skills_api_config( - provider=litellm.LlmProviders(custom_llm_provider), + skills_api_provider_config: Optional[BaseSkillsAPIConfig] = ( + ProviderConfigManager.get_provider_skills_api_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if skills_api_provider_config is None: @@ -529,10 +529,10 @@ def get_skill( ) # Get provider config for external providers (Anthropic, etc.) - skills_api_provider_config: Optional[ - BaseSkillsAPIConfig - ] = ProviderConfigManager.get_provider_skills_api_config( - provider=litellm.LlmProviders(custom_llm_provider), + skills_api_provider_config: Optional[BaseSkillsAPIConfig] = ( + ProviderConfigManager.get_provider_skills_api_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if skills_api_provider_config is None: @@ -696,10 +696,10 @@ def delete_skill( ) # Get provider config for external providers (Anthropic, etc.) - skills_api_provider_config: Optional[ - BaseSkillsAPIConfig - ] = ProviderConfigManager.get_provider_skills_api_config( - provider=litellm.LlmProviders(custom_llm_provider), + skills_api_provider_config: Optional[BaseSkillsAPIConfig] = ( + ProviderConfigManager.get_provider_skills_api_config( + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if skills_api_provider_config is None: diff --git a/litellm/types/integrations/datadog_llm_obs.py b/litellm/types/integrations/datadog_llm_obs.py index 1f281e93e8c..4ea5ed66b87 100644 --- a/litellm/types/integrations/datadog_llm_obs.py +++ b/litellm/types/integrations/datadog_llm_obs.py @@ -3,6 +3,7 @@ Payloads for Datadog LLM Observability Service (LLMObs) API Reference: https://docs.datadoghq.com/llm_observability/setup/api/?tab=example#api-standards """ + from typing import Any, Dict, List, Literal, Optional from typing_extensions import TypedDict diff --git a/litellm/types/integrations/prometheus.py b/litellm/types/integrations/prometheus.py index 0d1501664b9..005121e8085 100644 --- a/litellm/types/integrations/prometheus.py +++ b/litellm/types/integrations/prometheus.py @@ -676,9 +676,9 @@ class PrometheusMetricLabels: litellm_managed_batch_created_total = _batch_user_labels - litellm_managed_file_size_bytes: List[ - str - ] = [] # labels: purpose, file_type, model, api_provider, user (custom) + litellm_managed_file_size_bytes: List[str] = ( + [] + ) # labels: purpose, file_type, model, api_provider, user (custom) litellm_managed_batch_duration_seconds = [ UserAPIKeyLabelNames.v1_LITELLM_MODEL_NAME.value, @@ -687,9 +687,9 @@ class PrometheusMetricLabels: litellm_managed_file_created_total = _batch_user_labels - litellm_managed_file_deleted_total: List[ - str - ] = [] # only "result" label, added at metric creation + litellm_managed_file_deleted_total: List[str] = ( + [] + ) # only "result" label, added at metric creation litellm_check_batch_cost_jobs_polled: List[str] = [] diff --git a/litellm/types/llms/bedrock.py b/litellm/types/llms/bedrock.py index 54237dfb37a..6830d95d36f 100644 --- a/litellm/types/llms/bedrock.py +++ b/litellm/types/llms/bedrock.py @@ -203,7 +203,9 @@ class ConverseResponseBlock(TypedDict, total=False): str ] # end_turn | tool_use | max_tokens | stop_sequence | content_filtered usage: Required[ConverseTokenUsageBlock] - serviceTier: ServiceTierBlock # Optional - only present when serviceTier was sent in request + serviceTier: ( + ServiceTierBlock # Optional - only present when serviceTier was sent in request + ) class ToolJsonSchemaBlock(TypedDict, total=False): diff --git a/litellm/types/llms/openai.py b/litellm/types/llms/openai.py index 80b6190db8f..2fd0c4ea970 100644 --- a/litellm/types/llms/openai.py +++ b/litellm/types/llms/openai.py @@ -985,12 +985,12 @@ class OpenAIChatCompletionChunk(ChatCompletionChunk): class Hyperparameters(BaseModel): batch_size: Optional[Union[str, int]] = None # "Number of examples in each batch." - learning_rate_multiplier: Optional[ - Union[str, float] - ] = None # Scaling factor for the learning rate - n_epochs: Optional[ - Union[str, int] - ] = None # "The number of epochs to train the model for" + learning_rate_multiplier: Optional[Union[str, float]] = ( + None # Scaling factor for the learning rate + ) + n_epochs: Optional[Union[str, int]] = ( + None # "The number of epochs to train the model for" + ) model_config = {"extra": "allow"} @@ -1019,18 +1019,18 @@ class FineTuningJobCreate(BaseModel): model: str # "The name of the model to fine-tune." training_file: str # "The ID of an uploaded file that contains training data." - hyperparameters: Optional[ - Hyperparameters - ] = None # "The hyperparameters used for the fine-tuning job." - suffix: Optional[ - str - ] = None # "A string of up to 18 characters that will be added to your fine-tuned model name." - validation_file: Optional[ - str - ] = None # "The ID of an uploaded file that contains validation data." - integrations: Optional[ - List[str] - ] = None # "A list of integrations to enable for your fine-tuning job." + hyperparameters: Optional[Hyperparameters] = ( + None # "The hyperparameters used for the fine-tuning job." + ) + suffix: Optional[str] = ( + None # "A string of up to 18 characters that will be added to your fine-tuned model name." + ) + validation_file: Optional[str] = ( + None # "The ID of an uploaded file that contains validation data." + ) + integrations: Optional[List[str]] = ( + None # "A list of integrations to enable for your fine-tuning job." + ) seed: Optional[int] = None # "The seed controls the reproducibility of the job." diff --git a/litellm/types/management_endpoints/cache_settings_endpoints.py b/litellm/types/management_endpoints/cache_settings_endpoints.py index fd68f43e7b0..6d8cb63a15c 100644 --- a/litellm/types/management_endpoints/cache_settings_endpoints.py +++ b/litellm/types/management_endpoints/cache_settings_endpoints.py @@ -13,14 +13,14 @@ class CacheSettingsField(BaseModel): field_value: Any field_description: str field_default: Any = None - options: Optional[ - List[str] - ] = None # For fields with predefined options/enum values + options: Optional[List[str]] = ( + None # For fields with predefined options/enum values + ) ui_field_name: str # User-friendly display name link: Optional[str] = None # Documentation link for the field - redis_type: Optional[ - str - ] = None # Which Redis type this field applies to (node, cluster, sentinel) + redis_type: Optional[str] = ( + None # Which Redis type this field applies to (node, cluster, sentinel) + ) # Redis type descriptions diff --git a/litellm/types/management_endpoints/router_settings_endpoints.py b/litellm/types/management_endpoints/router_settings_endpoints.py index 4f09b7da853..4c2abfdbca8 100644 --- a/litellm/types/management_endpoints/router_settings_endpoints.py +++ b/litellm/types/management_endpoints/router_settings_endpoints.py @@ -75,9 +75,9 @@ class RouterSettingsField(BaseModel): field_value: Any field_description: str field_default: Any = None - options: Optional[ - List[str] - ] = None # For fields with predefined options/enum values + options: Optional[List[str]] = ( + None # For fields with predefined options/enum values + ) ui_field_name: str # User-friendly display name link: Optional[str] = None # Documentation link for the field diff --git a/litellm/types/mcp_server/mcp_server_manager.py b/litellm/types/mcp_server/mcp_server_manager.py index db7657a0174..e32cb8089e7 100644 --- a/litellm/types/mcp_server/mcp_server_manager.py +++ b/litellm/types/mcp_server/mcp_server_manager.py @@ -28,19 +28,19 @@ class MCPServer(BaseModel): auth_type: Optional[MCPAuthType] = None authentication_token: Optional[str] = None mcp_info: Optional[MCPInfo] = None - extra_headers: Optional[ - List[str] - ] = None # allow admin to specify which headers to forward from client to the MCP server + extra_headers: Optional[List[str]] = ( + None # allow admin to specify which headers to forward from client to the MCP server + ) allowed_tools: Optional[List[str]] = None disallowed_tools: Optional[List[str]] = None tool_name_to_display_name: Optional[Dict[str, str]] = None tool_name_to_description: Optional[Dict[str, str]] = None - allowed_params: Optional[ - Dict[str, List[str]] - ] = None # map of tool names to allowed parameter lists - static_headers: Optional[ - Dict[str, str] - ] = None # static headers to forward to the MCP server + allowed_params: Optional[Dict[str, List[str]]] = ( + None # map of tool names to allowed parameter lists + ) + static_headers: Optional[Dict[str, str]] = ( + None # static headers to forward to the MCP server + ) # OAuth-specific fields client_id: Optional[str] = None client_secret: Optional[str] = None diff --git a/litellm/types/prompts/init_prompts.py b/litellm/types/prompts/init_prompts.py index 838271a2f31..eefd3d3dc87 100644 --- a/litellm/types/prompts/init_prompts.py +++ b/litellm/types/prompts/init_prompts.py @@ -73,9 +73,9 @@ class PromptTemplateBase(BaseModel): class PromptInfoResponse(BaseModel): prompt_spec: PromptSpec raw_prompt_template: Optional[PromptTemplateBase] = None - environments: Optional[ - List[str] - ] = None # All environments this prompt is deployed to + environments: Optional[List[str]] = ( + None # All environments this prompt is deployed to + ) class ListPromptsResponse(BaseModel): diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py index c87086bdce2..94f219a5fc6 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/generic_guardrail_api.py @@ -60,9 +60,9 @@ class GenericGuardrailAPIRequest(BaseModel): input_type: Literal["request", "response"] litellm_call_id: Optional[str] = None # the call id of the individual LLM call - litellm_trace_id: Optional[ - str - ] = None # the trace id of the LLM call - useful if there are multiple LLM calls for the same conversation + litellm_trace_id: Optional[str] = ( + None # the trace id of the LLM call - useful if there are multiple LLM calls for the same conversation + ) structured_messages: Optional[List[AllMessageValues]] = None images: Optional[List[str]] = None tools: Optional[List[ChatCompletionToolParam]] = None diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/openai/openai_moderation.py b/litellm/types/proxy/guardrails/guardrail_hooks/openai/openai_moderation.py index ee67626967e..7d81cf9fe03 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/openai/openai_moderation.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/openai/openai_moderation.py @@ -8,11 +8,11 @@ from ..base import GuardrailConfigModel class BaseOpenAIModerationGuardrailConfigModel(GuardrailConfigModel): """Base configuration model for the OpenAI Moderation guardrail""" - model: Optional[ - Literal["omni-moderation-latest", "text-moderation-latest"] - ] = Field( - default="omni-moderation-latest", - description="The OpenAI moderation model to use. 'omni-moderation-latest' supports more categorization options and multi-modal inputs. Defaults to 'omni-moderation-latest'.", + model: Optional[Literal["omni-moderation-latest", "text-moderation-latest"]] = ( + Field( + default="omni-moderation-latest", + description="The OpenAI moderation model to use. 'omni-moderation-latest' supports more categorization options and multi-modal inputs. Defaults to 'omni-moderation-latest'.", + ) ) diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/pillar.py b/litellm/types/proxy/guardrails/guardrail_hooks/pillar.py index 92e76d6693a..17391c070d0 100644 --- a/litellm/types/proxy/guardrails/guardrail_hooks/pillar.py +++ b/litellm/types/proxy/guardrails/guardrail_hooks/pillar.py @@ -1,6 +1,7 @@ """ Pillar Security Guardrail Config Model """ + from typing import Optional from pydantic import BaseModel, Field diff --git a/litellm/types/proxy/management_endpoints/internal_user_endpoints.py b/litellm/types/proxy/management_endpoints/internal_user_endpoints.py index 4770877daba..6023094a920 100644 --- a/litellm/types/proxy/management_endpoints/internal_user_endpoints.py +++ b/litellm/types/proxy/management_endpoints/internal_user_endpoints.py @@ -25,13 +25,13 @@ class UserListResponse(BaseModel): class BulkUpdateUserRequest(BaseModel): """Request for bulk user updates""" - users: Optional[ - List[UpdateUserRequest] - ] = None # List of specific user update requests + users: Optional[List[UpdateUserRequest]] = ( + None # List of specific user update requests + ) all_users: Optional[bool] = False # Flag to update all users - user_updates: Optional[ - UpdateUserRequestNoUserIDorEmail - ] = None # Updates to apply to all users when all_users=True + user_updates: Optional[UpdateUserRequestNoUserIDorEmail] = ( + None # Updates to apply to all users when all_users=True + ) @field_validator("users", "all_users", "user_updates") @classmethod diff --git a/litellm/types/proxy/management_endpoints/model_management_endpoints.py b/litellm/types/proxy/management_endpoints/model_management_endpoints.py index be4d730e93e..bbbfc0de9f8 100644 --- a/litellm/types/proxy/management_endpoints/model_management_endpoints.py +++ b/litellm/types/proxy/management_endpoints/model_management_endpoints.py @@ -21,12 +21,12 @@ class UpdateUsefulLinksRequest(BaseModel): class NewModelGroupRequest(BaseModel): access_group: str # The access group name (e.g., "production-models") - model_names: Optional[ - List[str] - ] = None # Existing model groups to include - tags ALL deployments for each name - model_ids: Optional[ - List[str] - ] = None # Specific deployment IDs to tag (more precise than model_names) + model_names: Optional[List[str]] = ( + None # Existing model groups to include - tags ALL deployments for each name + ) + model_ids: Optional[List[str]] = ( + None # Specific deployment IDs to tag (more precise than model_names) + ) class NewModelGroupResponse(BaseModel): @@ -37,12 +37,12 @@ class NewModelGroupResponse(BaseModel): class UpdateModelGroupRequest(BaseModel): - model_names: Optional[ - List[str] - ] = None # Updated list of model groups to include - tags ALL deployments for each name - model_ids: Optional[ - List[str] - ] = None # Specific deployment IDs to tag (more precise than model_names) + model_names: Optional[List[str]] = ( + None # Updated list of model groups to include - tags ALL deployments for each name + ) + model_ids: Optional[List[str]] = ( + None # Specific deployment IDs to tag (more precise than model_names) + ) class DeleteModelGroupResponse(BaseModel): diff --git a/litellm/types/rerank.py b/litellm/types/rerank.py index fb6dae0d1df..d2c252a1e92 100644 --- a/litellm/types/rerank.py +++ b/litellm/types/rerank.py @@ -59,9 +59,9 @@ class RerankResponseResult(TypedDict, total=False): class RerankResponse(BaseModel): id: Optional[str] = None - results: Optional[ - List[RerankResponseResult] - ] = None # Contains index and relevance_score + results: Optional[List[RerankResponseResult]] = ( + None # Contains index and relevance_score + ) meta: Optional[RerankResponseMeta] = None # Contains api_version and billed_units # Define private attributes using PrivateAttr diff --git a/litellm/types/router.py b/litellm/types/router.py index 4257628e7cb..2e867e93a7c 100644 --- a/litellm/types/router.py +++ b/litellm/types/router.py @@ -95,16 +95,18 @@ class ModelInfo(BaseModel): id: Optional[ str ] # Allow id to be optional on input, but it will always be present as a str in the model instance - db_model: bool = False # used for proxy - to separate models which are stored in the db vs. config. + db_model: bool = ( + False # used for proxy - to separate models which are stored in the db vs. config. + ) updated_at: Optional[datetime.datetime] = None updated_by: Optional[str] = None created_at: Optional[datetime.datetime] = None created_by: Optional[str] = None - base_model: Optional[ - str - ] = None # specify if the base model is azure/gpt-3.5-turbo etc for accurate cost tracking + base_model: Optional[str] = ( + None # specify if the base model is azure/gpt-3.5-turbo etc for accurate cost tracking + ) tier: Optional[Literal["free", "paid"]] = None """ @@ -173,12 +175,12 @@ class GenericLiteLLMParams(CredentialLiteLLMParams, CustomPricingLiteLLMParams): custom_llm_provider: Optional[str] = None tpm: Optional[int] = None rpm: Optional[int] = None - timeout: Optional[ - Union[float, str, httpx.Timeout] - ] = None # if str, pass in as os.environ/ - stream_timeout: Optional[ - Union[float, str] - ] = None # timeout when making stream=True calls, if str, pass in as os.environ/ + timeout: Optional[Union[float, str, httpx.Timeout]] = ( + None # if str, pass in as os.environ/ + ) + stream_timeout: Optional[Union[float, str]] = ( + None # timeout when making stream=True calls, if str, pass in as os.environ/ + ) max_retries: Optional[int] = None organization: Optional[str] = None # for openai orgs configurable_clientside_auth_params: CONFIGURABLE_CLIENTSIDE_AUTH_PARAMS = None diff --git a/litellm/types/search.py b/litellm/types/search.py index bbac1237a19..e94477fe1f6 100644 --- a/litellm/types/search.py +++ b/litellm/types/search.py @@ -3,6 +3,7 @@ LiteLLM Search API Types This module defines types for the unified search API across different providers. """ + from typing import List, Optional from typing_extensions import Required, TypedDict diff --git a/litellm/types/videos/utils.py b/litellm/types/videos/utils.py index bf51fdda370..afa368ecdc3 100644 --- a/litellm/types/videos/utils.py +++ b/litellm/types/videos/utils.py @@ -4,6 +4,7 @@ Utility functions for video ID encoding/decoding with provider information. Follows the pattern used in responses/utils.py for consistency. Format: vid_{base64_encoded_string} """ + import base64 from typing import Optional, Tuple diff --git a/litellm/utils.py b/litellm/utils.py index 9d17b001729..91bb6175ee3 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -783,9 +783,9 @@ def function_setup( # noqa: PLR0915 coroutine_checker = get_coroutine_checker_fn() ## DYNAMIC CALLBACKS ## - dynamic_callbacks: Optional[ - List[Union[str, Callable, "CustomLogger"]] - ] = kwargs.pop("callbacks", None) + dynamic_callbacks: Optional[List[Union[str, Callable, "CustomLogger"]]] = ( + kwargs.pop("callbacks", None) + ) all_callbacks = get_dynamic_callbacks(dynamic_callbacks=dynamic_callbacks) if len(all_callbacks) > 0: @@ -1691,9 +1691,9 @@ def client(original_function): # noqa: PLR0915 exception=e, retry_policy=kwargs.get("retry_policy"), ) - kwargs[ - "retry_policy" - ] = reset_retry_policy() # prevent infinite loops + kwargs["retry_policy"] = ( + reset_retry_policy() + ) # prevent infinite loops litellm.num_retries = ( None # set retries to None to prevent infinite loops ) @@ -1740,9 +1740,9 @@ def client(original_function): # noqa: PLR0915 exception=e, retry_policy=kwargs.get("retry_policy"), ) - kwargs[ - "retry_policy" - ] = reset_retry_policy() # prevent infinite loops + kwargs["retry_policy"] = ( + reset_retry_policy() + ) # prevent infinite loops litellm.num_retries = ( None # set retries to None to prevent infinite loops ) @@ -3771,10 +3771,10 @@ def pre_process_non_default_params( if "response_format" in non_default_params: if provider_config is not None: - non_default_params[ - "response_format" - ] = provider_config.get_json_schema_from_pydantic_object( - response_format=non_default_params["response_format"] + non_default_params["response_format"] = ( + provider_config.get_json_schema_from_pydantic_object( + response_format=non_default_params["response_format"] + ) ) else: non_default_params["response_format"] = type_to_response_format_param( @@ -3903,16 +3903,16 @@ def pre_process_optional_params( True # so that main.py adds the function call to the prompt ) if "tools" in non_default_params: - optional_params[ - "functions_unsupported_model" - ] = non_default_params.pop("tools") + optional_params["functions_unsupported_model"] = ( + non_default_params.pop("tools") + ) non_default_params.pop( "tool_choice", None ) # causes ollama requests to hang elif "functions" in non_default_params: - optional_params[ - "functions_unsupported_model" - ] = non_default_params.pop("functions") + optional_params["functions_unsupported_model"] = ( + non_default_params.pop("functions") + ) elif ( litellm.add_function_to_prompt ): # if user opts to add it to prompt instead @@ -4893,9 +4893,7 @@ def _get_order_filtered_deployments( ) -> List: if target_order is not None: filtered = [ - d - for d in healthy_deployments - if _get_deployment_order(d) == target_order + d for d in healthy_deployments if _get_deployment_order(d) == target_order ] if filtered: return filtered @@ -7567,9 +7565,9 @@ class ModelResponseIterator: if convert_to_delta is True: _stream_response = ModelResponseStream() _stream_response.choices[0].delta.content = model_response.choices[0].message.content # type: ignore - self.model_response: Union[ - ModelResponse, ModelResponseStream - ] = _stream_response + self.model_response: Union[ModelResponse, ModelResponseStream] = ( + _stream_response + ) else: self.model_response = model_response self.is_done = False diff --git a/litellm/vector_stores/vector_store_registry.py b/litellm/vector_stores/vector_store_registry.py index 2596f968a06..1fd95b16309 100644 --- a/litellm/vector_stores/vector_store_registry.py +++ b/litellm/vector_stores/vector_store_registry.py @@ -24,9 +24,9 @@ class VectorStoreIndexRegistry: def __init__( self, vector_store_indexes: List[LiteLLM_ManagedVectorStoreIndex] = [] ): - self.vector_store_indexes: List[ - LiteLLM_ManagedVectorStoreIndex - ] = vector_store_indexes + self.vector_store_indexes: List[LiteLLM_ManagedVectorStoreIndex] = ( + vector_store_indexes + ) def get_vector_store_indexes(self) -> List[LiteLLM_ManagedVectorStoreIndex]: """ diff --git a/litellm/videos/main.py b/litellm/videos/main.py index cd61293cd1c..a61fe99d584 100644 --- a/litellm/videos/main.py +++ b/litellm/videos/main.py @@ -174,7 +174,10 @@ def video_generation( # noqa: PLR0915 extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[VideoObject, Coroutine[Any, Any, VideoObject],]: +) -> Union[ + VideoObject, + Coroutine[Any, Any, VideoObject], +]: """ Maps the https://api.openai.com/v1/videos endpoint. @@ -203,11 +206,11 @@ def video_generation( # noqa: PLR0915 ) # get provider config - video_generation_provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=model, - provider=litellm.LlmProviders(custom_llm_provider), + video_generation_provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=model, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if video_generation_provider_config is None: @@ -286,7 +289,10 @@ def video_content( extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[bytes, Coroutine[Any, Any, bytes],]: +) -> Union[ + bytes, + Coroutine[Any, Any, bytes], +]: """ Download video content from OpenAI's video API. @@ -331,11 +337,11 @@ def video_content( litellm_params = GenericLiteLLMParams(**kwargs) # get provider config - video_provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + video_provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if video_provider_config is None: @@ -574,7 +580,10 @@ def video_remix( # noqa: PLR0915 extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[VideoObject, Coroutine[Any, Any, VideoObject],]: +) -> Union[ + VideoObject, + Coroutine[Any, Any, VideoObject], +]: """ Maps the https://api.openai.com/v1/videos/{video_id}/remix endpoint. @@ -604,11 +613,11 @@ def video_remix( # noqa: PLR0915 litellm_params = GenericLiteLLMParams(**kwargs) # get provider config - video_remix_provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + video_remix_provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if video_remix_provider_config is None: @@ -793,7 +802,10 @@ def video_list( # noqa: PLR0915 extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[List[VideoObject], Coroutine[Any, Any, List[VideoObject]],]: +) -> Union[ + List[VideoObject], + Coroutine[Any, Any, List[VideoObject]], +]: """ Maps the https://api.openai.com/v1/videos endpoint. @@ -820,11 +832,11 @@ def video_list( # noqa: PLR0915 litellm_params = GenericLiteLLMParams(**kwargs) # get provider config - video_list_provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + video_list_provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if video_list_provider_config is None: @@ -991,7 +1003,10 @@ def video_status( # noqa: PLR0915 extra_query: Optional[Dict[str, Any]] = None, extra_body: Optional[Dict[str, Any]] = None, **kwargs, -) -> Union[VideoObject, Coroutine[Any, Any, VideoObject],]: +) -> Union[ + VideoObject, + Coroutine[Any, Any, VideoObject], +]: """ Retrieve video status from OpenAI's video API. @@ -1043,11 +1058,11 @@ def video_status( # noqa: PLR0915 litellm_params = GenericLiteLLMParams(**kwargs) # get provider config - video_status_provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + video_status_provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if video_status_provider_config is None: @@ -1186,11 +1201,11 @@ def video_create_character( litellm_params = GenericLiteLLMParams(**kwargs) - provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if provider_config is None: @@ -1315,11 +1330,11 @@ def video_get_character( litellm_params = GenericLiteLLMParams(**kwargs) - provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if provider_config is None: @@ -1447,11 +1462,11 @@ def video_edit( litellm_params = GenericLiteLLMParams(**kwargs) - provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if provider_config is None: @@ -1582,11 +1597,11 @@ def video_extension( litellm_params = GenericLiteLLMParams(**kwargs) - provider_config: Optional[ - BaseVideoConfig - ] = ProviderConfigManager.get_provider_video_config( - model=None, - provider=litellm.LlmProviders(custom_llm_provider), + provider_config: Optional[BaseVideoConfig] = ( + ProviderConfigManager.get_provider_video_config( + model=None, + provider=litellm.LlmProviders(custom_llm_provider), + ) ) if provider_config is None: