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(prompts): keep prompts created mid-sync out of the deleted-row sweep

mateo-berri4fd7b9946f03
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 4adbabae1fa..c5afadbf7b7 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -7269,6 +7269,7 @@ class ProxyConfig:
return None
try:
+ prompt_ids_loaded_before_db_read: Final = frozenset(IN_MEMORY_PROMPT_REGISTRY.IN_MEMORY_PROMPTS)
prompts_in_db: Final[Sequence[object]] = await PromptRepository(prisma_client).table.find_many()
parsed_specs: Final[tuple[PromptSpec, ...]] = tuple(
spec for row in prompts_in_db if (spec := parse_row(row)) is not None
@@ -7296,8 +7297,10 @@ class ProxyConfig:
if every_row_parsed:
deleted_db_prompt_ids: Final = tuple(
prompt_id
- for prompt_id, spec in IN_MEMORY_PROMPT_REGISTRY.IN_MEMORY_PROMPTS.items()
- if spec.prompt_info.prompt_type == "db" and prompt_id not in newest_spec_per_id
+ for prompt_id in prompt_ids_loaded_before_db_read
+ if (loaded_spec := IN_MEMORY_PROMPT_REGISTRY.IN_MEMORY_PROMPTS.get(prompt_id)) is not None
+ and loaded_spec.prompt_info.prompt_type == "db"
+ and prompt_id not in newest_spec_per_id
)
for deleted_prompt_id in deleted_db_prompt_ids:
IN_MEMORY_PROMPT_REGISTRY.remove_prompt(prompt_id=deleted_prompt_id)
diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.py
index 4d429abd96e..35e248a43e6 100644
--- a/tests/test_litellm/proxy/test_proxy_server.py
+++ b/tests/test_litellm/proxy/test_proxy_server.py
@@ -11600,6 +11600,42 @@ async def test_init_prompts_in_db_keeps_the_in_memory_copy_when_a_row_fails_to_p
IN_MEMORY_PROMPT_REGISTRY.delete_prompts_by_base_id("greeting_broken")
+async def test_init_prompts_in_db_keeps_a_prompt_created_while_the_sync_was_reading(monkeypatch):
+ from litellm.proxy.prompts.prompt_registry import IN_MEMORY_PROMPT_REGISTRY
+ from litellm.proxy.proxy_server import ProxyConfig
+ from litellm.types.prompts.init_prompts import PromptInfo, PromptLiteLLMParams, PromptSpec
+
+ monkeypatch.setattr(litellm, "callbacks", [])
+
+ prisma_client = MagicMock()
+ try:
+
+ async def create_prompt_behind_the_select() -> list:
+ IN_MEMORY_PROMPT_REGISTRY.initialize_prompt(
+ prompt=PromptSpec(
+ prompt_id="greeting_race.v1",
+ litellm_params=PromptLiteLLMParams(
+ prompt_id="greeting_race",
+ prompt_integration="dotprompt",
+ prompt_data={"content": "Begin every reply with AHOY", "metadata": {}},
+ ),
+ prompt_info=PromptInfo(prompt_type="db"),
+ )
+ )
+ return []
+
+ prisma_client.db.litellm_prompttable.find_many = AsyncMock(side_effect=create_prompt_behind_the_select)
+ await ProxyConfig()._init_prompts_in_db(prisma_client=prisma_client)
+
+ surviving_callback = IN_MEMORY_PROMPT_REGISTRY.get_prompt_callback_by_id("greeting_race.v1")
+ assert IN_MEMORY_PROMPT_REGISTRY.get_prompt_by_id("greeting_race.v1") is not None
+ assert surviving_callback is not None
+ assert litellm.callbacks == [surviving_callback]
+ finally:
+ IN_MEMORY_PROMPT_REGISTRY.delete_prompts_by_base_id("greeting_race")
+
+
class TestEmbeddingsFailureHookRequestData:
@pytest.mark.asyncio
async def test_failure_hook_gets_post_setup_data_with_logging_obj(self):