United Nations Standard Minimum Rules for the Treatment of Prisoners (the Mandela Rules). Rule 109
Rule 109
1. Persons who are found to be not criminally responsible, or who are later diagnosed with severe mental disabilities and/or health conditions, for whom staying in prison would mean an exacerbation of their condition, shall not be detained in prisons, and arrangements shall be made to transfer them to mental health facilities as soon as possible.
[pdf] https://documents.un.org/doc/undoc/ltd/v15/030/48/pdf/v1503048.pdf