Getting an error when trying to switch context mode

I have a script that was working, but which has mysteriously started failing. It’s even failing on the very first line where I’m simply trying to set the context mode. What could be causing this?

This is the start of my script:

import bpy

bpy.ops.object.mode_set(mode='OBJECT')

I’m getting the error

Python: Traceback (most recent call last):
  File "D:\biz\fiverr\2023\2023-07-08-bakeScript\bake_original.blend\Text.001", line 3, in <module>
  File "C:\Program Files\Blender Foundation\Blender 3.6\3.6\scripts\modules\bpy\ops.py", line 113, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Calling operator "bpy.ops.object.mode_set" error, can't modify blend data in this state (drawing/rendering)

This code runs fine if I try it in a new blender file. It’s just failing when I try running it in the scene that I need it to work in.

I don’t think I’m in “drawing/rendering” state, whatever that is. My viewport is showing me in object mode.

Can you please share .blend file so I can recreate the issue?

I don;t think I have it anymore. i think I overwrote it. Sorry.