Several of the groups, including the Alliance for Open Society International, objected to the requirement, not because they favored prostitution. They said it would interfere with their work. They seek to encourage women, including prostitutes, to come to their clinics for testing and treatment.

The Supreme Court took up the issue on appeal from the U.S. appeals court in New York, which said the "compelled speech" rule violated free-speech protections.

An Obama administration lawyer urged the court to uphold the requirement.

"Prostitution and sex trafficking contribute to the spread of AIDS. So it makes good sense that Congress would have imposed this condition," said Deputy Solicitor Gen. Sri Srinivasan.

But the justices sounded troubled by tying the money to the message. "There has to be some limitation on what type of loyalty oath you can require them to sign," said Chief Justice John G. Roberts Jr.

Justices Ruth Bader Ginsburg and Samuel A. Alito Jr. said they worried about giving public agencies, including universities, a free hand to require people to voice support for certain policies as a condition of receiving support.

"It seems like quite a dangerous proposition," Alito said.

The issue of government-mandated speech has divided the justices before. In one prominent case, the court by a 5-4 vote said doctors and nurses at federally funded family planning clinics could be required not to mention abortion as a condition of receiving funds. But in another 5-4 decision, the court struck down a law that barred government-funded legal aid attorneys from challenging aid restrictions as unconstitutional.

David Bowker, a lawyer representing the international aid groups, ran into trouble when he argued that the government could not select groups for funding based on their policies. Justices Anthony M. Kennedy and Antonin Scalia quickly objected. "They can't fund the Boy Scouts of America because they like the programs that the BSA has? They have to treat them equally with the Muslim Brotherhood?" asked Scalia, sounding aghast.

When Bowker replied that the government could not withhold money to "penalize a viewpoint," Roberts posed an even harder question. Suppose the government had been funding groups to dig wells in South Africa decades ago, and one group supported apartheid and another opposed it. The government "can't say we're going to prefer the anti-apartheid group?" Roberts asked.

Bowker said this case did not involve selecting groups to do the work of fighting AIDS. The groups were already selected, he said, and then were told, "We want you to toe the line."

In closing, Bowker said it would be "wildly inconsistent" with the 1st Amendment to force people to state a message in exchange for funding. "Under the government's theory, you can give anyone a dollar in Medicare funds and say, 'Now that you have taken a dollar of our money, we want you to profess agreement with the Affordable Care Act,'" he said. "Where does that end?"

Justice Elena Kagan sat out the case of Agency for International Development vs. Alliance for Open Society. A ruling is expected by late June.