Jenna Coleman planned to quit Doctor Who last year, the show's chief writer Steven Moffat has said.
The actress was widely rumoured to be leaving her role as Clara Oswald after a cliffhanger in the series eight finale, Death In Heaven.
Moffat confirmed to Doctor Who magazine she had asked to be written out.
"That was her last episode," he said. "Then she asked me if she could be in [the] Christmas [episode]. So I said, 'OK, I'll write you out in Christmas.'"
"She came to the read through and did the 'write out' version - and again changed her mind."
"But the truth is I never wanted her to go. And with Last Christmas, I'd already written the alternative version where she stayed, and I preferred that version.
"Frankly, I didn't want to lose her. She's an amazing actress, and she never stops working to make Clara better.
"I was very happy to go the extra mile to make sure we could keep her."
Coleman, who began her career in ITV soap Emmerdale, will now return for a third series as the Doctor's companion.
It makes her the longest-serving assistant of the show's modern era, ahead of Karen Gillan and Billie Piper.
Confirming she would return to the show last December, the 28-year-old, from Blackpool, said: "I couldn't walk away with the story being unresolved.
"The arrival of the 12th Doctor has just kind of dropped this whole bombshell and allowed the dynamic to totally change, so I think just when Clara was feeling more comfortable in the relationship, it has suddenly thrown something new up."
Peter Capaldi, who plays the titular Time Lord said: "I'm thrilled. Jenna has just been fantastic and such a pleasure to work with."
The show is due to return to the BBC this autumn.