def cast_tensor_by_spec(
    _input: tf_ext.TensorLike, spec: tf_ext.TypeSpec
) -> tf_ext.TensorLike:
    """
    transform dtype & shape following spec
    """
    if not LazyType["tf_ext.TensorSpec"](
        "tensorflow.python.framework.tensor_spec.TensorSpec"
    ).isinstance(spec):
        return _input

    if LazyType["tf_ext.CastableTensorType"]("tf.Tensor").isinstance(
        _input
    ) or LazyType["tf_ext.CastableTensorType"](
        "tensorflow.python.framework.ops.EagerTensor"
    ).isinstance(_input):
        # TensorFlow Issues #43038
        # pylint: disable=unexpected-keyword-arg, no-value-for-parameter
        return t.cast(
            "tf_ext.TensorLike",
            tf.cast(_input, dtype=spec.dtype, name=t.cast(str, spec.name)),
        )
    else:
        return t.cast(
            "tf_ext.TensorLike",
            tf.constant(_input, dtype=spec.dtype, name=t.cast(str, spec.name)),
        )
transform dtype & shape following spec
src/bentoml/_internal/frameworks/utils/tensorflow.py