Block with rounded edges in python
BMesh script.

&quot;Spam tin&quot; created with script below, in edit mode. Note has ngon top and bottom.
The bmesh equivalent of example linked in question using bpy.ops.mesh.bevel(...)

which is pretty much the bmesh.ops.bevel equivalent.
Select the four vertical edges How to select with python code only vertical edges from cube? and pass into operator.
Test script, run in OBJECT mode.
import bpy
import bmesh
from mathutils import Vector
context = bpy.context

corner_radius = 0.2 # 0.5 &gt; cylinder 
corner_segments = 5
width, depth, height = (1.5, 1, 2)

bpy.ops.mesh.primitive_cube_add()
ob = context.object
me = ob.data
bm = bmesh.new()
bm.from_mesh(me)

bm.edges.ensure_lookup_table()
# scale first to keep corners round
bmesh.ops.scale(
        bm,
        verts=bm.verts,
        vec=(width, depth, height),
        )  
bmesh.ops.bevel(
        bm,
        geom=[bm.edges[i] for i in (1, 3, 6, 9)],
        loop_slide=True,
        affect='EDGES',
        profile=0.5, # round
        offset=corner_radius * 2,
        segments=corner_segments,
        )
      
bm.to_mesh(me)