{"record":{"id":"d2be948b5d27203c","repo":"BerriAI/litellm","slug":"memory-with-key-key-already-exists","errorCode":null,"errorMessage":"Memory with key '{key}' already exists.","messagePattern":"Memory with key '(.+?)' already exists\\.","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"error","filePath":"litellm/proxy/memory/memory_endpoints.py","lineNumber":537,"sourceCode":"                \"team_id\": team_id,\n                \"created_by\": user_api_key_dict.user_id,\n                \"updated_by\": user_api_key_dict.user_id,\n            }\n            if body.metadata is not None:\n                create_data[\"metadata\"] = _serialize_metadata_for_prisma(body.metadata)\n            try:\n                row = await _memory_table(prisma_client).create(data=create_data)\n            except Exception as e:\n                # Race: a concurrent PUT/POST created the row after our check.\n                # Re-read and fall back to an update so the PUT stays idempotent\n                # instead of surfacing a 500 on a unique-violation.\n                if not _is_unique_violation(e):\n                    raise\n                existing_after_race: Final = await _find_existing()\n                if existing_after_race is None:\n                    # Row exists globally but isn't visible to this caller\n                    # (owned by someone else). Treat as conflict.\n                    raise HTTPException(\n                        status_code=409,\n                        detail=f\"Memory with key '{key}' already exists.\",\n                    )\n                # Same write-authorization check as the non-race path.\n                await _assert_write_access(prisma_client, existing_after_race, user_api_key_dict)\n                row = await _memory_table(prisma_client).update(\n                    where={\"memory_id\": existing_after_race.memory_id},\n                    data=data,\n                )\n    except HTTPException:\n        raise\n    except Exception as e:\n        raise _internal_error(\"Error upserting memory: %s\", e, \"Internal error updating memory entry.\")\n\n    return _row_to_model(row)\n\n\n@router.delete(","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/memory/memory_endpoints.py#L519-L555","documentation":"HTTPException raised on PUT when the initial create attempt lost a race — a concurrent request created the row with the same key after the existence check, and the retry/fallback also found a conflict. Signals the key now exists so the caller should retry as an update.","triggerScenarios":"Thrown at litellm/proxy/memory/memory_endpoints.py:537 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Choose a different key, or update the existing memory entry instead of creating a new one."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}