The judge in the trial of South African Paralympic athlete Oscar Pistorius has ordered that he should undergo a month-long mental evaluation.
She ruled that psychiatric evidence before the court could not replace “a proper inquiry” into his mental health.
Correspondents say the trial, which began in March, will almost certainly now face a lengthy delay.
The prosecution argued on Tuesday that psychiatric tests were essential and that he should be referred.
But the defence vigorously opposed the move.
Mr Pistorius denies intentionally killing his girlfriend Reeva Steenkamp on Valentine’s Day last year.
He says he accidentally shot her through the toilet door in a state of panic, mistaking the 29-year-old model and law graduate for an intruder.
The BBC’s Andrew Harding in court says that Judge Thokozile Masipa’s decision caught almost everyone by surprise.
The trial has now been adjourned until Tuesday, when the judge will give the details behind her ruling which could mean the Olympic athlete is evaluated for a minimum of 30 days at a psychiatric faculty.
Mr Pistorius’s uncle, Arnold, welcomed the judge’s “thorough” ruling and said it gave the family “confidence” in the justice system.
Judge Masipa said that the criminal code stipulates that if an accused person is alleged not to be criminally responsible or is alleged to be mentally ill, he should be evaluated.
She said that such an evaluation would ensure that Mr Pistorius would get a fair trial, adding it would be “preferable” for him to undergo the evaluation as an outpatient.
Prosecution lawyer Gerrie Nel made the referral application on Tuesday after forensic psychiatrist Merryll Vorster said earlier that the double amputee was suffering from generalised anxiety disorder (Gad).
Mr Nel said that the disorder diagnosis left him with no choice but to apply for an evaluation because the condition might have affected Mr Pistorius’ behaviour on the night he killed Ms Steenkamp.
Legal experts say that the case may well hinge on the judge’s understanding of the athlete’s state of mind when he pulled the trigger.
They say the prosecution is keen to show that the defence keeps changing its reasons why Mr Pistorius fired his gun – from putative self-defence, to accidental shooting, and now to something linked to his anxiety disorder.
There are no juries at trials in South Africa, so the athlete’s fate will ultimately be decided by the judge, assisted by two assessors.
If found guilty of murder, Mr Pistorius could face life imprisonment. If he is acquitted of that charge, the court will consider an alternative charge of culpable homicide, for which he could receive about 15 years in prison.