fix: handle month overflow in duration_in_seconds for multi-month durations - #23099
Conversation
…ations
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
Greptile SummaryThis PR fixes a Key observations:
Confidence Score: 4/5
Last reviewed commit: 947db46 |
| # 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 |
There was a problem hiding this comment.
The PR description outlines a test plan (November + 2 months, October + 3 months, etc.) but no test code was actually added. The existing test file only exercises get_next_standardized_reset_time; there are currently zero tests directly covering duration_in_seconds("Nmo") for multi-month cases like "2mo" or "3mo" — the scenarios that this fix addresses.
Please add unit tests for the fixed code paths as described in the test plan, for example:
from unittest.mock import patch
from datetime import datetime
from litellm.litellm_core_utils.duration_parser import duration_in_seconds
def _mock_time(year, month, day):
dt = datetime(year, month, day, 12, 0, 0)
return dt.timestamp()
def test_november_plus_two_months():
with patch("time.time", return_value=_mock_time(2023, 11, 15)):
result = duration_in_seconds("2mo")
assert result > 0
def test_october_plus_three_months():
with patch("time.time", return_value=_mock_time(2023, 10, 15)):
result = duration_in_seconds("3mo")
assert result > 0
def test_december_plus_one_month():
with patch("time.time", return_value=_mock_time(2023, 12, 15)):
result = duration_in_seconds("1mo")
assert result > 0Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)
|
I have read the CLA Document and I hereby sign the CLA |
1 similar comment
|
I have read the CLA Document and I hereby sign the CLA |
0d3735f
into
BerriAI:litellm_oss_staging_03_09_2026
…ations (BerriAI#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>
Summary
duration_in_seconds("Nmo")crashes withValueError: month must be in 1..12whencurrent_month + N > 12andcurrent_month != 12.The existing code only handles the year rollover for
current_month == 12, but not for cases like November + 2 months = month 13. This affects any multi-month budget duration (e.g.,"2mo","3mo") used in Router provider budget routing or Proxy key/team generation.Before:
month=11, value=2→target_month=13→ValueErrorAfter: Uses modular arithmetic:
target_month = (total_months % 12) + 1,target_year += total_months // 12Test plan
duration_in_seconds("2mo")works correctly in November (month 11)duration_in_seconds("3mo")works correctly in October (month 10)duration_in_seconds("1mo")behavior is unchanged