The government appealed that ruling to the United States Court of Appeals for the Second Circuit, with a prosecutor, Justin Anderson, telling the three-judge panel in an oral argument that it was not Judge Gardephe’s role “to see himself as a 13th juror in the trial and decide whether his view of the evidence was in line with the jury’s view of the evidence.”

On Thursday, the Second Circuit, by a vote of 2 to 1, affirmed Judge Gardephe’s decision overturning the conspiracy conviction, calling his opinion “thorough and thoughtful.”

“This does not mean that fantasies are harmless,” Judge Barrington D. Parker Jr. wrote for the majority. “To the contrary, fantasies of violence against women are both a symptom of and a contributor to a culture of exploitation, a massive social harm that demeans women.”

“Yet,” Judge Parker added, “we must not forget that in a free and functioning society, not every harm is meant to be addressed with the federal criminal law.”

Dawn Dearden, a spokeswoman for Preet Bharara, the United States attorney for the Southern District of New York, said the office had no comment on the appeals court’s decision.

In its ruling, the panel also reversed Mr. Valle’s conviction on the charge that he had illegally gained access to the law enforcement database, which Judge Gardephe had upheld, and which the defense had appealed.

In a statement, Mr. Valle’s federal public defenders, Julia L. Gatto, Edward S. Zas and Robert M. Baum, said the “ruling is a very important victory not just for Mr. Valle, who has now been cleared of all criminal charges, but for an open society that treasures freedom of thought and expression.”