def _wrap_as_constant(value, tensor_spec):
  """Wrap a value as a constant, using tensor_spec for shape and type info."""
  if isinstance(tensor_spec, tf.SparseTensorSpec):
    result = tf.SparseTensor(
        indices=tf.constant(value.indices, dtype=tf.int64),
        values=tf.constant(value.values, dtype=tensor_spec.dtype),
        dense_shape=tf.constant(value.dense_shape, dtype=tf.int64))
  elif isinstance(tensor_spec, tf.RaggedTensorSpec):
    result = _ragged_value_as_constant(value, tensor_spec.dtype)
  else:
    result = tf.constant(value, dtype=tensor_spec.dtype)
    result.shape.assert_is_compatible_with(tensor_spec.shape)
  return result
Wrap a value as a constant, using tensor_spec for shape and type info.
tensorflow_transform/test_case.py