Testimony laced with “a particularly noxious strain of racial prejudice” in a Texas death penalty case required a new sentencing for the defendant, Duane Buck, the Supreme Court ruled on Wednesday.

The testimony came from a psychologist who said black defendants were more dangerous than white ones. Chief Justice John G. Roberts Jr., writing for the majority in a 6-to-2 decision, said the psychologist’s report “said, in effect, that the color of Buck’s skin made him more deserving of execution.”

“Our law punishes people for what they do, not who they are,” Chief Justice Roberts wrote. “Dispensing punishment on the basis of an immutable characteristic flatly contravenes this guiding principle.”

Mr. Buck was convicted of the 1995 murders of a former girlfriend, Debra Gardner, and one of her friends, Kenneth Butler, while Ms. Gardner’s young children watched. Texas law allows death sentences only if prosecutors can show the defendant poses a future danger to society.