Three and a half years ago, when Mayor Karen Bass of Los Angeles took office, the pandemic-worn city looked to her with hope and relief. Tent camps filled with homeless people had consumed parks, beaches and sidewalks. Melrose Avenue merchants had posted armed guards outside high-end sneaker outlets. Malls were bracing for smash-and-grab robberies.

On her first day, Ms. Bass declared an emergency, citing the sheer scope of the homeless situation. As months passed, the size of the unsheltered homeless population and violent crime rates both dropped. Commuters cheered when she tapped state and federal connections to fix and swiftly reopen a damaged freeway.

Then came the fires.

Two years into the job, while she was in Ghana attending the inauguration of a new president, Pacific Palisades erupted in flames, claiming 12 lives and destroying thousands of homes in the coastal enclave. She rushed back, demoted the fire chief and tried to explain that she wouldn’t have gone abroad if she had known what would happen.

Ms. Bass — a former congresswoman who in 2020 was on the vice-presidential shortlist — never fully recovered from that absence, and now she’s fighting for a second term. More than a dozen challengers are on the June 2 primary ballot, and the mayor, 72, is widely expected to face a runoff in the fall.

A standoff last summer with the Border Patrol and Immigration and Customs Enforcement over the Trump administration’s deportations bolstered the mayor’s approval ratings citywide, but lawn signs calling for her resignation still dot the Palisades.

Ms. Bass is facing two main challengers from opposite ends of the political spectrum: Nithya Raman, a liberal City Council member and former ally of the mayor, and Spencer Pratt, a Republican reality television star whose house burned in the Palisades fire. Both Ms. Raman and Mr. Pratt have accused Ms. Bass of mismanaging Los Angeles, the country’s second most populous city, on issues ranging from the fire recovery to streetlights.

“This has been a campaign about fire and ICE — and so far, the fire is winning,” said Dan Schnur, a longtime political analyst who teaches at the University of California, Berkeley, and the University of Southern California. “That’s not to say she can’t win, but it’s pretty rare for a politician to get a second chance to make a first impression in a city like Los Angeles that doesn’t pay much attention to politics.”

Public-opinion surveys and internal polling shared by her campaign show the mayor’s once formidable support has diminished. An annual survey by the Luskin School of Public Affairs at the University of California, Los Angeles, found that 40 percent of likely voters remained undecided as recently as March, a red flag for an incumbent.

Los Angeles is overwhelmingly liberal — Democrats alone represent more than 55 percent of registered voters. But a growing minority of the city’s elected leaders has risen from Ms. Raman’s progressive wing of the party. And on the wealthy Westside, the MAGA right that Mr. Pratt has galvanized has become increasingly vocal, amplified by coverage in The New York Post, which started a California edition this year in time for the midterm elections. (“Shocking Scenes From L.A.’s Homeless Apocalypse,” one recent headline read.)

On Wednesday night, the mayor faced off against Ms. Raman and Mr. Pratt on a debate stage at the Skirball Cultural Center, just a few miles northeast of the Palisades fire zone. Ms. Bass and Ms. Raman sparred over housing policy and city spending while Mr. Pratt, a tabloid media fixture, invoked drug users, squatters, tortured dogs, fraud and people burning alive.

“This is a MAGA Republican’s idea of what Los Angeles looks like,” Ms. Raman said at one point with a sigh.

When the mayor fact-checked an assortment of erroneous claims about the fire, Mr. Pratt called her “an incredible liar,” and the moderator interjected: “No name-calling!”

Since Ms. Bass was sworn into office in December 2022, the city has experienced one emergency after another. Pandemic tent camps. Busloads of migrants sent by the Republican governor of Texas during a tropical storm. School strikes. The threatened collapse of part of the Interstate 10 freeway.

The Palisades fire hit as the Trump administration prepared to take office. Then came the ICE raids and, after protests downtown, thousands of federal troops. Even the preparations for the 2028 Olympics were thrown into crisis mode after revelations surfaced about the chairman of the city’s organizing committee and his ties to Jeffrey Epstein.

In an interview last week at the Getty House, the official mayoral residence where she lives with three grandchildren and other relatives, Ms. Bass said she hoped voters would keep in mind her success in tackling some of those earlier challenges, and in doing the hard work of building consensus. Last month, President Trump, who has repeatedly bashed her, met with her and a Republican county supervisor, Kathryn Barger, and agreed to their request for additional financial assistance for fire victims.

The fallout of the last few years, she acknowledged, has taken a toll both on the city and on her own political situation.

“Obviously what happened in the Palisades has hit me,” she said, leaning in from a fireside chair in the wood-paneled office. But she added, “I think we’re dealing with a very disgruntled electorate. Period.”

Ms. Raman, 44, won her City Council seat in 2020 with the support of the Democratic Socialists of America, which also helped to elect Mayor Zohran Mamdani of New York. Once a supporter of Ms. Bass’s policies on homelessness and housing, Ms. Raman endorsed the mayor before entering the race at the last minute.

With a screenwriter husband and degrees in political theory and urban planning from Harvard and the Massachusetts Institute of Technology, Ms. Raman has been compared by both critics and admirers to Mr. Mamdani. At the Wednesday debate, however, her performance was far more halting than that of the polished mayor of New York. She has also downplayed her D.S.A. support and focused her campaign on the maintenance of streetlights and other basic services, promising “a city that works.”

“I’ve been to Boyle Heights, I’ve been to San Pedro, I’ve been to Brentwood,” Ms. Raman said in an interview, listing a diverse range of the city’s neighborhoods. “What has been interesting is that in all of those neighborhoods there’s been a feeling of a lack of urgency and focus, a lack of action, and a feeling that things are getting worse rather than better.”

The mayor has countered with a series of infrastructure programs, saying that the city had a decade-long backlog of streetlight repairs when she took office, and that Ms. Raman, who has served more than six years on the City Council, has had “twice as long as I have” to address issues like potholes and housing. Ms. Raman has responded by suggesting Ms. Bass is simply passing the buck.

“She is the leader of the city, and I think people understand the difference between being one council member out of 15 and being the mayor,” Ms. Raman said.

Mr. Pratt, 42, is best known for his role on the MTV reality TV series “The Hills” as the angry boyfriend of Heidi Montag, who is now his wife. He has spent much of his post-“Hills” life claiming that his vindictive, opportunistic personality on the show was a fiction created by the producers. In past interviews and in his new memoir, “The Guy You Loved to Hate,” Mr. Pratt has said he leaned into the persona for fame and money.

Since the Palisades fire, he has criticized Ms. Bass, Gov. Gavin Newsom and other Democratic leaders for his property losses. In campaign ads, he asserts that city officials “let my home burn down.”

He fought a bill to increase density near transit stops that he feared would allow lots in the Palisades to be filled with high-rises and subsidized housing. Otherwise, he has said little until recently about problems affecting the four million or so Los Angeles residents outside his neighborhood.

“I’ve always told people, they go: ‘Why do you live in California? Why do you live in L.A.?’” Mr. Pratt said in an interview last year during a trip to Washington to ask for a congressional investigation into the fire’s handling. “And I say, ‘I don’t — I live in the Palisades.’”

Last week, he declined an interview via text message, writing that he had only one comment: “Protecting our mothers and children is my top priority and it’s a winning message.”

On his campaign website, he says that, if elected, he will make long-term city assistance for homeless people contingent on sobriety and mental health treatment, and that he will push for an audit of the Los Angeles Department of Water and Power, which he and Ms. Montag are suing over the fire.

Online, his sister, Stephanie Pratt — who also appeared on “The Hills” — posted that “a vote for him is a vote for stupidity,” and said that her brother was merely clinging to the spotlight with his campaign.

The Daily Show” produced a reality TV highlight reel of celebrities running for office that included footage of Mr. Pratt lashing out at his sister on camera. “Wah, wah, wah, wah — what are you crying about, Stephanie?” Mr. Pratt said in one moment, mocking her tears.

Ms. Raman reposted the “Daily Show” segment to her campaign’s Instagram.

Each candidate has local celebrities who have given the maximum $1,800 individual contribution to their campaigns: Jeanie Buss, the former owner of the Los Angeles Lakers, to Mr. Pratt; the actor Mindy Kaling to Ms. Raman; and the director and producer J.J. Abrams to Ms. Bass.

In a less star-studded section of Los Angeles, Mikayeel Khan, 39, the president of the Pacoima Neighborhood Council, said that he had not made a decision yet. But he said it might help if City Hall were more responsive to his working-class neighborhood.

Pacoima “needs a lot of attention,” he said. “The city doesn’t know what’s going on.”

Emily Baumgaertner Nunn contributed reporting.