You could use <node3d:avatar>.getAnimationList and check the length of the list returned. If the ID is greater then that length the animation is not defined.
Code:
animations = avatar.getAnimationList()
if 123 <= len(animations):
avatar.state(123)
else:
print 'The avatar does not have this animation id'