It took weeks of negotiations to settle on an end to balance billing, a much-loathed feature of Virginia’s medical system that’s been locked in a legislative deadlock for years.

Lawmakers were jubilant on Thursday as both the House and Senate unanimously passed identical legislation to remove the risk of surprise hospital bills for some Virginians. The often-expensive fees often come when patients seek emergency care at an out-of-network hospital, or receive treatment from out-of-network doctors at a facility that’s otherwise covered by their insurance.

In many cases, patients don’t know they’re receiving treatment from an out-of-network provider. In one case, a Northern Virginia teenager was billed $34,000 after an out-of-network plastic surgeon treated his broken nose at a hospital included on his family’s insurance plan.

Doctors and hospitals have been pitted against insurance companies on the issue for years. Neither side thinks patients should receive surprise medical bills, but they’ve been unable to reach a resolution on how — and how much — doctors should be paid for treating an out-of-network patient.

Frustrated, a team of legislators pushed a bill that leaves the decision to a third party if providers and insurers can’t reach an agreement.

“When an individual needs medical care, that person needs to feel confident they can access that care without going bankrupt,” said Sen. Barbara Favola, D-Arlington, who carried the legislation in the Senate. “This has been a long time coming.”

The final version of the legislation borrows a model that was first adopted in Washington state. Under the new law, patients who receive out-of-network emergency care — or treatment from an out-of-network doctor at a hospital covered by their plan — can only be charged the in-network rate required by their plan, including their usual copay or deductible.

Importantly, the law only applies to patients with state-regulated health care plans. ERISA plans (coverage provided and paid for by a single employer) have to opt into the law, potentially leaving up to 3 million Virginians still vulnerable to balance billing, according to data from the state’s Bureau of Insurance.

Insurers are responsible for compensating an out-of-network provider at a “commercially reasonable” rate, based on payments for similar services in the same geographic area. Insurance companies have 30 days to pay the bill, and doctors have 30 days to dispute the initial offer or the payment.

If providers and insurers disagree on the amount, the bill gives them 30 days to negotiate an agreement. If that fails, either party can notify the State Corporation Commission, which is responsible for providing a list of approved arbitrators to settle the dispute.

“Patients will never be part of the arbitration process,” said Del. Mark Sickles, D-Alexandria, who helped negotiate the final version of the legislation. “The goal of arbitration is to serve as a powerful backstop and provide an incentive for the health plans and providers to be honest brokers from the start.”

Lawmakers hope the month-long negotiation process will discourage providers and insurers from seeking further action. The final legislation also mandates a “baseball-style” arbitration process, with both parties submitting their final offers without going through multiple series of offers and counter-offers.

Both Favola and Sickles described the final bill as a “compromise” between doctors, hospitals and insurers on behalf of Virginia patients. But Doug Gray, executive director of the Virginia Association of Health Plans, disagreed with any characterization of an agreement between the opposing groups.

“We were forced to accept this legislation,” he said. “I don’t mean to sound like a sore loser. But what we did was try to make this best version it could be. That doesn’t mean we like it.”

Virginia’s insurance companies have long preferred legislation that would set fixed rates for out-of-network providers — usually capped at the rate of Medicaid reimbursement or slightly higher — while doctors and hospitals have argued that the proposed rates are unfairly low. Negotiators on the bill said that neither said was particularly happy with legislation that opened the process to third-party arbitration.

What matters to lawmakers, though, is that patients will no longer be caught in the middle.

“This is a momentous moment for Virginians,” Favola said.