The crashing is not specified in the API, so there is no “correct way” that it does not crash. The crash is likely an instability or bug in the macOS version of SketchUp.
In order to analyse what causes it (and what you can do to circumvent it), you need to decompose your code snippet into smaller steps (and comment/uncomment line by line) and retry.
I suspect that:
doing an operation within an observer callback (that could be triggered from within another operation) is highly risky. If this is the cause of the crash, you could “defer” it with a UI.start_timer(0, false).
undo within an operation (within an observer callback), like redraw_with_undo
Does it crash with anythin imported (simple, not nested component) or only nested ones?
This is explicitly starting another undo operation inside yours. Operations cannot be nested.
So you’d need to move this after your commit call.
As the name of the receiver implies, the object is an observer that specifically watches dynamic components, has a set of observers which call that method to redraw instances when needed. That method creates a undo operation which might not play nice with other operations.
So, you could try calling .redraw(entity) instead of .redraw_with_undo(entity),
(as the latter just calls the former anyway.)