Viviani edged Alexander Kristoff and Caleb Ewan who were second and third on a 1.5km home straight with all the top sprinters and their lead-off trains, were active over a nervy finale.
In a fast snaking finish in which speeds nudged nearly 60mph, Thomas and Ineos team-mate Egan Bernal stayed out of trouble as Viviani was led to the line by team-mate and current race leader Julian Alaphilippe of France, victor of Stage 3 in Epernay.
The reduced peloton finished 26 seconds later with defending champion Geraint Thomas conceding a further five seconds to the new leader.
"Then it was just a case of getting to the finish and being safe", said Thomas, whose Ineos team did much of the chasing.
Comedian Brendan Grace Has Died
His family confirmed the news to RTE, and said they were "very grateful" for the support over the world from his fans. In recent years, he divided his time between Ireland, running a pub in Killaloe, Co Clare, and his home in Florida.
"It is what it is", Thomas said. But with Thursday's final climb to La Planche des Belles Filles looming on the horizon, speculation over the Welshman's form continues following Monday's finish in Épernay when he lost five seconds to Egan Bernal in the uphill finish.
"Obviously it is nice not to lose five seconds", he said.
But Ineos team principal Sir Dave Brailsford dismissed the gap as irrelevant. "If anyone understands the sport, you watch the sport, there is 30-metre gap". It makes no difference'.
The Tour crossed into France earlier in the stage after opening with two stages in Belgium. "This means a lot to me". Probably, I can't believe it.
"It was my biggest goal of the year".
Futures Down Ahead of Fed News
Trump, who is counting on a strong economy as he heads into a re-election campaign, has called the Fed his biggest threat. Trump's attacks have raised alarms that he is undermining the Fed's long-recognized independence from political pressure.
"Julian's great ride yesterday and him taking the yellow jersey really switched on the team.it gave us the edge". But I think we rode well as a team, we were always in a good position and communicating really well, so it's a good day. Today we did a ideal job, you saw how the lead-out did.
Viviani had won 73 races before the high-speed sprint into Nancy and an Olympic gold medal on the track in Rio, yet he had still to win a sprint at the Tour de France.
Viviani has now posted stage wins at all three Grand Tours, including four at the Giro and three at the Spanish Vuelta.
The rest of the peloton were simply happy to make it through a sketchy final five kilometres unscathed, with the route designers sending them barrelling down a wide dual carriageway before a roundabout funnelled them into a tight left-hander two kilometres from the line.
Chiefs QB Patrick Mahomes wins Best NFL Player at the ESPYs
The epic MNF game between the Rams-Chiefs ended with 105 points and 1,001 yards. Mahomes is up for "Best Male Athlete" and "Best NFL Player".