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(prompts): sync only the newest row when environments share a versioned prompt id
mateo-berri5461bb3b4892
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex afc29255d58..b1ffce9c15a 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -7239,16 +7239,38 @@ class ProxyConfig:from litellm.proxy.prompts.prompt_registry import IN_MEMORY_PROMPT_REGISTRYfrom litellm.types.prompts.init_prompts import PromptSpec+ def parse_row(db_prompt: object) -> PromptSpec | None:+ try:+ return self._get_prompt_spec_for_db_prompt(db_prompt=db_prompt)+ except Exception as row_error: # noqa: BLE001 # a malformed row must not block syncing the remaining prompts+ verbose_proxy_logger.exception(+ "litellm.proxy.proxy_server.py::ProxyConfig:_init_prompts_in_db - failed to parse prompt row %s: %s",+ getattr(db_prompt, "prompt_id", None),+ row_error,+ )+ return None+try:prompts_in_db: Final[Sequence[object]] = await PromptRepository(prisma_client).table.find_many()- for prompt in prompts_in_db:+ parsed_specs: Final[tuple[PromptSpec, ...]] = tuple(+ spec for row in prompts_in_db if (spec := parse_row(row)) is not None+ )+ newest_spec_per_id: Final[Mapping[str, PromptSpec]] = MappingProxyType(+ {+ spec.prompt_id: spec+ for spec in sorted(+ parsed_specs,+ key=lambda s: s.updated_at.timestamp() if s.updated_at else float("-inf"),+ )+ }+ )+ for prompt_spec in newest_spec_per_id.values():try:- prompt_spec = self._get_prompt_spec_for_db_prompt(db_prompt=prompt)IN_MEMORY_PROMPT_REGISTRY.sync_prompt_from_db(prompt=prompt_spec)except Exception as prompt_sync_error: # noqa: BLE001 # one poisoned row must not block syncing the remaining promptsverbose_proxy_logger.exception("litellm.proxy.proxy_server.py::ProxyConfig:_init_prompts_in_db - failed to sync prompt %s: %s",- getattr(prompt, "prompt_id", None),+ prompt_spec.prompt_id,prompt_sync_error,)except Exception as e:diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.pyindex 42cff844513..fbf71829abc 100644--- a/tests/test_litellm/proxy/test_proxy_server.py+++ b/tests/test_litellm/proxy/test_proxy_server.py@@ -11369,6 +11369,57 @@ async def test_init_prompts_in_db_syncs_remaining_rows_when_one_row_fails(monkeyIN_MEMORY_PROMPT_REGISTRY.delete_prompts_by_base_id("broken_sync")+async def test_init_prompts_in_db_serves_the_newest_row_when_environments_collide_on_a_versioned_id(monkeypatch):+ from litellm.proxy.prompts.prompt_registry import IN_MEMORY_PROMPT_REGISTRY+ from litellm.proxy.proxy_server import ProxyConfig++ monkeypatch.setattr(litellm, "callbacks", [])++ def db_row(environment: str, content: str, updated_at: datetime) -> MagicMock:+ row = MagicMock()+ row.model_dump.return_value = {+ "prompt_id": "greeting_env",+ "version": 1,+ "environment": environment,+ "created_by": None,+ "litellm_params": json.dumps(+ {+ "prompt_id": "greeting_env",+ "prompt_integration": "dotprompt",+ "prompt_data": {"content": content, "metadata": {}},+ }+ ),+ "prompt_info": json.dumps({"prompt_type": "db"}),+ "created_at": None,+ "updated_at": updated_at,+ }+ return row++ freshly_patched = db_row(+ "production", "Begin every reply with HOWDY", datetime(2026, 8, 26, 12, 0, tzinfo=timezone.utc)+ )+ stale_sibling = db_row(+ "development", "Begin every reply with AHOY", datetime(2026, 8, 26, 11, 0, tzinfo=timezone.utc)+ )++ prisma_client = MagicMock()+ try:+ prisma_client.db.litellm_prompttable.find_many = AsyncMock(return_value=[freshly_patched, stale_sibling])+ await ProxyConfig()._init_prompts_in_db(prisma_client=prisma_client)++ first_callback = IN_MEMORY_PROMPT_REGISTRY.get_prompt_callback_by_id("greeting_env.v1")+ assert first_callback is not None+ assert first_callback.prompt_manager.get_prompt("greeting_env").content == "Begin every reply with HOWDY"++ await ProxyConfig()._init_prompts_in_db(prisma_client=prisma_client)++ assert IN_MEMORY_PROMPT_REGISTRY.get_prompt_callback_by_id("greeting_env.v1") is first_callback+ assert litellm.callbacks == [first_callback]+ finally:+ IN_MEMORY_PROMPT_REGISTRY.delete_prompts_by_base_id("greeting_env")++class TestEmbeddingsFailureHookRequestData:@pytest.mark.asyncioasync def test_failure_hook_gets_post_setup_data_with_logging_obj(self):