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(router): copy kwargs instead of popping target order

Emerson Gomesf7b1cc1f41b0
diff --git a/litellm/router.py b/litellm/router.py
index 81e49645462..6eadaaa9913 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -2172,9 +2172,8 @@ class Router:
"messages": messages,
"caching": self.cache_responses,
"client": model_client,
- **kwargs,
+ **{k: v for k, v in kwargs.items() if k != "_target_order"},
}
- input_kwargs.pop("_target_order", None)
response: Final = litellm.completion(**input_kwargs)
verbose_router_logger.info("litellm.completion(model=%s)\x1b[32m 200 OK\x1b[0m", model_name)
@@ -3194,11 +3193,10 @@ class Router:
"messages": messages,
"caching": self.cache_responses,
"client": model_client,
- **kwargs,
+ **{k: v for k, v in kwargs.items() if k != "_target_order"},
}
input_kwargs.pop("silent_model", None)
input_kwargs.pop("include_fallback_errors", None)
- input_kwargs.pop("_target_order", None)
_response: Final = litellm.acompletion(**input_kwargs)
diff --git a/litellm/router_utils/fallback_event_handlers.py b/litellm/router_utils/fallback_event_handlers.py
index c37fbdc8ed7..8c33bf1481f 100644
--- a/litellm/router_utils/fallback_event_handlers.py
+++ b/litellm/router_utils/fallback_event_handlers.py
@@ -412,11 +412,10 @@ async def run_async_fallback(
# LOGGING
kwargs = litellm_router.log_retry(kwargs=kwargs, e=original_exception)
verbose_router_logger.info("Falling back to model_group = %s", mask_sensitive_structure(mg))
+ kwargs = {k: v for k, v in kwargs.items() if k != "_target_order"} # rebind-ok: next hop must not inherit the previous order target
if isinstance(mg, str):
kwargs["model"] = mg
- kwargs.pop("_target_order", None)
elif isinstance(mg, dict):
- kwargs.pop("_target_order", None)
kwargs.update(mg)
fallback_depth = fallback_depth + 1
_hop_metadata = dict(kwargs.get(metadata_variable_name) or {})
diff --git a/tests/test_litellm/test_router_order_fallback.py b/tests/test_litellm/test_router_order_fallback.py
index 33916444003..d74e0a6ffa4 100644
--- a/tests/test_litellm/test_router_order_fallback.py
+++ b/tests/test_litellm/test_router_order_fallback.py
@@ -14,6 +14,7 @@ import litellm
from litellm import Router
from litellm.integrations.custom_logger import CustomLogger
from litellm.router_utils.prompt_caching_cache import PromptCachingCache
+from litellm.types.router import RouterRateLimitError
from litellm.utils import _get_deployment_order, _get_order_filtered_deployments
# ---------------------------------------------------------------------------
@@ -454,13 +455,12 @@ async def test_router_order_fallback_does_not_reselect_order_1_when_order_2_is_f
)
litellm.callbacks.append(drop_order_2)
try:
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(RouterRateLimitError, match="No deployments available") as exc_info:
await router.acompletion(
model="test-model",
messages=[{"role": "user", "content": "hi"}],
)
assert "success from order 2" not in str(exc_info.value)
- assert getattr(exc_info.value, "_hidden_params", {}).get("model_id") != "1"
finally:
litellm.callbacks.remove(drop_order_2)