From 18c2592f2b2155e723450400205e857af9167562 Mon Sep 17 00:00:00 2001 From: Emerson Gomes Date: Mon, 17 Mar 2025 19:19:20 -0500 Subject: [PATCH] fix redis serialization issue with Redis + lowest latency strategy --- litellm/router_strategy/lowest_latency.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/litellm/router_strategy/lowest_latency.py b/litellm/router_strategy/lowest_latency.py index b049c942642..0ec71116e29 100644 --- a/litellm/router_strategy/lowest_latency.py +++ b/litellm/router_strategy/lowest_latency.py @@ -124,7 +124,9 @@ class LowestLatencyLoggingHandler(CustomLogger): len(request_count_dict[id].get("latency", [])) < self.routing_args.max_latency_list_size ): - request_count_dict[id].setdefault("latency", []).append(final_value) + latency_value = final_value.total_seconds() if isinstance(final_value, timedelta) else final_value + request_count_dict[id].setdefault("latency", []).append(latency_value) + else: request_count_dict[id]["latency"] = request_count_dict[id][ "latency" @@ -332,7 +334,9 @@ class LowestLatencyLoggingHandler(CustomLogger): len(request_count_dict[id].get("latency", [])) < self.routing_args.max_latency_list_size ): - request_count_dict[id].setdefault("latency", []).append(final_value) + latency_value = final_value.total_seconds() if isinstance(final_value, timedelta) else final_value + request_count_dict[id].setdefault("latency", []).append(latency_value) + else: request_count_dict[id]["latency"] = request_count_dict[id][ "latency" @@ -465,6 +469,8 @@ class LowestLatencyLoggingHandler(CustomLogger): total: float = 0.0 if ( request_kwargs is not None + and len(item_latency) > 0 + and isinstance(item_latency[0], timedelta) and request_kwargs.get("stream", None) is not None and request_kwargs["stream"] is True and len(item_ttft_latency) > 0 @@ -472,6 +478,9 @@ class LowestLatencyLoggingHandler(CustomLogger): for _call_latency in item_ttft_latency: if isinstance(_call_latency, float): total += _call_latency + elif len(item_latency) > 0 and isinstance(item_latency[0], timedelta): + for _call_latency in item_latency: + total += _call_latency.total_seconds() else: for _call_latency in item_latency: if isinstance(_call_latency, float):