diff --git a/src/spatialdata/models/models.py b/src/spatialdata/models/models.py index c7234175..5d8cea0c 100644 --- a/src/spatialdata/models/models.py +++ b/src/spatialdata/models/models.py @@ -794,7 +794,10 @@ def _validate_table_annotation_metadata(self, data: AnnData) -> None: raise ValueError(f"`{attr[self.REGION_KEY_KEY]}` not found in `adata.obs`.") if attr[self.INSTANCE_KEY] not in data.obs: raise ValueError(f"`{attr[self.INSTANCE_KEY]}` not found in `adata.obs`.") - if (dtype := data.obs[attr[self.INSTANCE_KEY]].dtype) not in [np.int16, np.int32, np.int64, str]: + if (dtype := data.obs[attr[self.INSTANCE_KEY]].dtype) not in [np.int16, np.int32, np.int64, "O"] or ( + dtype == "O" and (val_dtype := type(data.obs[attr[self.INSTANCE_KEY]][0])) != str + ): + dtype = dtype if dtype != "O" else val_dtype raise TypeError( f"Only np.int16, np.int32, np.int64 or string allowed as dtype for " f"instance_key column in obs. Dtype found to be {dtype}"