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(memory): return 404 when the memory row vanishes before delete

mateo-berrid16afd202729
diff --git a/litellm/proxy/memory/memory_endpoints.py b/litellm/proxy/memory/memory_endpoints.py
index 193f7e09f07..98c5fdd198c 100644
--- a/litellm/proxy/memory/memory_endpoints.py
+++ b/litellm/proxy/memory/memory_endpoints.py
@@ -535,8 +535,10 @@ async def delete_memory(
# Visibility != write authority — see the upsert handler for the rationale.
await _assert_write_access(prisma_client, row, user_api_key_dict)
try:
- await _memory_table(prisma_client).delete(where={"memory_id": row.memory_id})
+ deleted: Final = await _memory_table(prisma_client).delete(where={"memory_id": row.memory_id})
except Exception as e:
raise _internal_error("Error deleting memory: %s", e, "Internal error deleting memory entry.")
+ if deleted is None:
+ raise HTTPException(status_code=404, detail=f"Memory with key '{key}' not found")
return MemoryDeleteResponse(key=key, deleted=True)
diff --git a/tests/test_litellm/proxy/memory/test_memory_endpoints.py b/tests/test_litellm/proxy/memory/test_memory_endpoints.py
index da23e362bae..be75d980d9d 100644
--- a/tests/test_litellm/proxy/memory/test_memory_endpoints.py
+++ b/tests/test_litellm/proxy/memory/test_memory_endpoints.py
@@ -975,6 +975,28 @@ class TestMemoryEndpoints:
resp = client.delete("/v1/memory/notes")
assert resp.status_code == 404
+ def test_delete_memory_row_deleted_mid_delete_returns_404(self):
+ table = self.prisma.db.litellm_memorytable
+ table.rows.append(
+ _make_row(memory_id="m1", key="notes", user_id="user-a", team_id="team-a")
+ )
+
+ async def vanished(*_args, **_kwargs):
+ return None
+
+ original_delete = table.delete
+ table.delete = vanished
+
+ client = _make_client(_user_auth("user-a", "team-a"))
+ try:
+ with _patch_prisma(self.prisma):
+ resp = client.delete("/v1/memory/notes")
+ finally:
+ table.delete = original_delete
+
+ assert resp.status_code == 404, resp.text
+ assert resp.json()["detail"] == "Memory with key 'notes' not found"
+
def test_visibility_filter_unscoped_for_admin_viewer(self):
"""
proxy_admin_viewer reads with the same unscoped filter as proxy_admin;