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]

Merge branch 'litellm_internal_staging' of https://github.com/BerriAI/litellm into litellm_replica_registry_read_through

mateo-berri790d645d3462
diff --cc litellm/router.py
index f3fb7c08d2c,efd3b5a527e..00c6c4c8b6f
--- a/litellm/router.py
+++ b/litellm/router.py
@@@ -8633,24 -8632,6 +8633,24 @@@ class Router
else:
raise e
+ def _restore_deployment_after_failed_upsert(self, previous_deployment: Deployment | None, model_id: str) -> None:
+ if previous_deployment is None or self.has_model_id(model_id):
+ return
+ try:
+ self.add_deployment(deployment=previous_deployment)
+ verbose_router_logger.info(
+ "Restored deployment %s (id=%s); it keeps serving its previous configuration.",
+ previous_deployment.model_name,
+ model_id,
+ )
- except Exception as restore_error:
++ except Exception as restore_error: # noqa: BLE001 # best-effort restore: a second failure must not abort the reload
+ verbose_router_logger.warning(
+ "Could not restore previously served deployment %s (id=%s) after the failed upsert: %s",
+ previous_deployment.model_name,
+ model_id,
+ restore_error,
+ )
+
@staticmethod
def _backend_cost_map_keys(model: str, custom_llm_provider: str | None) -> tuple[str, ...]:
"""The ``litellm.model_cost`` keys a deployment's shared backend info is registered under."""
diff --cc tests/test_litellm/proxy/test_proxy_server.py
index 3fbbcf674da,7fdfbea843f..e7d7d4c9323
--- a/tests/test_litellm/proxy/test_proxy_server.py
+++ b/tests/test_litellm/proxy/test_proxy_server.py
@@@ -11100,27 -11100,44 +11100,70 @@@ async def test_moderations_reraises_pro
mock_logging.post_call_failure_hook.assert_awaited_once()
+async def test_init_agents_in_db_rebuilds_registry_under_agent_reconcile_lock(monkeypatch):
+ from litellm.proxy.agent_endpoints.agent_registry import (
+ AGENT_RECONCILE_LOCK,
+ global_agent_registry,
+ )
+ from litellm.proxy.proxy_server import ProxyConfig
+
+ lock_states: list[bool] = []
+
+ async def fake_get_all_agents_from_db(prisma_client) -> list:
+ lock_states.append(AGENT_RECONCILE_LOCK.locked())
+ return []
+
+ def fake_load_agents_from_db_and_config(db_agents) -> None:
+ lock_states.append(AGENT_RECONCILE_LOCK.locked())
+
+ monkeypatch.setattr(global_agent_registry, "get_all_agents_from_db", fake_get_all_agents_from_db)
+ monkeypatch.setattr(global_agent_registry, "load_agents_from_db_and_config", fake_load_agents_from_db_and_config)
+
+ await ProxyConfig()._init_agents_in_db(prisma_client=MagicMock())
+
+ assert lock_states == [True, True]
+ assert not AGENT_RECONCILE_LOCK.locked()
++
++
+ class TestEmbeddingsFailureHookRequestData:
+ @pytest.mark.asyncio
+ async def test_failure_hook_gets_post_setup_data_with_logging_obj(self):
+ """Request setup replaces the processor's data dict (adding the logging
+ object the failure hook needs to lift token usage from); the embeddings
+ exception handler must pass that replaced dict, not the raw request body
+ dict it was rebuilt from."""
+ from litellm.proxy._types import ProxyException
+
+ captured = {}
+ logging_obj_sentinel = MagicMock()
+
+ async def fake_process(self, **kwargs):
+ self.data = {**self.data, "litellm_logging_obj": logging_obj_sentinel}
+ captured["processor_data"] = self.data
+ raise RuntimeError("provider timeout")
+
+ with (
+ patch.object(
+ proxy_server_module,
+ "_read_request_body",
+ new=AsyncMock(return_value={"model": "my-embed", "input": "hello"}),
+ ),
+ patch.object(
+ proxy_server_module.ProxyBaseLLMRequestProcessing,
+ "base_process_llm_request",
+ new=fake_process,
+ ),
+ patch.object(proxy_server_module, "proxy_logging_obj") as mock_logging,
+ ):
+ mock_logging.post_call_failure_hook = AsyncMock(return_value=None)
+ with pytest.raises(ProxyException):
+ await proxy_server_module.embeddings(
+ request=MagicMock(),
+ fastapi_response=MagicMock(),
+ user_api_key_dict=UserAPIKeyAuth(),
+ )
+
+ hook_request_data = mock_logging.post_call_failure_hook.await_args.kwargs["request_data"]
+ assert hook_request_data is captured["processor_data"]
+ assert hook_request_data["litellm_logging_obj"] is logging_obj_sentinel