I thought the problem might be that I created the action implicitly with keyframe_insert, but that doesn’t seem to be the case. There is no difference if I explicitly create the action before inserting frames.
import bpy
ob = bpy.context.object
if ob.animation_data is None:
ob.animation_data_create()
act = bpy.data.actions.new(name="MyAction")
ob.animation_data.action = act
ob.keyframe_insert("location")
print(ob.animation_data.action)
print(bpy.context.active_action)
and the output is
<bpy_struct, Action("MyAction") at 0x0000000039BE4588>
None
My original problem was that I wanted to mark the action as an asset. I thought I needed an operator to do that, but it can be done much simpler with action.asset_mark(). So the problem is solved as far as I’m concerned.