Party chiefs breathed a huge sigh of relief as Kim Leadbeater confounded expectations to cling on to the West Yorkshire constituency with a slender majority of 323 over the Conservatives.
Their calculations had been complicated by the candidacy of the former Labour and Respect MP George Galloway, standing for the Workers Party, who targeted the seat’s substantial Muslim population with a pro-Palestine and anti-Starmer platform.
Labour had mounted a major “get out the vote” operation – backed up by concerted social media messaging – to see off the challenges from Tory candidate Ryan Stephenson and Mr Galloway.
Sir Keir would have been under huge pressure – and have faced a potential leadership challenge – if the party had suffered a second by-election loss to the Conservatives in the North of England.
After six months of poor opinion poll ratings and lacklustre election performances culminating in defeat in Hartlepool, Sir Keir now has breathing space to re-establish his authority over the summer.
Talk of challenges will melt away – for now – following intense speculation that Angela Rayner, his deputy, would attempt to replace him.
The next big test for Sir Keir will come in September when he delivers his first in-person conference speech as leader.
He could face a new round of sniping if the party is still struggling in the polls.
The party has also received a much-needed morale boost from Ms Leadbeater’s ability to cling on in a seat where Labour sources had put their chances of winning as low as 5 per cent.
A senior Labour source said: “Everyone’s been calling this a referendum on Keir’s leadership. Well, we’ve won, bucked the trend, held on to this marginal seat and advanced in Tory areas. A fantastic result.”
During a visit to Batley, Sir Keir insisted the easing of lockdown curbs would enable him for the first time properly to spell out party policies and present himself to electors in the run-up to the Labour conference in September.
The former Cabinet minister Lord Mandelson told BBC Radio 4: “We’re not pulling people towards us in sufficient numbers. We have to show we are a competitive, viable alternative to the Tories.”
Lord Mandelson said Sir Keir needed to use the Labour Party conference to “set out his stall properly”.
“He’s got to show and map out a way forward for the party and I’m confident he’ll do so,” he said.
Labour’s shadow Scotland secretary Ian Murray claimed Labour’s by-election victory showed the “politics of division has been defeated”.
He praised Ms Leadbeater, saying: "I'm delighted for Kim who will be a fantastic MP and this result shows that people want representatives who will work to bring people and communities together rather than setting them off against each other.
"The old politics of division has been defeated and there is a lesson for us all in that.
"While our opponents both in Scotland and across the UK want to focus on divisions between us, Labour will win by focusing on what unites us and delivering on community priorities."
The Edinburgh South MP also hit out at Mr Galloway, who had promised to eat his hat if Labour didn’t come third.
On Friday morning, dozens of placards and banners promoting Mr Galloway’s failed bid to topple Mr Starmer and the constituency’s Labour candidate were still plastered across lamp posts and buildings in the West Yorkshire town of Batley.
Mr Murray said: “Galloway’s disgusting and divisive politics have been rejected again by voters.
"While he is eating his hat he should reflect on why his attempt to tear a community apart has failed.”
The result is a personal triumph for Ms Leadbeater, with her sister Jox Cox having been the seat MP until her murder by a far-right extremist in 2016.
Speaking afterwards, Ms Leadbeater said: “She knew how capable I was. So she’d be absolutely delighted that I’ve got to this point.
“And if I can be half the MP that she was, then I’ll be doing a good job.”
Ms Leadbeater said she hoped to heal the divisions in the constituency after a gruelling by-election campaign.
She said: “Sadly we have seen some nastiness during this by-election campaign and there are some divisions that need to be healed. I think that if anyone can achieve that I can.”
Sir Keir had earlier claimed Labour’s triumph was a “victory of hope over division”.
Arriving in the constituency to celebrate, the Labour leader praised Ms Leadbeater’s courage in standing.
“This campaign has been tough because others have poisoned it with hatred, with division and finding difference and misinformation, with lies and harassment and threats and intimidation,” he told supporters.
“That that should have happened in Batley and Spen in all places is disgusting. That that should have happened to Kim of all people is unforgivable.
“All those who engaged in it and all those who didn’t call it out – they should be utterly ashamed of themselves.”
Conservative Party co-chair Amanda Milling said the result was “disappointing”, but insisted it was not a “great win” for Labour.
“This was always going to be really a tough battle for us,” she told Sky News.
"Governing parties don’t tend to win by-elections. This is a Labour hold, not a Labour gain.”