WASHINGTON – The Supreme Court ruled Thursday that it is a violation of the First Amendment for the federal government to force groups to endorse the government’s views opposing prostitution in order to receive funding to combat AIDS overseas.

The justices ruled 6 to 2 that a requirement in a multibillion-dollar anti-AIDS program withholding money from organizations that do not have a policy “explicitly opposing prostitution and sex trafficking” violates their free-speech rights.

“This case is not about the government’s ability to enlist the assistance of those with whom it already agrees,” wrote Chief Justice John Roberts Jr. “It is about compelling a grant recipient to adopt a particular belief as a condition of funding.”

The decision came on what is supposed to be the penultimate week of the Supreme Court’s term. But marquee decisions on same-sex marriage, on the future of affirmative action in college admissions and on a key section of the Voting Rights Act are undecided, as are seven other cases.

Monday is the court’s last scheduled day for decisions, but the justices are likely to add one or two more days next week.

The provision at the center of the court’s ruling Thursday is part of a 2003 law under which the United States is spending $60 billion to combat infectious diseases worldwide. It forbade using any of the money to “promote or advocate the legalization or practice of prostitution or sex trafficking,” which are ways the diseases can be spread.

But a second provision required the groups receiving the funding to have an explicit policy denouncing prostitution.

Four groups involved in overseas programs sued, arguing that the requirement could undermine their effectiveness because they must often work with those involved in the sex trade in order to fight the spread of AIDS.

The groups noted that the World Health Organization, which also receives money from the program, was exempted from the requirement.

Lower courts agreed with the organizations, and the restriction has never been enforced.

Roberts noted that the case required balancing the government’s interests in seeing its policies carried out and in forcing organizations to adopt the desired speech.

He noted that the court has upheld federal laws that restrict doctors in government-subsidized clinics in discussing abortion and that require libraries not to allow pornography on their computers.