Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deployment_id for cooldown_handlers.py #7404

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions litellm/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,19 +774,19 @@ def _completion(
@overload
async def acompletion(
self, model: str, messages: List[Dict[str, str]], stream: Literal[True], **kwargs
) -> CustomStreamWrapper:
) -> CustomStreamWrapper:
...

@overload
async def acompletion(
self, model: str, messages: List[Dict[str, str]], stream: Literal[False] = False, **kwargs
) -> ModelResponse:
) -> ModelResponse:
...

@overload
async def acompletion(
self, model: str, messages: List[Dict[str, str]], stream: Union[Literal[True], Literal[False]] = False, **kwargs
) -> Union[CustomStreamWrapper, ModelResponse]:
) -> Union[CustomStreamWrapper, ModelResponse]:
...

# fmt: on
Expand Down Expand Up @@ -1284,13 +1284,13 @@ async def check_response(task: asyncio.Task):
@overload
async def schedule_acompletion(
self, model: str, messages: List[Dict[str, str]], priority: int, stream: Literal[False] = False, **kwargs
) -> ModelResponse:
) -> ModelResponse:
...

@overload
async def schedule_acompletion(
self, model: str, messages: List[Dict[str, str]], priority: int, stream: Literal[True], **kwargs
) -> CustomStreamWrapper:
) -> CustomStreamWrapper:
...

# fmt: on
Expand Down Expand Up @@ -3370,7 +3370,7 @@ def deployment_callback_on_failure(
litellm_router_instance=self,
exception_status=exception_status,
original_exception=exception,
deployment=deployment_id,
deployment_id=deployment_id,
time_to_cooldown=_time_to_cooldown,
) # setting deployment_id in cooldown deployments

Expand Down Expand Up @@ -3691,7 +3691,7 @@ async def async_routing_strategy_pre_call_checks(
litellm_router_instance=self,
exception_status=e.status_code,
original_exception=e,
deployment=deployment["model_info"]["id"],
deployment_id=deployment["model_info"]["id"],
time_to_cooldown=self.cooldown_time,
)
raise e
Expand Down
44 changes: 22 additions & 22 deletions litellm/router_utils/cooldown_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

def _should_run_cooldown_logic(
litellm_router_instance: LitellmRouter,
deployment: Optional[str],
deployment_id: Optional[str],
exception_status: Union[str, int],
original_exception: Any,
) -> bool:
Expand All @@ -46,33 +46,33 @@ def _should_run_cooldown_logic(

Does not run cooldown logic when:
- router.disable_cooldowns is True
- deployment is None
- deployment_id is None
- _is_cooldown_required() returns False
- deployment is in litellm_router_instance.provider_default_deployment_ids
- deployment_id is in litellm_router_instance.provider_default_deployment_ids
- exception_status is not one that should be immediately retried (e.g. 401)
"""
if litellm_router_instance.disable_cooldowns:
return False

if deployment is None:
if deployment_id is None:
return False

if not litellm_router_instance._is_cooldown_required(
model_id=deployment,
model_id=deployment_id,
exception_status=exception_status,
exception_str=str(original_exception),
):
return False

if deployment in litellm_router_instance.provider_default_deployment_ids:
if deployment_id in litellm_router_instance.provider_default_deployment_ids:
return False

return True


def _should_cooldown_deployment(
litellm_router_instance: LitellmRouter,
deployment: str,
deployment_id: str,
exception_status: Union[str, int],
original_exception: Any,
) -> bool:
Expand Down Expand Up @@ -102,10 +102,10 @@ def _should_cooldown_deployment(
is False
):
num_successes_this_minute = get_deployment_successes_for_current_minute(
litellm_router_instance=litellm_router_instance, deployment_id=deployment
litellm_router_instance=litellm_router_instance, deployment_id=deployment_id
)
num_fails_this_minute = get_deployment_failures_for_current_minute(
litellm_router_instance=litellm_router_instance, deployment_id=deployment
litellm_router_instance=litellm_router_instance, deployment_id=deployment_id
)

total_requests_this_minute = num_successes_this_minute + num_fails_this_minute
Expand All @@ -115,8 +115,8 @@ def _should_cooldown_deployment(
num_successes_this_minute + num_fails_this_minute
)
verbose_router_logger.debug(
"percent fails for deployment = %s, percent fails = %s, num successes = %s, num fails = %s",
deployment,
"percent fails for deployment_id = %s, percent fails = %s, num successes = %s, num fails = %s",
deployment_id,
percent_fails,
num_successes_this_minute,
num_fails_this_minute,
Expand All @@ -143,7 +143,7 @@ def _should_cooldown_deployment(
else:
return should_cooldown_based_on_allowed_fails_policy(
litellm_router_instance=litellm_router_instance,
deployment=deployment,
deployment_id=deployment_id,
original_exception=original_exception,
)

Expand All @@ -154,7 +154,7 @@ def _set_cooldown_deployments(
litellm_router_instance: LitellmRouter,
original_exception: Any,
exception_status: Union[str, int],
deployment: Optional[str] = None,
deployment_id: Optional[str] = None,
time_to_cooldown: Optional[float] = None,
) -> bool:
"""
Expand All @@ -170,25 +170,25 @@ def _set_cooldown_deployments(
"""
if (
_should_run_cooldown_logic(
litellm_router_instance, deployment, exception_status, original_exception
litellm_router_instance, deployment_id, exception_status, original_exception
)
is False
or deployment is None
or deployment_id is None
):
return False

exception_status_int = cast_exception_status_to_int(exception_status)

verbose_router_logger.debug(f"Attempting to add {deployment} to cooldown list")
verbose_router_logger.debug(f"Attempting to add {deployment_id} to cooldown list")
cooldown_time = litellm_router_instance.cooldown_time or 1
if time_to_cooldown is not None:
cooldown_time = time_to_cooldown

if _should_cooldown_deployment(
litellm_router_instance, deployment, exception_status, original_exception
litellm_router_instance, deployment_id, exception_status, original_exception
):
litellm_router_instance.cooldown_cache.add_deployment_to_cooldown(
model_id=deployment,
model_id=deployment_id,
original_exception=original_exception,
exception_status=exception_status_int,
cooldown_time=cooldown_time,
Expand All @@ -198,7 +198,7 @@ def _set_cooldown_deployments(
asyncio.create_task(
router_cooldown_event_callback(
litellm_router_instance=litellm_router_instance,
deployment_id=deployment,
deployment_id=deployment_id,
exception_status=exception_status,
cooldown_time=cooldown_time,
)
Expand Down Expand Up @@ -284,7 +284,7 @@ def _get_cooldown_deployments(

def should_cooldown_based_on_allowed_fails_policy(
litellm_router_instance: LitellmRouter,
deployment: str,
deployment_id: str,
original_exception: Any,
) -> bool:
"""
Expand All @@ -304,14 +304,14 @@ def should_cooldown_based_on_allowed_fails_policy(
litellm_router_instance.cooldown_time or DEFAULT_COOLDOWN_TIME_SECONDS
)

current_fails = litellm_router_instance.failed_calls.get_cache(key=deployment) or 0
current_fails = litellm_router_instance.failed_calls.get_cache(key=deployment_id) or 0
updated_fails = current_fails + 1

if updated_fails > allowed_fails:
return True
else:
litellm_router_instance.failed_calls.set_cache(
key=deployment, value=updated_fails, ttl=cooldown_time
key=deployment_id, value=updated_fails, ttl=cooldown_time
)

return False
Expand Down