{"record":{"id":"1ba6f2224a4f7590","repo":"tensorflow/models","slug":"cannot-convert-type-to-feature","errorCode":null,"errorMessage":"Cannot convert type {} to feature","messagePattern":"Cannot convert type (.+?) to feature","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/vision/data/tfrecord_lib.py","lineNumber":59,"sourceCode":"  Returns:\n    feature: A tf.train.Feature object.\n  \"\"\"\n\n  if value_type is None:\n\n    element = value[0] if isinstance(value, list) else value\n\n    if isinstance(element, bytes):\n      value_type = 'bytes'\n\n    elif isinstance(element, (int, np.integer)):\n      value_type = 'int64'\n\n    elif isinstance(element, (float, np.floating)):\n      value_type = 'float'\n\n    else:\n      raise ValueError('Cannot convert type {} to feature'.\n                       format(type(element)))\n\n    if isinstance(value, list):\n      value_type = value_type + '_list'\n\n  if value_type == 'int64':\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n\n  elif value_type == 'int64_list':\n    value = np.asarray(value).astype(np.int64).reshape(-1)\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))\n\n  elif value_type == 'float':\n    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\n  elif value_type == 'float_list':\n    value = np.asarray(value).astype(np.float32).reshape(-1)\n    return tf.train.Feature(float_list=tf.train.FloatList(value=value))","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/vision/data/tfrecord_lib.py#L41-L77","documentation":"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.","triggerScenarios":"Thrown at official/vision/data/tfrecord_lib.py:59 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Convert values to bytes, int, or float before building features (e.g. str(x).encode('utf-8') for strings)","Pass an explicit value_type like 'bytes_list' when the value is a list of strings"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}