Quickly, I know how to do it on frames by applying it on the first and last. Is it necessary to be key indices? The only problem left is that you need to drop the query flag in order to apply a new value. Using a different state flag like edit, or query changes the behavior of the command and at the same time, the inputs it is expecting. This should work:
Hope it helped.
from maya import cmds
def adjust_tangents( inNode ):
# Get the curves.
anim_curves = cmds.listConnections( inNode, type='animCurve' )
# Apply the new tangent on the first and last keys.
for anim in anim_curves:
keys = cmds.keyframe( anim, query=True )
cmds.keyTangent( anim, time=( keys, ), inTangentType='spline', outTangentType='spline' )
cmds.keyTangent( anim, time=( keys[-1], ), inTangentType='spline', outTangentType='spline' )
adjust_tangents( <your_animated_node_here> )