China’s former premier Li Keqiang died early on Friday of a heart attack, an abrupt end to the life of a leader who had served alongside Xi Jinping for a decade and set a milder tone while never appearing to directly challenge Mr. Xi’s hard-line policies.

Mr. Li, 68, was visiting Shanghai when he suddenly suffered the heart attack, a report on Chinese state television said. “All efforts to resuscitate him failed,” the report said.

Mr. Li was once considered a potential top leader of the ruling Chinese Communist Party. But in the end, he was overtaken by Mr. Xi, and became China’s premier — the leader of the cabinet — in 2013. Over the following 10 years, he steered Chinese government ministries, but appeared to wield less power than previous premiers, with Mr. Xi dominating policy instead.

Mr. Li, who had a doctorate in economics, exemplified a generation of highly educated Chinese leaders who rose quickly in the 1980s and 1990s as Mao Zedong’s generation faded from politics.

But he and many of his contemporaries lost much of their influence in the past decade, as Mr. Xi consolidated power and promoted an inner circle of Communist Party loyalists whom he has known for decades.

Before Mr. Xi became the top leader, “officials like Li Keqiang did very well and continued the institutionalization of the party,” said Victor Shih, a specialist in Chinese factional politics at the University of California at San Diego. But Mr. Li and many other professionals “were not as practiced in the ruthless politics that always has characterized elite politics,” he said.

Born in Anhui Province in eastern China, Mr. Li studied law and economics at Peking University, where he mixed with liberal students who later threw themselves into the pro-democracy movement of 1989. But Mr. Li chose another path, rising through the Communist Party’s Youth League.

He later served as a provincial leader and was seen as a potential successor to Hu Jintao, the Chinese leader who also rose through the Youth League. In the end, Mr. Xi emerged as the top leader in 2012, and Mr. Li worked in his shadow.

As premier, Mr. Li sought to reduce government red tape and interference in the private economy. But Mr. Xi dominated the major decisions.

Mr. Xi had muscled Mr. Li aside on a broad range of policy issues over the past decade. Mr. Xi created a series of Communist Party commissions to make policy on issues like national security, the economy and finance, supplanting much of the policymaking role once played by government ministries, which reported to Mr. Li as the premier.

Mr. Li “must have lived a life of frustration and unhappiness for the past 10 years,” said Barry Naughton, another specialist in Chinese politics at the University of California at San Diego. “He had been so systematically deprived of power and influence by Xi Jinping that I don’t think his passing will have much impact on the political situation in China.”

Chinese internet users expressed shock at the news of Mr. Li’s death on Weibo, China’s Twitter-like social media platform.

“He will always live in our heart,” one commenter wrote. “This is too shocking,” wrote Hu Xijin, the former chief editor of Global Times, a Communist Party newspaper. “Deep condolences.”

Shi Feike, a lawyer and former investigative journalist, said that some people, like him, would remember Mr. Li for his liberal leanings and his advocacy of market reforms.

“He may not have been a strong and forceful politician, nor a proficient public speaker,” Mr. Shi wrote in a post on the social media platform WeChat. “But in my impression, almost all his public expressions were closely related to keywords such as democracy, rule of law, market economy and government streamlining.”

“May this elderly man who was aligned with the direction of his time rest in peace!” he wrote.

Deaths of leaders in China are regarded as politically sensitive, with the potential for setting off unrest, raising the question of how Mr. Xi and the Communist Party will commemorate Mr. Li.

Over the past year, the authorities have been quick to quash signs of dissent, especially as China grapples with economic challenges, a housing crisis and high youth unemployment. Late last year, residents and students in various Chinese cities held protests against Covid restrictions that were the boldest challenge to the party’s rule in decades and an unusual affront to Mr. Xi.

For some Chinese, the news of Mr. Li’s death on Friday carried echoes of the death in 1989 of Hu Yaobang, a relatively liberal former Communist Party leader who succumbed to a heart attack during a party meeting. His death was met with a wave of public grief that grew into the Tiananmen pro-democracy protests.

But Mr. Xi, China’s most powerful leader in decades, has sought to exercise tight control over social media, universities and society at large. Mr. Li was not seen as the same transformative figure that Mr. Hu had been in the 1980s, said Joseph Torigian, a research fellow at the Hoover Institution at Stanford University.

Still, Mr. Torigian added, Mr. Xi will want to handle the mourning carefully, including the official obituary and the party’s assessment of Mr. Li’s role.

“I’m sure that this will lead to questions about whether the death of Li Keqiang was partly the result of him feeling frustrated and depressed about the way he was treated, or the way the country is going,” Mr. Torigian said.

“The country, broadly, is in a state of malaise, like it was in 1989, and this is definitely out of the blue,” he said. But, he added: “The leadership will keep a very close eye on whether people try to use this as an opportunity to create trouble for them.”

Claire Fu, Amy Chang Chien and Li Yuan contributed reporting.