{"record":{"id":"af211b38e60d0be9","repo":"apache/beam","slug":"embeddings-can-only-be-generated-on-dict-str-str-got-dict","errorCode":null,"errorMessage":"Embeddings can only be generated on dict[str, str].Got dict[str, {type(batch[0])}] instead.","messagePattern":"Embeddings can only be generated on dict\\[str, str\\]\\.Got dict\\[str, (.+?)\\] instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/transforms/base.py","lineNumber":766,"sourceCode":"  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\n  PCollection[dict[str, E]] to a PCollection[dict[str, P]].\n\n  _TextEmbeddingHandler 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 _TextEmbeddingHandler is to generate embeddings for\n  text inputs using the EmbeddingsManager instance.\n\n  If the input is not a text column, a RuntimeError will be 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    if not isinstance(batch[0], (str, bytes)):\n      raise TypeError(\n          'Embeddings can only be generated on dict[str, str].'\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_TextEmbeddingHandler')\n\n\nclass _ImageEmbeddingHandler(_EmbeddingHandler):\n  \"\"\"\n  A ModelHandler intended to be work on list[dict[str, Image]] 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\n  PCollection[dict[str, E]] to a PCollection[dict[str, P]].","sourceCodeStart":748,"sourceCodeEnd":784,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/transforms/base.py#L748-L784","documentation":"The text-embedding EmbeddingsManager validates that each batched column value is str or bytes before running inference. If batch[0] is any other type (int, float, list, dict, None, etc.), it raises TypeError because the underlying text embedding models only accept string inputs.","triggerScenarios":"Applying a text embedding transform (e.g. SentenceTransformerEmbeddings via MLTransform) to a column whose values are ints, floats, lists, or None instead of strings.","commonSituations":"PCollection elements are dicts of mixed types and a non-string column was selected for embedding; numeric/NaN values in a CSV column passed through; forgetting to cast a column to str before embedding.","solutions":["Cast the column values to str before the embedding transform (beam.Map(lambda d: {**d, 'col': str(d['col'])})).","Point the embedding transform at a column that actually contains text.","Pre-filter or handle None/non-string rows before applying the embedding.","If you meant to embed images or structured data, use the appropriate EmbeddingsManager (image or dataclass variants)."],"exampleFix":"// before\nMLTransform().with_transform(SentenceTransformerEmbeddings(columns=['num_col']))\n// after\ndata = data | beam.Map(lambda d: {**d, 'num_col': str(d['num_col'])})\ndata = data | MLTransform().with_transform(SentenceTransformerEmbeddings(columns=['num_col']))","handlingStrategy":"validation","validationCode":"def validate_text_batch(batch):\n    assert isinstance(batch[0], (str, bytes)), f'Expected str, got {type(batch[0])}'","typeGuard":"def is_text_column(values) -> bool:\n    return all(isinstance(v, (str, bytes)) for v in values)","tryCatchPattern":"try:\n    data | embedding_transform\nexcept TypeError as e:\n    if 'dict[str, str]' in str(e):\n        data = data | beam.Map(cast_columns_to_str)\n    else:\n        raise","preventionTips":["Cast selected columns to str before embedding transforms.","Check dtypes of CSV/BigQuery columns feeding the pipeline.","Handle None/NaN values before embedding.","Use the right embeddings manager for the data modality."],"tags":["python","type-mismatch","embeddings"],"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"}