The City Council is planning to repeal a ban on so-called “conversion therapy” aimed at turning gay people straight ­– a move aimed at avoiding a court challenge that could potentially set a pro-conversion therapy precedent nationwide.

Council Speaker Corey Johnson, who is gay, plans to introduce a bill Thursday to reverse the city’s ban of conversion therapy approved two years ago – even though he still considers the practice “barbaric and inhumane.”

“This was a painful decision that was made after leading LGBTQ advocates requested that the Council repeal our 2017 bill,” Johnson said in a statement announcing the move.

“After intense deliberation, the Council concluded that it was best to take this drastic step. The courts have changed considerably over the last few years, and we cannot count on them to rule in favor of much-needed protections for the LGBTQ community.”

Johnson added, “I can’t stress enough how agonizing of a decision this was, but ultimately I listened to the advocates who know the issue best, as well as my heart. I will never stop fighting for the community I am so proud to be a part of.”

Johnson is responding to a federal lawsuit filed in January by Alliance Defending Freedom, an Arizona-based anti-LGBTQ Christian group, challenging the constitutionality of the city’s ban. That same month, state legislators in Albany passed a separate law banning conversion therapy for minors throughout New York.

LGBTQ advocates have raised concerns that the group’s lawsuit, filed in Brooklyn, could potentially reach the right-leaning US Supreme Court, leading to a precedent-setting ruling in ADF’s favor. They’ve requested the Council repeal the ban and instead rely on the new state law.

ADF has a long history of litigating all the way to the nation’s highest court.

Although Johnson’s bill is expected to have widespread council support, it can’t be voted on until there’s a public hearing. However, sources said a vote could take place later this month.