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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): coerce batch candidate counts like the live limiter path
mateo-berri69ea1c659921
diff --git a/litellm/proxy/hooks/batch_rate_limiter.py b/litellm/proxy/hooks/batch_rate_limiter.pyindex aa3ac6b820b..404de776396 100644--- a/litellm/proxy/hooks/batch_rate_limiter.py+++ b/litellm/proxy/hooks/batch_rate_limiter.py@@ -429,14 +429,7 @@ class _PROXY_BatchRateLimiter(CustomLogger):),None,)- candidate_count: Final = max(- (- v- for v in (body.get("n"), body.get("best_of"))- if isinstance(v, int) and not isinstance(v, bool) and v > 1- ),- default=1,- )+ candidate_count: Final = self.parallel_request_limiter.get_output_candidate_count(body)if explicit_cap is not None:try:return max(0, int(explicit_cap)) * candidate_countdiff --git a/litellm/proxy/hooks/parallel_request_limiter_v3.py b/litellm/proxy/hooks/parallel_request_limiter_v3.pyindex 7cd56e66000..4a8da065fcf 100644--- a/litellm/proxy/hooks/parallel_request_limiter_v3.py+++ b/litellm/proxy/hooks/parallel_request_limiter_v3.py@@ -750,8 +750,8 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):return cls._get_explicit_output_cap(data, call_type) is not None@staticmethod- def _get_output_candidate_count(data: object, call_type: str | None = None) -> int:- if not isinstance(data, dict):+ def get_output_candidate_count(data: object, call_type: str | None = None) -> int:+ if not isinstance(data, Mapping):return 1config: Final = ((data.get("config") if "config" in data else data.get("generationConfig"))@@ -951,7 +951,7 @@ class _PROXY_MaxParallelRequestsHandler_v3(CustomLogger):else max(estimated_input_tokens, output_floor))- return estimated_input_tokens, max_tokens_estimate * self._get_output_candidate_count(data, call_type)+ return estimated_input_tokens, max_tokens_estimate * self.get_output_candidate_count(data, call_type)def _is_redis_cluster(self) -> bool:"""diff --git a/tests/test_litellm/proxy/hooks/test_batch_file_validation.py b/tests/test_litellm/proxy/hooks/test_batch_file_validation.pyindex 3a9d3ff72aa..31afb5c6019 100644--- a/tests/test_litellm/proxy/hooks/test_batch_file_validation.py+++ b/tests/test_litellm/proxy/hooks/test_batch_file_validation.py@@ -1992,9 +1992,13 @@ def _output_estimator():the no-`max_tokens` floor mock returns a distinctive sentinel so tests cantell "floor was used" apart from "an explicit cap was read"."""from litellm.proxy.hooks.batch_rate_limiter import _PROXY_BatchRateLimiter+ from litellm.proxy.hooks.parallel_request_limiter_v3 import (+ _PROXY_MaxParallelRequestsHandler_v3,+ )limiter = MagicMock()limiter.no_max_tokens_output_floor.return_value = 999+ limiter.get_output_candidate_count = _PROXY_MaxParallelRequestsHandler_v3.get_output_candidate_countreturn _PROXY_BatchRateLimiter(internal_usage_cache=MagicMock(),parallel_request_limiter=limiter,@@ -2070,6 +2074,9 @@ def test_estimate_entry_output_tokens_prefers_max_tokens_over_max_output_tokens(({"max_tokens": 40, "n": 3, "best_of": 5}, 200),({"max_tokens": 40, "n": 0}, 40),({"max_tokens": 40, "n": -2}, 40),+ ({"max_tokens": 40, "n": 5.0}, 200),+ ({"max_tokens": 40, "n": "10"}, 400),+ ({"max_tokens": 40, "n": "not-a-number"}, 40),({"n": 3}, 2997),],)diff --git a/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py b/tests/test_litellm/proxy/hooks/test_tpm_concurrent.pyindex e12610b769c..e08b18c1ae9 100644--- a/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py+++ b/tests/test_litellm/proxy/hooks/test_tpm_concurrent.py@@ -3384,7 +3384,7 @@ def test_split_quota_helpers_handle_non_mapping_inputs(rate_limiter):assert _call_id_from_callback_kwargs(object()) is Noneassert handler._is_embedding_request(object(), None) is Falseassert handler._get_explicit_output_cap(object(), None) is None- assert handler._get_output_candidate_count(object()) == 1+ assert handler.get_output_candidate_count(object()) == 1assert (handler._get_explicit_output_cap({"max_output_tokens": []}, "responses") is None)