The cities of San Francisco, New York and Philadelphia filed a sweeping federal lawsuit Tuesday accusing the U.S. Department of Defense of failing to live up to its legal duty to notify the FBI when a member of the military is convicted of a crime that would bar them from buying or possessing firearms.

The lawsuit, filed in federal court in Virginia, accuses the defense department, the Army, Navy, Air Force and a host of high-ranking Pentagon officials — including Secretary of Defense James Mattis — of unevenly feeding reports about convictions and dishonorable discharges to the FBI’s criminal background check system for the past two decades.

The cities are working with the San Francisco-based Giffords Law Center to Prevent Gun Violence and the New York law firm Pillsbury Winthrop Shaw Pittman, which is representing them.

In their joint complaint, the cities contend that these kinds of reporting lapses allowed Devin Kelley — a former member of the Air Force who was court-martialed and convicted of assaulting his wife and stepson in 2012 — to purchase the assault-style weapon that he’s accused of using to kill 26 people at a church in Texas last month. Kelley, who was discharged from the Air Force in 2014, killed himself shortly after the mass shooting.

According to federal law, Kelley’s assault conviction should have prevented him from purchasing or possessing a firearm. But the Air Force admitted that it failed to report the conviction to the FBI.

The cities said their suit “is intended to prevent such senseless carnage from ever again being inflicted by current or former members of the military who should be blocked from acquiring guns or licenses to carry guns.”

Since the Texas shooting, numerous federal officials have acknowledged that the defense department has failed to fully inform the FBI of all military convictions, as required.

In a hearing before the Senate Judiciary Committee this month, Glenn Fine, acting inspector general for the defense department, testified that the military failed to tell the FBI about 31 percent of service members’ criminal convictions between Jan. 1, 2015, and Dec. 31, 2016.

Fine said that the defense department first reviewed its criminal history data-sharing practices with the FBI in 1997. That review, Fine said, found “significant deficiencies in the military services’ compliance with the requirement to submit criminal history data to the FBI.”

Last month, Attorney General Jeff Sessions called the situation “alarming” and “unacceptable.” He ordered the FBI and the Bureau of Alcohol, Tobacco, Firearms and Explosives to review the National Instant Criminal Background Check System, which is used by law enforcement and gun dealers to determine whether someone is legally allowed to purchase or possess a firearm.

Sessions specifically asked the FBI and ATF to identify any “federal government entities that are not fully and accurately reporting information” to the background check system, and to develop a plan to make sure reporting is done correctly in the future.

Lauren Ehrsam, a spokeswoman for the Department of Justice, declined to comment on the lawsuit.

The cities want the courts to appoint “an independent and apolitical branch of government” to ensure that the defense department complies with its obligation to share information about military criminal convictions with law enforcement in order to strengthen background checks.

“The national criminal background check system is the backbone of common-sense gun regulations,” San Francisco City Attorney Dennis Herrera said in a statement announcing the city’s participation in the lawsuit.

“The Defense Department’s failure to fulfill its legal duty and accurately report criminal convictions puts innocent Americans at risk,” he said. “It is past time to ensure that we’re doing everything we can to keep guns out of the wrong hands.”

Dominic Fracassa is a San Francisco Chronicle staff writer. Email: dfracassa@sfchronicle.com Twitter: @dominicfracassa