CAPE TOWN, South Africa (AP) -- For the first time in 30 years, South African lawmakers will elect a president with the outcome not a mere formality as unprecedented coalition talks between parties continued until right before a new Parliament convened on Friday.

Cyril Ramaphosa is seeking a second, five-year term as leader of Africa's most industrialized economy but his African National Congress party has been weakened after losing its long-held majority in an election last month. He will need the support of other parties if he is to return as president.

The ANC is hoping that a general coalition agreement with others -- particularly the main opposition Democratic Alliance -- will hold and they will back Ramaphosa's reelection. The ANC needs lawmakers from parties that were once its main political foes to now support Ramaphosa and continue the ANC's three-decade hold on the presidency.

The ANC announced late Thursday that it had a coalition agreement in principle with the DA and other smaller parties. ANC Secretary-General Fikile Mbalula said the final details of the agreement were still being worked out. The DA, the second largest party in Parliament behind the ANC with a potentially decisive number of lawmakers, said talks on the details had continued through the night and into early Friday, just before Parliament convened at 10 a.m. local time.

The 71-year-old Ramaphosa will be reelected automatically if no other candidates are nominated for president. No one else has so far been put forward, though lawmakers can nominate a candidate during Friday's Parliament session, the first since the landmark May 29 national election. Ramaphosa smiled and shook hands with members of his party as he arrived before taking his seat.

The sitting was being overseen by the chief justice and Parliament first swore in hundreds of lawmakers for a new term before it could elect a speaker, a deputy speaker and then the president. It was likely to take hours. The 400-seat lower house of Parliament, called the National Assembly, will vote by secret ballot for all those positions if more than one candidate is nominated. A majority of votes cast is required.

The ANC had faced a deadline to cobble together a coalition agreement of some sort given Parliament must sit for the first time and vote for the president within 14 days of the election results being declared.

South Africa has not faced this level of political uncertainty since the ANC swept to power in the first all-race election in 1994 that ended nearly a half-century of racial segregation.

The party had held a clear majority in Parliament since then, meaning parliamentary votes for the president were formalities and every South African leader since has been from the ANC, starting with Nelson Mandela. Last month's national election changed that as the ANC's share of the vote slumped to 40%. The DA won the second largest share of the vote with 21%, making it a key party in the coalition talks.

The ANC had proposed forming a government of national unity in the wake of the election and invited all of the other 17 parties that won seats in Parliament to join. Some have refused.

An ANC-DA agreement was seen as complex given their ideological differences. The two have been at odds as ruling party and main opposition for years. The DA is also a white-led party in a country where more than 80% of the population are Black, and that had led to some scepticism of a deal between them and the ANC.

The unity government that's expected to take South Africa forward also harked back to the way Mandela, South Africa's first Black president, invited political opponents to be part of a new unity government in 1994 in an act of reconciliation.

The ANC's hand has been forced this time.

At least one party, the MK Party of former ANC leader and South African President Jacob Zuma, said it would boycott the first sitting and its 58 lawmakers would not take their seats. That did not affect the voting procedure as South Africa's constitution says that at least one third of the 400 lawmakers need to be present for a quorum and for votes to take place.

Parliament also convened in an unusual setting after a fire in 2022 gutted the National Assembly building in Cape Town. It has not yet been restored and so lawmakers will decide the next leader of their country at a conference center near the city's waterfront.