BlockDefinition import and export Error in PYTHON

I want to create new rhino file and copy all block definition from origin file.
But This code is work incompletely. The TextEntities in original block definition are not copied as expected.

I found that if the origin definition have 4 geometries(1 curve, 4 texts) but copied definition has only one curve geometry.

I think this happens in this method.
temp_doc.InstanceDefinitions.Add()

If I change this temp_doc to current_doc(document currently opened), it works well.

One thing more! similar things happen, when I import InstanceDefinitions, InstanceObject in InstanceDefinition is skipped.

Why this happen, and How can I fix this?

def export_block_definition_to_3dm(current_doc, filepath):
    """Block definition을 3dm 파일로 저장"""
    # CREATE TEMP DOC
    temp_doc = Rhino.RhinoDoc.CreateHeadless(None)

    try:
        block_defs = sinutils.get_all_block_definitions(current_doc)

        for block_def in block_defs:
            # Block definition을 임시 문서에 추가
            origin_geoms = sinutils.get_geom_list_from_block_defintion(
                current_doc, block_def
            )

            res = temp_doc.InstanceDefinitions.Add(
                f"{block_def.Name} COPY",
                block_def.Description,
                geo.Point3d(0, 0, 0),
                [obj.Geometry for obj in block_def.GetObjects()],
                [obj.Attributes for obj in block_def.GetObjects()],
            )

            created_def = temp_doc.InstanceDefinitions[res]
            created_geoms = sinutils.get_geom_list_from_block_defintion(
                temp_doc, created_def
            )
            if len(origin_geoms) != len(created_geoms):

                raise ValueError(
                    f"{block_def.Name} ERROR input {len(origin_geoms)} != output {len(created_geoms)}"
                )

        # SAVE 3dm 
        temp_doc.WriteFile(filepath, FILE_WRITE_OPTION)  # 8 = Rhino 8 버전
        print(f"Saved: {filepath}")

    finally:
        temp_doc.Dispose()

just a fast guess:
an open doc is based on a template, while a headless doc is nearly empty.
make sure all required layers exists.
And for a fast test, set the Layerindex of the Attributes to 0

if this does not work, check other differences between the headless and the template based file (units, tolerances, …)

As it is text that does not work - try an example that uses Arial with no special characters. a simple “hello world” Block

maybe those fast ideas help - good luck, kind regards - tom