The District’s public school system and the Washington Teachers’ Union reached a deal late Thursday on how to reopen schools, capping months of contentious negotiations in a city that has so far been unable to bring teachers and students back together in classrooms.

The deal settles a major dispute that has held up previous agreements: Teachers who do not have medical exemptions may be required to go back to classrooms, even if they do not want to.

D.C. Public Schools Chancellor Lewis D. Ferebee and Washington Teachers’ Union President Elizabeth Davis signed the agreement, according to announcements from the city and union.

“The road ahead is long, and there is much learning loss to be made up for, but this agreement brings us another step closer to getting our students where they need to be — in the classroom, surrounded by educators and friends, feeling challenged and loved,” Mayor Muriel E. Bowser (D) said in a statement late Thursday.

After he signed the deal, Ferebee said Friday that he is “optimistic” and plans to reopen school buildings for in-person learning in February if health conditions allow for it. The school system abandoned plans to bring thousands of students back last month because it didn’t have sufficient staffing.

The school system did not need a deal to reopen schools for in-person learning, but a signed agreement will pave a more viable path to reopening and make it more likely that teachers will willingly return to classrooms.

Ferebee said the school system has already started sending some teachers their working assignments for February.

Thursday’s announcement of a deal came after the city and union had reached multiple tentative agreements, only for the union to back out at the last minute.

“D.C. teachers miss our students and recognize that many have struggled to adapt to distance learning,” Davis said in announcing the deal.

“We must take every precaution and build trust that we’ve done all that is possible to safeguard our communities.”

Under the agreement, returning to classrooms could be required for teachers if student demand exceeds the number of teachers who volunteer to go back.

This marks a concession from the union, which had wanted in-person teaching to be optional for all its members — a demand the chancellor has said he cannot meet. The union backed out of an agreement last month because it required teachers who did not meet government health exemptions to return to classrooms.

Ferebee said the deal signed Thursday is not significantly different from the one that the two groups nearly reached in November.

Under the agreement, the school system would be required to share with the union data showing how many families want to return to classrooms. This is something the union has wanted but has not been put in previous versions of the agreement.

The chancellor has pointed to fall reading assessments administered to kindergartners and first- and second-graders — which show students falling behind and a widening of the achievement gap between White students and students of color — to reinforce his point that there’s an urgent need to reopen schools.

In November, the District began opening classrooms for students to complete their virtual learning under the supervision of nonteaching staff. More than 900 students accepted slots in these classrooms, according to the school system. But attendance hovers at just 50 to 60 percent each day, so it is unclear how many students regularly attend.

The accord also outlines the safety protocols and protective items that should be in each school building before students and teachers enter. Many of these points have long been agreed upon, and the city says nearly all elementary schools are equipped with the safeguards listed in the agreement.

Ferebee and the union have long agreed that union representatives, parents and community members must walk through buildings to ensure they meet all agreed-upon safety measures before they are allowed to reopen. Davis said this agreement contains more transparency around that provision, including requiring that people on these school visits sign off when the school system completes any necessary fixes to buildings detected during the walk-throughs.