It’s official: The razor’s edge primary contest between GOP Senate candidates Mehmet Oz and David McCormick is headed to a recount, state elections officials announced Wednesday, ensuring that the victor in the closely watched race won’t be officially declared for at least two weeks.

The announcement came even as counties continued to tally lingering batches of ballots and the gap between the two candidates dwindled to fewer than 1,000 votes.

Speaking at a news conference in Harrisburg, acting Secretary of State Leigh M. Chapman said that Oz led McCormick by just 902 votes, or less than 0.08% of the more than 1.3 million ballots cast in their race. By Wednesday evening the margin had shrunk even further.

That put their contest well within the 0.5% margin of victory that triggers an automatic recount under state law. And as Pennsylvania’s 67 counties prepared to begin the retallying process as early as Friday, Chapman vowed the recount would take place “transparently, as dictated by law.”

“I know Pennsylvanians and, indeed, people throughout the country have been following this race attentively and are eagerly awaiting the results,” she said. “I thank everyone for their patience as we count every vote.”

» READ MORE: Pennsylvania’s heading to a recount in the Oz-McCormick Republican Senate primary. Here’s what to expect.

But even as the campaigns said they looked forward to a swift resolution, attorneys for both Oz, a celebrity TV doctor endorsed by former President Donald Trump, and McCormick, a formerchief executive of the world’s largest hedge fund, continued Wednesday to scrap both in court and before county elections boards in battles over small pockets of contested ballots they hoped would help cement a victory.

The recount in the McCormick-Oz race is only the seventh time one has been automatically triggered since the implementation of the 2004 law that required them in close races.

Over the next two weeks, elections officials will run all counted mail and in-person ballots through different scanning machines than those they used in their original counts to confirm their totals. They must complete this process by June 7.

At stake for the eventual victor is the chance to face off against the Democratic nominee, Lt. Gov. John Fetterman, for a six-year term to replace retiring Sen. Pat Toomey in the narrowly divided chamber.

As the recount progresses, the vote totals are likely to shift slightly. For example, in situations where a voter may have marked two different candidates on their ballot in the same race or had a stray mark that confused a machine, workers will be reviewing the ballot to determine the voter’s intent.

But even before the recount begins, McCormick still has a shot at picking up votes against Oz, thanks to a few thousand Republican votes left to be counted statewide in the next few days.

As of Wednesday, roughly 6,000 mail ballots had yet to be tallied, said Jonathan Marks, deputy secretary of elections. Roughly half of those votes are from overseas and military voters, whose mail ballots could arrive as late as Tuesday as long as they were postmarked by election day, May 17.

But only about 1,200 of the still outstanding mail ballots were from Republicans, the department confirmed Wednesday night.

Counties are also still making decisions on 4,000 so-called provisional ballots — or those set aside over questions about a voter’s eligibility. However, Marks cautioned, that figure includes both Democratic and Republican votes, meaning the actual number of provisional ballots still up for grabs in the Oz-McCormick race is likely much lower.

Meanwhile, McCormick continued to press his case in court Wednesday over an even smaller batch of contested ballots that previously would have been rejected under state law — fewer than 900 mail ballots from Republican voters that arrived on time but were missing the required handwritten date on their outer envelopes.

McCormick, who has outperformed Oz among mail voters, has sued seeking to have those undated ballots counted. He’s citing a recent ruling from the U.S. Court of Appeals for the Third Circuit that concluded the state’s dating requirements had no bearing on whether a vote was legitimately cast and that undated mail ballots should be counted in a case that was focused on the 2021 general election in Lehigh County.

The court has not indicated whether it intended for its ruling to be applied broadly to other elections, including this year’s primary vote. But state elections officials have advised counties to count those ballots and keep the tallies and ballots separate from the rest of the votes while they await further guidance from the courts.

“The right to vote is sacrosanct,” McCormick campaign attorneys Ronald L. Hicks Jr. and Charles J. Cooper wrote in court filings earlier this week. “The handwritten date on an exterior mailing envelope [of an] indisputably timely received absentee or mail-in ballot … is anything but.”

Responding Wednesday, Oz and the state and national Republican parties urged the Commonwealth Court to conclude — as it has done previously in other cases — that those ballots should be rejected. They dismissed McCormick’s legal wrangling as a “desperate attempt to scrounge up more votes.”

The court has scheduled a May 31 hearing on the matter. Meanwhile, McCormick has petitioned the state Supreme Court to take over the case.

The fallout from their legal battle has split top Republican officials who have previously made strict control over mail voting a central part of their party’s identity.

» READ MORE: David McCormick, Mehmet Oz, and the politics behind undated Pennsylvania mail ballots

In the meantime, counties across the state continued to open and count undated ballots.

In Philadelphia, which has found itself at the heart of the debate over undated mail ballots since 2020, the Philadelphia City Commissioners — a three-member board that oversees elections — voted Wednesday to include the 2,125 such ballots they had received.

Commissioner Lisa Deeley said she found vindication in the decision. In 2020, Philadelphia was one of several counties that counted undated mail ballots — drawing lawsuits from the Trump campaign.

Those cases ultimately split the state Supreme Court, which rendered a complicated decision. Three justices said undated ballots should be rejected, three said they could be counted, and one said they should only be counted in 2020 — when no-excuse mail voting was still new — and rejected in future elections once voters had had more time to become educated on the state’s requirements.

Republican leaders in Harrisburg vowed to impeach two Democratic members of the board, when Philadelphia’s commissioners chose to count undated mail ballots again in the 2021 primary. The threats prompted one of them, Omar Sabir, to reverse course and change his vote, ultimately resulting in the commissioners rejecting the undated ballots.

But as all three commissioners voted Wednesday to count the undated ballots in this year’s primary, Deeley noted she’d remained consistent in her stance from the start.

“I am not gonna lie,” she said Wednesday. “It was very satisfying to see the Department of State putting out guidance yesterday now saying to count the undated ballots.”

Seth Bluestein, the sole Republican commissioner, also voted Wednesday to count the ballots.

“We should not deny the right of any individual to vote in any election because of an error or omission that is not material,” he said, “in determining whether such individual is qualified under state law to vote in such election.”