tensorflow/models · error · ValueError

Cannot convert type to feature

Error message

Cannot convert type {} to feature

What it means

Raised in convert_to_feature when the value's element type is not bytes, int, or float and no explicit value_type was given; the type cannot be inferred.

Solutions

  1. Convert values to bytes, int, or float before building features (e.g. str(x).encode('utf-8') for strings)
  2. Pass an explicit value_type like 'bytes_list' when the value is a list of strings
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at official/vision/data/tfrecord_lib.py:59 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/1ba6f2224a4f7590. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/data/tfrecord_lib.py:59

  Returns:
    feature: A tf.train.Feature object.
  """

  if value_type is None:

    element = value[0] if isinstance(value, list) else value

    if isinstance(element, bytes):
      value_type = 'bytes'

    elif isinstance(element, (int, np.integer)):
      value_type = 'int64'

    elif isinstance(element, (float, np.floating)):
      value_type = 'float'

    else:
      raise ValueError('Cannot convert type {} to feature'.
                       format(type(element)))

    if isinstance(value, list):
      value_type = value_type + '_list'

  if value_type == 'int64':
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

  elif value_type == 'int64_list':
    value = np.asarray(value).astype(np.int64).reshape(-1)
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

  elif value_type == 'float':
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

  elif value_type == 'float_list':
    value = np.asarray(value).astype(np.float32).reshape(-1)
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))

View on GitHub (pinned to e006f5f0d5)