Another item of note is that you can get a P0171 with a rich or lean condition.
As mentioned previously, the O2 sensor is sensing Oxygen, not fuel.
So if the engine is so rich that combustion can not take place, (misfire) then lots of Oxygen can be present in the exhaust.
The converse is also true. The engine can be so lean that combustion can not take place (misfire) and lots of Oxygen can be present in the exhaust.
As mentioned previously, the O2 sensor is sensing Oxygen, not fuel.
So if the engine is so rich that combustion can not take place, (misfire) then lots of Oxygen can be present in the exhaust.
The converse is also true. The engine can be so lean that combustion can not take place (misfire) and lots of Oxygen can be present in the exhaust.