def _sparse_feature_from_feature_spec(spec, name, domains):
  """Returns a representation of a SparseFeature from a feature spec."""
  if isinstance(spec.index_key, list):
    assert isinstance(spec.size, (list, tuple, tf.TensorShape)), type(spec.size)
    assert len(spec.index_key) == len(spec.size), (spec.index_key, spec.size)
    spec_size = [
        s.value if isinstance(s, tf.compat.v1.Dimension) else s
        for s in spec.size
    ]
    spec_size = [s if s != -1 else None for s in spec_size]
    int_domains = [
        schema_pb2.IntDomain(min=0, max=size - 1) if size is not None else None
        for size in spec_size
    ]
    index_feature = [
        schema_pb2.Feature(
            name=key, type=schema_pb2.INT, int_domain=int_domain)
        for (key, int_domain) in zip(spec.index_key, int_domains)
    ]
    index_feature_ref = [
        schema_pb2.SparseFeature.IndexFeature(name=key)
        for key in spec.index_key
    ]
  else:
    # Create a index feature.
    index_feature = [
        schema_pb2.Feature(
            name=spec.index_key,
            type=schema_pb2.INT,
            int_domain=schema_pb2.IntDomain(min=0, max=spec.size - 1))
    ]
    index_feature_ref = [
        schema_pb2.SparseFeature.IndexFeature(name=spec.index_key)
    ]

  # Create a value feature.
  value_feature = schema_pb2.Feature(name=spec.value_key)
  _set_type(name, value_feature, spec.dtype)
  _set_domain(name, value_feature, domains.get(name))

  # Create a sparse feature which refers to the index and value features.
  value_feature_ref = schema_pb2.SparseFeature.ValueFeature(name=spec.value_key)
  sparse_feature = schema_pb2.SparseFeature(
      name=name,
      is_sorted=True if spec.already_sorted else None,
      index_feature=index_feature_ref,
      value_feature=value_feature_ref)

  return (index_feature, value_feature, sparse_feature)
Returns a representation of a SparseFeature from a feature spec.
tensorflow_transform/tf_metadata/schema_utils.py