Single item to a tf.train.Feature.

def _item_to_tf_feature(item, key_name):
  """Single item to a tf.train.Feature."""
  v = item
  if isinstance(v, (list, tuple)) and not v:
    raise ValueError(
        "Feature {} received an empty list value, so is unable to infer the "
        "feature type to record. To support empty value, the corresponding "
        "FeatureConnector should return a numpy array with the correct dtype "
        "instead of a Python list.".format(key_name)
    )

  # Handle strings/bytes first
  if isinstance(v, (six.binary_type, six.string_types)):
    v = [tf.compat.as_bytes(v)]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
  elif (isinstance(v, (tuple, list)) and
        all(isinstance(x, (six.binary_type, six.string_types)) for x in v)):
    v = [tf.compat.as_bytes(x) for x in v]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
  elif (isinstance(v, np.ndarray) and
        (v.dtype.kind in ("U", "S") or v.dtype == object)):  # binary or unicode
    v = [tf.compat.as_bytes(x) for x in v.flatten()]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))

  # Use NumPy for numeric types
  v = np.array(v).flatten()  # Convert v into a 1-d array

  if np.issubdtype(v.dtype, np.integer):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=v))
  elif np.issubdtype(v.dtype, np.floating):
    return tf.train.Feature(float_list=tf.train.FloatList(value=v))
  else:
    raise ValueError(
        "Value received: {}.\n"
        "tf.train.Feature does not support type {} for feature key {}. "
        "This may indicate that one of the FeatureConnectors received an "
        "unsupported value as input.".format(repr(v), repr(type(v)), key_name)
    )