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 G. 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 any of the money being used 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.

“The relevant distinction,” Roberts wrote, “is between conditions that define the limits of the government spending program — those that specify the activities Congress wants to subsidize — and conditions that seek to leverage funding to regulate speech outside the contours of the program itself.” He acknowledged that “the line is hardly clear.”

Roberts said the government can restrict the ways money is spent, but cannot require groups receiving the funding to “pledge allegiance to the government’s policy of eradicating prostitution.”

The groups said they were gratified that the court recognized their sensitive position.

“Public health groups cannot tell sex workers that we ‘oppose’ them, yet expect them to be partners in preventing HIV,” said Marine Buissonniere, director of the Open Society Public Health Program, one of the groups involved in challenging the provision. “Condemnation and alienation are not public health strategies. The pledge ignores years of evidence that sex workers are critical partners in the fight against AIDS.”

Roberts was joined in the ruling by Justices Anthony M. Kennedy, Ruth Bader Ginsburg, Stephen G. Breyer, Sonia Sotomayor and Samuel A. Alito Jr. Justice Elena Kagan recused herself from the case, presumably because she had worked on it when she was President Obama’s solicitor general.

Justices Antonin Scalia and Clarence Thomas dissented. Scalia said the government was not coercing speech but simply acquiring partners who share its beliefs.

“The First Amendment does not mandate a viewpoint-neutral government,” Scalia wrote.

“The government may enlist the assistance of those who believe in its ideas to carry them to fruition; and it need not enlist for that purpose those who oppose or do not support the ideas. That seems to me a matter of the most common common sense.”

The case is Agency for International Development v. Alliance for Open Society International.