Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): guard candidate-count and batch cap coercion against float overflow

mateo-berri3c34c344597b
diff --git a/litellm/proxy/hooks/batch_rate_limiter.py b/litellm/proxy/hooks/batch_rate_limiter.py
index 404de776396..d6229fb80a6 100644
--- a/litellm/proxy/hooks/batch_rate_limiter.py
+++ b/litellm/proxy/hooks/batch_rate_limiter.py
@@ -433,7 +433,7 @@ class _PROXY_BatchRateLimiter(CustomLogger):
if explicit_cap is not None:
try:
return max(0, int(explicit_cap)) * candidate_count
- except (TypeError, ValueError):
+ except (TypeError, ValueError, OverflowError):
pass
return self.parallel_request_limiter.no_max_tokens_output_floor(min_configured_otpm_limit) * candidate_count
diff --git a/litellm/proxy/hooks/parallel_request_limiter_v3.py b/litellm/proxy/hooks/parallel_request_limiter_v3.py
index 4a8da065fcf..2d799ded752 100644
--- a/litellm/proxy/hooks/parallel_request_limiter_v3.py
+++ b/litellm/proxy/hooks/parallel_request_limiter_v3.py
@@ -768,7 +768,7 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):
for value in candidate_values:
try:
candidate_count = max(candidate_count, int(value or 1))
- except (TypeError, ValueError):
+ except (TypeError, ValueError, OverflowError):
continue
return candidate_count
diff --git a/tests/test_litellm/proxy/hooks/test_batch_file_validation.py b/tests/test_litellm/proxy/hooks/test_batch_file_validation.py
index 31afb5c6019..1ce1a2f3e51 100644
--- a/tests/test_litellm/proxy/hooks/test_batch_file_validation.py
+++ b/tests/test_litellm/proxy/hooks/test_batch_file_validation.py
@@ -2077,6 +2077,8 @@ def test_estimate_entry_output_tokens_prefers_max_tokens_over_max_output_tokens(
({"max_tokens": 40, "n": 5.0}, 200),
({"max_tokens": 40, "n": "10"}, 400),
({"max_tokens": 40, "n": "not-a-number"}, 40),
+ ({"max_tokens": 40, "n": 1e309}, 40),
+ ({"max_tokens": 1e309, "n": 3}, 2997),
({"n": 3}, 2997),
],
)
diff --git a/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py b/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py
index e08b18c1ae9..8d03857c917 100644
--- a/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py
+++ b/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py
@@ -3385,6 +3385,7 @@ def test_split_quota_helpers_handle_non_mapping_inputs(rate_limiter):
assert handler._is_embedding_request(object(), None) is False
assert handler._get_explicit_output_cap(object(), None) is None
assert handler.get_output_candidate_count(object()) == 1
+ assert handler.get_output_candidate_count({"n": 1e309}) == 1
assert (
handler._get_explicit_output_cap({"max_output_tokens": []}, "responses") is None
)