WASHINGTON — Groups receiving federal financing to combat AIDS abroad may not be required to adopt policies opposing prostitution, the Supreme Court ruled on Thursday.

Under a 2003 law, the federal government has distributed billions of dollars to private groups to help fight AIDS around the world, imposing two conditions in the process. First, the money may not be used “to promote or advocate the legalization or practice of prostitution and sex trafficking.” That condition was not before the court.

The question for the justices was whether the second condition, requiring recipients to have “a policy explicitly opposing prostitution and sex trafficking,” passed constitutional muster.

Chief Justice John G. Roberts Jr., writing for a six-justice majority, said the condition ran afoul of the First Amendment because it required recipients “to pledge allegiance to the government’s policy of eradicating prostitution.”