Coordinate Conversion causes Flipped Rotation

I am importing location/rotation data in the form of a list of floats. The raw data is in an X forward coordinate system, and I want to rotate it 90 degrees around the world origin to match Blenders coordinate system. This adjusted rotation however is causing certain objects to ‘flip’ around randomly.

raw_data = [1.24477, -2.01742, -0.001801461, 2.5548, -0.137604, 0.403129, -0.904739]
raw_location = raw_data[:3]
raw_rotation = raw_data[3:]
loc = Vector(raw_location)
rot = Quaternion((raw_rotation[4], raw_rotation[5], raw_rotation[6]), raw_rotation[3])
raw_matrix = Matrix.LocRotScale(loc, rot.normalized(), None)
m = Matrix.Rotation(radians(deg), 4, 'Z') @ raw_matrix

I’m still new to linear algebra, but I would expect something like this to happen at 180 degrees, however as you can see in the screenshot above this object goes to a maximum of 177 degrees.

On a side note, after I fix the rotations manually by adjusting the keys in the graph editor, if I get the objects world matrix and run the “to_euler()” function it will report the wrong values.

Frame 11 in editor:
X = 30.4
Y = -62.6
Z = 177

Frame 11 using matrix_world.to_euler() and then convert to degrees:
X = -149.62
Y = -117.42
Z = -2.78

I have a Blender file with test objects, however as a new user I cannot upload it yet. I am using Blender 3.3.3 LTS. I appreciate any help.