A champion in the Spanish capital in 2024, Swiatek rebounded from a one-set deficit to level the contest but put an end to proceedings while down 0-3 in the decider.
The Polish six-time Grand Slam champion appeared to be struggling and asked for the doctor after getting broken early in the third set.
Following a conversation with the trainers, Swiatek tried to break Li back, but when her opponent held for a 3-0 advantage, the fourth seed realised she was unable to continue the match.
She revealed after the match that she has been fighting a virus for the last two days and was hoping she could still find a way to win despite feeling "terrible".
"I heard there is something going on between players, that the virus is somewhere on site. I'm sure I'll be fine in a couple of days, but I had zero energy and zero stability and just felt really bad physically, and yesterday even worse," said Swiatek.
It is Swiatek's earliest ever exit in five appearances in Madrid.
"I knew that it's going to be hard, but I still wanted to try," she added.
"On the court before the tournament I felt like I'm playing great, so actually it's sad for me that I can't play, because I was feeling really good with my game, and I was moving forward in the process, so, this was positive. But for me the tournament has just started and I couldn't even compete today, so it's disappointing."
Earlier in the day, 15th-seeded Iva Jovic squandered a lead and fell 3-6, 6-3, 6-2 to Canadian 24th seed Leylah Fernandez, who will be Li's opponent in the last 16.
Wins for Andreeva and Zverev
Ninth-seeded Russian Mirra Andreeva overcame an inspired Dalma Galfi 6-3, 6-2 in a match that was much closer than the scoreline suggests.
The Hungarian qualifier ranked 117 in the world broke early in both sets but was unable to maintain her advantage as Andreeva recovered to improve her clay-court record this season to 9-1 and set up a last-16 showdown with another Hungarian, Anna Bondar.
"Today it was a very tricky match, Dalma is a very tricky opponent to play against," said Andreeva, who turns 19 on Wednesday.
"In the first set, I found myself on the back foot a little bit. She started well, hitting a lot of winners, being very solid and consistent. So I told myself that, 'If she wants to beat me today, that's the level she has to play the whole match'. And I told myself, 'It's okay, even if now I don't feel like I'm playing my best, slowly I'll get there and figure out'.
"I'm just super happy that I won in straight sets today."
Follow the women's side in Madrid here.
In ATP action, defending champion Casper Ruud raced into the third round with a 6-0, 6-1 demolition of home favourite Jaume Munar in just 65 minutes.
The 12th-seeded Ruud leads the ATP Tour in clay wins (133) and titles (12) since the start of the 2020 season and bids to become the third man to defend the Madrid crown after Rafael Nadal (2013-14) and Carlos Alcaraz (2022-23).
Second-seeded Alexander Zverev recovered from a "terrible" second set to begin his campaign with a 6-1, 3-6, 6-3 performance against recent Bucharest champion Mariano Navone.
"First set was almost perfect, and then I lost focus completely, and then the second set was terrible," admitted Zverev, who previously clinched the trophy in Madrid in 2018 and 2021.
"But that's what happens sometimes, first match of a tournament. I definitely have to focus a bit better, but the level was there. I mean, the first set and the third set was great tennis, and I just have to focus on that."
Zverev will next square off with Terence Atmane, who overcame excruciating cramps to triumph in an all-French duel 7-6(3), 7-6(5) over 30th seed Ugo Humbert.
