Henry Haller’s entree to the White House came in late 1965, after the executive chef hired by the Kennedys had quit, finding it beneath his dignity at long last to prepare food like the spare ribs, spoon bread and mashed garbanzo beans requested by the subsequent White House occupants, Lyndon and Lady Bird Johnson.

Mr. Haller, a pragmatic and versatile Swiss-born chef, had impressed Johnson by preparing meals for him at the Ambassador Hotel during the president’s trips to Manhattan as a senator. He got the job and would go on to become the longest-tenured executive chef in White House history.

From 1966 until his retirement in 1987, Mr. Haller catered to five presidents of varying politics, temperaments and palates, whipped up comfort food for their families, oversaw 250 state dinners and endured several tempest-in-a-fondue-pot controversies.

Mr. Haller, who lived in the Washington suburb of Gaithersburg, Md., died on Nov. 7, his family said. He was 97.

The White House kitchens were originally run by slaves, then an assortment of military stewards and mostly unremarkable professional chefs, each brought in by whatever president was in office. That all changed in 1961, when Jacqueline Kennedy reorganized management of the Executive Mansion to reflect its status as an international showplace. She hired the French-born René Verdon as White House chef, the one who lasted two years into Johnson’s presidency before resigning in frustration.

Stellar cooking skills were by then a prerequisite for the job. What set Mr. Haller apart was his flexibility — culinary, personal and managerial — which allowed him to thrive in the hottest kitchen of all. As he told the historian Richard Norton Smith for an oral history project in 2010, “Whatever they wanted, that’s what they’re going to get.”

Mr. Haller was typical of the Swiss, his wife, Carol Itjen Haller, said in a phone interview. “With an Englishman, they act one way,” she said. “With a Frenchman, they’ll act another way. They are an adjustable people.”

Henry Haller was born on Jan. 10, 1923, in Altdorf, Switzerland, near Lake Lucerne, to Emile Haller, a factory supervisor who was active in the local Red Cross, and Rosa (Furter) Haller, who cooked with vegetables harvested from the family vegetable patch.

His father told him that a life in the kitchen would allow him to travel the world, Ms. Haller said. After serving a stint in the Swiss army, he attended the prestigious culinary training school at the Hotel des Balances in Lucerne, which led to a job as chef in the five-star Hotel Bellevue Palace in Bern.

Like many other young Europeans right after World War II, Mr. Haller saw a brighter future in the New World. He made his reputation as a superb sous-chef in Phoenix before moving to New York, where he rose to top positions in hotel restaurants, which were hotbeds of culinary stardom before the era of celebrity chefs.

He met his wife-to-be in the early 1950s, when both were working summer jobs in Martha’s Vineyard.

The Johnsons would test Mr. Haller’s mettle. They were proud of Texas cooking and encouraged the use of canned and frozen food to save money. (Mr. Verdon, his predecessor, would have none of it, complaining to a reporter, “You do not serve barbecued spareribs at a banquet with the ladies in white gloves.”)

Mr. Haller saw no reason a chef couldn’t steer a middle course between haute and down-home. He was unfazed by Mrs. Johnson’s warning, during his one-hour interview for the job in late 1965, that pleasing the president would not be easy,

Things got off to a shaky start. Early in his tenure, Mr. Haller presented Johnson with a plate of Florida pole beans but forgot to remove their stringy stems. He was summoned to the dining room, where the leader of the free world handed him a fistful of stems. Mr. Haller pocketed them and padded off.

“If you wilt, you die,” said Sam Kass, who was a nutrition adviser for President Barack Obama and his family, describing the demands of the job.

A trim, gray-templed figure almost seldom seen without his toque blanche, Mr. Haller quickly found his footing. A short time after the bean incident, Mr. Haller whipped up an elegant lunch for a gathering of foreign dignitaries on a few hours notice and was rewarded with a note of appreciation from the president.

“When I hired you, I certainly did not intend you to be a short-order cook,” Johnson joked.

Johnson ate heartily and encouraged Mr. Haller to chat up the press. But Mr. Haller’s next boss, President Richard M. Nixon, was quietly obsessed with his waistline and demanded lighter fare, not to mention greater secrecy.

Early in Nixon’s presidency, Mr. Haller let it slip in public that the president was not only fond of martinis but that he also liked to mix them himself — inviting a rare rebuke from the president’s political staff.

Mr. Haller was never close to the Nixons, but he tried to accommodate their needs. The first lady, Patricia Nixon, was a light eater who tended to consume even less when under stress, so Mr. Haller worked with her two daughters to create a menu of items that she would be more likely to eat, his wife recalled.

Mr. Haller himself proudly recalled that at 7:30 a.m. on Aug. 9, 1974 — hours before Nixon’s resignation was to take effect — the president, barefoot in his pajamas, walked into the kitchen, grasped Mr. Haller’s hand and said, “Chef, I have eaten all over the world, but your food is the best.”

Gerald R. Ford’s presidency was relaxed and comparatively uneventful for Mr. Haller. The Carters, who came next, were frugal, friendly and unfussy — but presented Mr. Haller with arguably his most daunting challenge ever: A dinner for 1,300 on the White House lawn to celebrate the Camp David accords in 1978. It had to be organized in a week.

Mr. Haller’s job changed dramatically in 1981 with the arrival of Ronald Reagan and his wife, Nancy. The new first lady saw White House event planning as central to giving her husband’s presidency a kind of cinematic quality.

She attended to the smallest details personally, and instituted a system of what Mr. Haller called “tryout” menus for state dinners, reviewing the results with him. “We take pictures with a Polaroid so the staff knows how they are to be done,” he told an interviewer. “With the Reagans, you have to be more creative.”

By 1987, as the Reagans prepared to leave Washington, Mr. Haller decided it was time to move on, too. He had raised four children on a modest federal salary and wanted to earn more by delivering speeches and working for food and beverage companies. Before he left the White House, though, he published “The White House Family Cookbook” (1987), which was heavy on recipes and light on gossip.

Along with Ms. Haller, he is survived by two sons, two daughters and five grandchildren.

In his later years, Mr. Haller — a stickler for fitness who limited his children to one dessert per week — indulged his passions for travel, skiing and photography. But he never strayed far from the stove.

“There are two kind of professional chefs,” his wife recalled. “There’s the kind that comes home and eats what his wife makes for dinner, and the kind like Henry, who was always in the kitchen saying, ‘You are not doing it right!’”