CHICAGO — The Chicago teachers’ union and school system reached a tentative contract agreement late Monday night, narrowly averting a strike that threatened to shut down the nation’s third-largest school system.

Teachers — who have been working without a contract since June 2015 — had planned to walk off the job Tuesday, a move that would have forced the school system to cancel classes for more than 300,000 students.

But the two sides reached a compromise just before midnight, capping nearly two years of negotiations about issues ranging from pay and benefits to class sizes, special education, teacher evaluations, layoffs and charter schools.

Chicago Teachers Union President Karen Lewis called the detente “a relief for the entire city.”

“What we ended up with is something that’s good for kids, it’s good for clinicians, it’s good for paraprofessionals, for teachers, for the community,” Lewis said at a late-night news conference streamed on Facebook.

The agreement now must be ratified by the city’s Board of Education and the union’s full membership.

It calls for a four-year contract good through June 2019, including salary increases based on teachers’ experience and education — which had been frozen during contract negotiations — and cost-of-living raises in the final two years.

The agreement also includes a cap on privately run charter schools and would require the school system to provide a teaching assistant in early education classrooms — kindergarten through second grade — with 32 students or more. And in a victory for the union, it requires the school system to continue its contribution to current teachers’ pensions, though new hires will lose that benefit.

The “pension pickup” — in which teachers pay 2 percent of their salary toward their own pension, and Chicago Public Schools pays 7 percent — had been a major sticking point in negotiations. The cash-strapped school system, which has at times appeared to approach bankruptcy, had argued that it could not afford to continue the pension pickup.

The total cost of the tentative agreement, and the school system’s plan for footing the bill, was not immediately clear.

The union had been building momentum toward a strike for months, and a walkout still seemed likely Monday, as teachers picked up signs to carry on picket lines Tuesday morning. It would have been the third strike in four years, and it came at a politically difficult time for Mayor Rahm Emanuel (D), who already is under pressure for his administration’s handling of city violence and police shootings.

Teachers walked off the job for one day in April, a move that union leaders said was meant to draw attention to the dire financial outlook of the city’s public schools and colleges.

“Teachers’ hard work will be respected in this contract and appropriately rewarded,” Emanuel said in a statement early Tuesday morning. “Chicago Public Schools’ finances will be stronger and on firmer ground. Parents and taxpayers will be relieved, and more importantly, reassured that we all came together to work with common purpose. And students across Chicago will be in school this morning … and on the path to a brighter future.”

Later Tuesday, the mayor began his 2017 budget address by recognizing the “good work at the negotiating table,” winning applause from the city council and many in the audience when he praised the “good faith” displayed by both sides.

Emanuel’s relationship with the 27,000-member Chicago Teachers Union has been strained for years. In 2012, during his first term, city teachers struck for seven days, turning Chicago into a focal point for the national debate about education reforms such as charter schools and evaluating teachers based in part on their student’s standardized test scores.

The current round of negotiations first appeared headed for conclusion in January, when the two sides reached a tentative agreement that Lewis, the union president, publicly praised as a good deal. But when she took it to her 40-person leadership team, they rejected it unanimously. That leadership team has given its blessing to the new tentative agreement.

As Chicago parents dropped off their children at school Tuesday morning, many expressed both support for the teachers and relief that a strike was averted.

In Pilsen, a largely immigrant Mexican neighborhood on the city’s southwest side, parents and children filled the sidewalks as they made their way to school or waited at bus stops. Eloisa Rogal, the mother of a kindergartner at Jose Clemente Orozco Community Academy, said she was happy when she woke up at 1 a.m. and found the news of the contract agreement online.

“If there was a strike, they were going to miss a lot of learning. The ones who pay the price are the kids,” Rogal said. “But I know the teachers need resources, some of them have 40 kids in a classroom and one teacher. The agreement is good for everyone, for the parents, the students, the teachers.”

Outside Richard T. Crane Medical Preparatory High School on the West Side, many African American parents also said they were glad a strike was avoided and voiced emphatic support for teachers along with disapproval of the city’s mayor. Students also said they were enthusiastic to be in school, mostly because they do not want to make up missed days during the summer.

Michael Walker, 47, who runs a shuttle and limo service, said he works full time and would have had to find something for his son to do during the day had the strike gone on as planned. He said he’s furious that officials don’t spend more money on schools and teachers.

“There’s so much violence because education is so screwed up,” Walker said. “Good schools should be a given, like drinking water and milk.”

Brown reported from Washington. This story has been updated.