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
Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_replica_registry_read_through
mateo-berridfc30e6b4f20
diff --cc litellm/proxy/agent_endpoints/a2a_endpoints.pyindex a4e1ac126d9,1cae87aed31..cea30ffad52--- a/litellm/proxy/agent_endpoints/a2a_endpoints.py+++ b/litellm/proxy/agent_endpoints/a2a_endpoints.py@@@ -152,16 -168,17 +168,16 @@@ def _jsonrpc_error)-def _get_agent(agent_id: str):+async def _get_agent(agent_id: str) -> "AgentResponse | None":"""Look up an agent by ID or name. Returns None if not found."""- from litellm.proxy.agent_endpoints.agent_registry import global_agent_registry+ from litellm.proxy.common_utils.registry_read_through import (+ get_agent_with_read_through,+ )- agent = global_agent_registry.get_agent_by_id(agent_id=agent_id)- if agent is None:- agent = global_agent_registry.get_agent_by_name(agent_name=agent_id)- return agent+ return await get_agent_with_read_through(agent_id)- def _enforce_inbound_trace_id(agent: Any, request: Request) -> None:+ def _enforce_inbound_trace_id(agent: "AgentResponse", request: Request) -> None:"""Raise 400 if agent requires x-litellm-trace-id on inbound calls and it is missing."""agent_litellm_params: Final = agent.litellm_params or {}if not agent_litellm_params.get("require_trace_id_on_calls_to_agent"):diff --cc tests/test_litellm/proxy/test_route_a2a_models.pyindex 22f99d03d21,02e4bddcee0..770f7857265--- a/tests/test_litellm/proxy/test_route_a2a_models.py+++ b/tests/test_litellm/proxy/test_route_a2a_models.py@@@ -105,77 -106,3 +107,79 @@@ async def test_route_non_a2a_model_raisuser_model=None,route_type="acompletion",)+++class _DbAgentRow:+ def __init__(self, agent_id: str, agent_name: str) -> None:+ self.agent_id = agent_id+ self.agent_name = agent_name+ self.object_permission = None+ self.spend = 0.0++ def __iter__(self):+ return iter(+ {+ "agent_id": self.agent_id,+ "agent_name": self.agent_name,+ "agent_card_params": {"name": self.agent_name, "url": "http://sibling-db-agent.example.com"},+ "litellm_params": {},+ }.items()+ )+++def _router_without_models():+ mock_router = Mock()+ mock_router.model_names = []+ mock_router.deployment_names = []+ mock_router.has_model_id = Mock(return_value=False)+ mock_router.model_group_alias = None+ mock_router.router_general_settings = Mock(pass_through_all_models=False)+ mock_router.default_deployment = None+ mock_router.pattern_router = Mock(patterns=[])+ mock_router.map_team_model = Mock(return_value=None)++ mock_router.is_recognized_model = Mock(return_value=False)++ mock_router.team_public_model_names = []+ return mock_router+++async def test_route_a2a_model_read_through_recovers_agent_created_on_sibling_replica(monkeypatch):+ import litellm.proxy.proxy_server as proxy_server+ from litellm.proxy.agent_endpoints.agent_registry import global_agent_registry++ agent_name = "a2a-sibling-replica-agent"+ prisma_client = Mock()+ prisma_client.db.litellm_agentstable.find_many = AsyncMock(+ return_value=[_DbAgentRow("a2a-sibling-replica-agent-id", agent_name)]+ )+ monkeypatch.setattr(proxy_server, "prisma_client", prisma_client)+ monkeypatch.setattr(proxy_server, "store_model_in_db", True)++ original_agents = list(global_agent_registry.agent_list)+ original_config_agents = getattr(global_agent_registry, "config_agents", ())+ global_agent_registry.agent_list = []+ global_agent_registry.config_agents = ()++ data = {+ "model": f"a2a/{agent_name}",+ "messages": [{"role": "user", "content": "Hello"}],+ }+ mock_acompletion = AsyncMock(return_value={"id": "read-through-response"})++ try:+ with patch("litellm.acompletion", mock_acompletion):+ await route_request(+ data=data,+ llm_router=_router_without_models(),+ user_model=None,+ route_type="acompletion",+ )+ finally:+ global_agent_registry.agent_list = original_agents+ global_agent_registry.config_agents = original_config_agents++ mock_acompletion.assert_called_once()+ call_kwargs = mock_acompletion.call_args.kwargs+ assert call_kwargs["model"] == f"a2a/{agent_name}"+ assert call_kwargs["api_base"] == "http://sibling-db-agent.example.com"+ prisma_client.db.litellm_agentstable.find_many.assert_awaited()diff --cc tests/test_litellm/proxy/test_route_llm_request.pyindex ebd52c448bc,fc3b14592cd..8f52fa179fe--- a/tests/test_litellm/proxy/test_route_llm_request.py+++ b/tests/test_litellm/proxy/test_route_llm_request.py@@@ -1093,122 -1119,25 +1119,145 @@@ async def test_route_request_rejects_chllm_router.acompletion.assert_not_called()+class FakeProxyModelTable:+ def __init__(self, rows):+ self.rows = rows+ self.find_many_wheres = []++ async def find_many(self, where=None, **kwargs):+ self.find_many_wheres.append(where)+ return list(self.rows)+++def _fake_prisma_client_with_models(rows):+ from types import SimpleNamespace++ table = FakeProxyModelTable(rows)+ return SimpleNamespace(db=SimpleNamespace(litellm_proxymodeltable=table)), table+++def _db_model_row(model_name: str, mock_response: str):+ from types import SimpleNamespace++ return SimpleNamespace(+ model_id=f"{model_name}-id",+ model_name=model_name,+ litellm_params={"model": "openai/gpt-4o", "api_key": "fake", "mock_response": mock_response},+ model_info={},+ blocked=False,+ )+++async def test_route_request_read_through_recovers_model_created_on_sibling_replica(monkeypatch):+ """Regression: a model written to the DB by another replica must be served on+ first request instead of 400ing until the periodic config reload."""+ import litellm+ import litellm.proxy.proxy_server as proxy_server++ model_name = "e2e-sibling-replica-model"+ router = litellm.Router(+ model_list=[+ {+ "model_name": "some-other-model",+ "litellm_params": {"model": "openai/gpt-4o", "api_key": "fake"},+ }+ ]+ )+ fake_prisma, table = _fake_prisma_client_with_models([_db_model_row(model_name, "hello-from-db")])+ monkeypatch.setattr(proxy_server, "prisma_client", fake_prisma)+ monkeypatch.setattr(proxy_server, "store_model_in_db", True)+ monkeypatch.setattr(proxy_server, "llm_router", router)++ llm_call = await route_request(+ data={"model": model_name, "messages": [{"role": "user", "content": "hi"}]},+ llm_router=router,+ user_model=None,+ route_type="acompletion",+ )+ response = await llm_call++ assert response.choices[0].message.content == "hello-from-db"+ assert len(table.find_many_wheres) == 1+ assert table.find_many_wheres[0] == {"OR": [{"model_name": model_name}, {"model_id": model_name}]}+++async def test_route_request_unknown_model_raises_and_hits_db_once_within_ttl(monkeypatch):+ import litellm+ import litellm.proxy.proxy_server as proxy_server++ model_name = "e2e-model-nobody-created"+ router = litellm.Router(+ model_list=[+ {+ "model_name": "some-other-model",+ "litellm_params": {"model": "openai/gpt-4o", "api_key": "fake"},+ }+ ]+ )+ fake_prisma, table = _fake_prisma_client_with_models([])+ monkeypatch.setattr(proxy_server, "prisma_client", fake_prisma)+ monkeypatch.setattr(proxy_server, "store_model_in_db", True)+ monkeypatch.setattr(proxy_server, "llm_router", router)++ data = {"model": model_name, "messages": [{"role": "user", "content": "hi"}]}+ with pytest.raises(ProxyModelNotFoundError):+ await route_request(data=data, llm_router=router, user_model=None, route_type="acompletion")+ with pytest.raises(ProxyModelNotFoundError):+ await route_request(data=data, llm_router=router, user_model=None, route_type="acompletion")++ assert len(table.find_many_wheres) == 1+++async def test_route_request_read_through_disabled_without_store_model_in_db(monkeypatch):+ import litellm+ import litellm.proxy.proxy_server as proxy_server++ model_name = "e2e-config-only-proxy-model"+ router = litellm.Router(+ model_list=[+ {+ "model_name": "some-other-model",+ "litellm_params": {"model": "openai/gpt-4o", "api_key": "fake"},+ }+ ]+ )+ fake_prisma, table = _fake_prisma_client_with_models([_db_model_row(model_name, "should-not-load")])+ monkeypatch.setattr(proxy_server, "prisma_client", fake_prisma)+ monkeypatch.setattr(proxy_server, "store_model_in_db", False)+ monkeypatch.setattr(proxy_server, "llm_router", router)++ with pytest.raises(ProxyModelNotFoundError):+ await route_request(+ data={"model": model_name, "messages": [{"role": "user", "content": "hi"}]},+ llm_router=router,+ user_model=None,+ route_type="acompletion",+ )++ assert table.find_many_wheres == []+++ @pytest.mark.asyncio+ async def test_route_request_routing_group_name_passes_model_gate():+ from unittest.mock import AsyncMock, patch++ from litellm import Router++ router = Router(+ model_list=[+ {"model_name": "member-a", "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-test"}},+ {"model_name": "member-b", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"}},+ ],+ routing_groups=[+ {"group_name": "grouped-quality", "models": ["member-a", "member-b"], "routing_strategy": "simple-shuffle"}+ ],+ )+ data = {"model": "grouped-quality", "messages": [{"role": "user", "content": "hi"}]}++ with patch.object(router, "acompletion", new=AsyncMock(return_value="group_response")) as spy:+ response = await (await route_request(data, router, None, "acompletion"))++ assert response == "group_response"+ spy.assert_called_once_with(**data)