I think you have to make sure that this property doesn’t get smaller than 1.
hframes
: Sprite2D — Godot Engine (stable) documentation in English
set_hframes: Amount of hframes cannot be smaller than 1.
maybe similar issue: Encountered an issue "set_frame: Index p_frame = 17 is out of bounds" with higher version when trying to upgrade Godot from 4.1.1
Is it always reproducible?
If you can share your project then I could try to debug it later when I find time.