Higher gasoline costs helped to drive consumer prices up by 0.5 percent in May after a 0.6 percent increase in April, the Labor Department reported yesterday.

If prices continued to rise at the May rate for a full year, inflation would measure 6.7 percent, the department said. This compares with a 3.5 percent price rise over the past 12 months and an annual-rate rise of 3 percent for the first five months of this year.

Although consumer prices have risen sharply in the past two months, most economists expect inflation to average between 3 percent and 4 percent this year, following a 3.9 percent increase in 1982. During 1984, some economists predict, the CPI will rise by between 4 and 5 percent.

The unusually good price performance in the first three months of 1983 is unlikely to be repeated, however, officials conceded. Consumer prices rose by only 0.2 percent and 0.1 percent in January and March, and they fell by 0.2 percent in February.

Fluctuations in energy prices have played a big part in both the deceleration and speedup in inflation, economists say.

"Consumer price movements during the last several months are due to shifts in relative energy prices, not to any fundamental change in the rate of inflation," said Jerry Jasinowski, chief economist for the National Association of Manufacturers. "Now that inflation has stabilized in the 4 percent range, I expect it to remain near that level for the rest of the year."

White House deputy press secretary Larry Speakes said officials do not believe that yesterday's report "indicates any strong return of inflation. It is substantially below what it was a year ago and substantially below what it was when we took office."

Commerce Department chief economist Robert Ortner said "the next year and a half looks pretty good," with inflation likely to run at an annual rate of 4 to 5 percent. "Beyond that I think we're going to have to work very hard" to keep inflation under control, Ortner said.

A sharp decline in inflation has been the most significant economic achievement since President Reagan took office. However, it has come during the most severe recession since the Great Depression.

The challenge facing policy makers now is to keep inflation down while economic growth resumes and unemployment declines, analysts say. Speakes said that "a strong recovery . . . is being accomplished without substantial inflation. It is exactly what the program has been aimed at."

Reagan's chief economic adviser, Martin S. Feldstein, said this week that "there's no short-term worry about inflation." But he also said that if recovery is rapid and unemployment falls swiftly, then inflation may speed up again. "It's always very tempting to say the inflation problem is licked . . . but if the recovery is too strong, we might well see inflation rising again," he said.

The consumer price index for all urban consumers stood at 297.1 in May, with 1967 representing 100. That means that a basket of goods costing $100 in 1967 would have cost $297.10 last month. The index for urban wage earners and clerical workers stood at 296.3. Both of these index numbers are before seasonal adjustment.

Higher energy prices in May accounted for "somewhat more than half" of the total rise in the CPI, the Labor report said. Gasoline prices shot up by 4.2 percent last month, after a 4 percent increase in April.

The sharp increases in gas prices in the past two months came after the oil glut pushed gasoline prices down by "17.4 percent from their peak level of March 1981," the report said.

Transportation costs overall rose by 1.3 percent in May, the Labor Department said. Continued special financing programs for new cars helped to push down new-car prices, the report said. "The 1.8 percent decline in automobile finance charges was the tenth consecutive monthly decline," it said. However, used car prices rose by 1 percent during the month.

Medical costs, which have climbed sharply for the last several years, have slowed markedly in recent months. During May, these costs were up by 0.4 percent, after 0.5 percent increases in the two previous months. This compares with an average monthly change of 0.9 percent between December, 1980, and February of this year, the report said.

Within the medical costs category, charges for professional services and hospital rooms rose 0.5 percent and 1.2 percent, respectively.

Bad weather has pushed up fruit and vegetable prices in recent months. They climbed sharply again in May, accounting for about half of the 0.3 percent increase in overall grocery prices, the Labor Department said. In the past three months fresh vegetable prices have risen by 21.3 percent, it said.