{"record":{"id":"5242c6fcfbe2d06e","repo":"apache/beam","slug":"embeddings-can-only-be-generated-on-dict-str-image-got-dict","errorCode":null,"errorMessage":"Embeddings can only be generated on dict[str, Image].Got dict[str, {type(batch[0])}] instead.","messagePattern":"Embeddings can only be generated on dict\\[str, Image\\]\\.Got dict\\[str, (.+?)\\] instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/transforms/base.py","lineNumber":803,"sourceCode":"\n  _ImageEmbeddingHandler will accept an EmbeddingsManager instance, which\n  contains the details of the model to be loaded and the inference_fn to be\n  used. The purpose of _ImageEmbeddingHandler is to generate embeddings for\n  image inputs using the EmbeddingsManager instance.\n\n  If the input is not an Image representation column, a RuntimeError will be\n  raised.\n\n  This is an internal class and offers no backwards compatibility guarantees.\n\n  Args:\n    embeddings_manager: An EmbeddingsManager instance.\n  \"\"\"\n  def _validate_column_data(self, batch):\n    # Don't want to require framework-specific imports\n    # here, so just catch columns of primatives for now.\n    if isinstance(batch[0], (int, str, float, bool)):\n      raise TypeError(\n          'Embeddings can only be generated on dict[str, Image].'\n          f'Got dict[str, {type(batch[0])}] instead.')\n\n  def get_metrics_namespace(self) -> str:\n    return (\n        self._underlying.get_metrics_namespace() or\n        'BeamML_ImageEmbeddingHandler')\n\n\nclass _MultiModalEmbeddingHandler(_EmbeddingHandler):\n  \"\"\"\n  A ModelHandler intended to be work on\n  list[dict[str, TypedDict(Image, Video, str)]] inputs.\n\n  The inputs to the model handler are expected to be a list of dicts.\n\n  For example, if the original mode is used with RunInference to take a\n  PCollection[E] to a PCollection[P], this ModelHandler would take a","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/transforms/base.py#L785-L821","documentation":"The image-embedding EmbeddingsManager expects dict[str, Image]-style columns where values are framework image objects (e.g. PIL Images). To avoid framework-specific imports it rejects primitive Python values (int, str, float, bool), raising TypeError, since those can never be valid image inputs.","triggerScenarios":"Applying an image embedding transform to a column containing strings/numbers (e.g. file paths as str, labels, or raw pixel ints) instead of actual image objects.","commonSituations":"Passing image file paths (strings) instead of loaded image objects; using the image embeddings manager where the text one was intended; a preprocessing step upstream emitted raw values.","solutions":["Load images into the framework-specific image type (e.g. PIL.Image.open(path)) before the embedding transform.","Use the text embeddings manager (dict[str, str]) if your columns are strings.","Verify the correct column names are configured; you may be embedding a metadata column by mistake."],"exampleFix":"// before\nrows | beam.Map(lambda d: {'img': d['path']}) | image_embedding_transform\n// after\nrows | beam.Map(lambda d: {'img': PIL.Image.open(d['path'])}) | image_embedding_transform","handlingStrategy":"validation","validationCode":"def validate_image_batch(batch):\n    assert not isinstance(batch[0], (int, str, float, bool)), f'Not an image object: {type(batch[0])}'","typeGuard":"def is_image_column(values) -> bool:\n    return not any(isinstance(v, (int, str, float, bool)) for v in values)","tryCatchPattern":"try:\n    data | image_embedding\nexcept TypeError as e:\n    if 'dict[str, Image]' in str(e):\n        data = data | beam.Map(load_images)\n    else:\n        raise","preventionTips":["Load images with PIL/framework loaders before embedding; never pass paths as str.","Match the embeddings manager to the modality (text vs image vs dataclass).","Inspect sample elements of the PCollection before the transform.","Keep metadata (paths, labels) in separate columns."],"tags":["python","type-mismatch","embeddings","image"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}