The House of Lords has voted to guarantee the rights of EU nationals living and working in the UK in an amendment to the Brexit bill.
Before voting by 358 to 256 to remove the question of the rights of foreign workers, peers debated the issue and accused the government of treating EU nationals like "bargaining chips".
However, the government has said it would seek to overturn the defeat for its draft legislation when the bill returns to the House of Commons.
The amendment would provide reassurance for some three million Europeans resident in the UK by guaranteeing their status and residence rights after Brexit.
Although the government has said it wanted to guarantee the rights of EU citizens in the UK and British citizens living in Europe, it is keen not to weaken its position ahead of Brexit negotiations.
But the Lords have sent a message that the matter should be addressed immediately rather than leaving EU nationals working in the UK in limbo over their future.
One conservative to back the amendment was Lord Hailsham (Douglas Hogg). He suggested that denying EU citizens the right to stay in Britain could face legal challenge.
He told the chamber of a conversation with a French waitress, who had asked him what would happen to her once Brexit happened.
He said: "I gave her my personal opinion, which was that there would be no problem for her, but I was not able to give her the guarantee that I think she was entitled to deserve."
The bill will return to the House of Commons in two weeks when the government will attempt to remove the amendment.