tf.py_function is not compatible with TPUs. So, if you have distributed TensorFlow training pipelines that use TPUs you cannot use tf.py_function. In that case, consider switching to a multi-GPU environment, or rewriting the contents of the function in pure TensorFlow.