Trade surplus came in at $1.59 billion in the January-June period, swinging from a government forecast of $43 million deficit, customs data showed.

Vietnam's exports in the six-month period rose 7.2% to $122.53 billion from a year earlier as shipments to the United States surged 27.4% and South Korea rose 16.8%, according to the Customs Department.

Smartphones, mostly made by South Korea's Samsung Electronics, were Vietnam's largest export earner in the first half, rising 3.9% to $23.5 billion from a year ago. Other key exports included electronics appliances and garments.

Vietnam's two largest trading partners are the United States and China, which are embroiled in an escalating trade war.

While Vietnam is enjoying a widening trade surplus with the United States, its trade deficit with China continues to grow, prompting the Trump administration to threaten tariffs on products from Vietnam.

Its trade surplus with the U.S. widened to $17.1 billion in the first five months of this year from $12.94 billion a year earlier. Its trade deficit with China widened to $16.29 billion from $11.05 billion.

Last month, Trump said Hanoi treated the United States "even worse" than China. The U.S. Commerce Department said last week it would slap tariffs of up to 456% on certain steel produced in South Korea or Taiwan, which are then shipped to Vietnam for minor processing and finally exported to the United States.

Vietnam responded by saying it was committed to free and fair trade with the United States and asked its manufacturers to use local materials to avoid incurring U.S. tariffs.

In June, Vietnam recorded a trade surplus of $1.93 billion, bigger than a government forecast of $400 million surplus, customs data showed.

Exports in June fell 2.2% from the previous month to $21.43 billion, while imports were down 15.9% to $19.49 billion, the Customs Department said.

(Reporting by Khanh Vu; Editing by Arun Koyyur)

By Khanh Vu