From 0d3735f9c0ee8102d43dcba9b39f4028c22d59c2 Mon Sep 17 00:00:00 2001 From: JiangNan <1394485448@qq.com> Date: Tue, 10 Mar 2026 10:46:26 +0800 Subject: [PATCH] fix: handle month overflow in duration_in_seconds for multi-month durations (#23099) When value > 1 (e.g., "2mo") and current_month + value > 12, target_month exceeds valid range (1-12), causing ValueError in datetime constructor. For example, calling duration_in_seconds("2mo") in November produces target_month=13. Use modular arithmetic to correctly wrap months and increment year. Signed-off-by: JiangNan <1394485448@qq.com> --- litellm/litellm_core_utils/duration_parser.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/duration_parser.py b/litellm/litellm_core_utils/duration_parser.py index 70c28c4e067..6d2b4226ff4 100644 --- a/litellm/litellm_core_utils/duration_parser.py +++ b/litellm/litellm_core_utils/duration_parser.py @@ -64,12 +64,10 @@ def duration_in_seconds(duration: str) -> int: now = time.time() current_time = datetime.fromtimestamp(now) - if current_time.month == 12: - target_year = current_time.year + 1 - target_month = 1 - else: - target_year = current_time.year - target_month = current_time.month + value + # Calculate target month and year, handling overflow past December + total_months = current_time.month - 1 + value # 0-indexed months + target_year = current_time.year + total_months // 12 + target_month = total_months % 12 + 1 # back to 1-indexed # Determine the day to set for next month target_day = current_time.day