{"record":{"id":"a8937f46ce70873a","repo":"apache/beam","slug":"apache-beam-io-gcp-datastore-v1new-datastoreio-entity","errorCode":null,"errorMessage":"apache_beam.io.gcp.datastore.v1new.datastoreio.Entity expected, got: %s","messagePattern":"apache_beam\\.io\\.gcp\\.datastore\\.v1new\\.datastoreio\\.Entity expected, got: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/gcp/datastore/v1new/datastoreio.py","lineNumber":538,"sourceCode":"      self,\n      project,\n      throttle_rampup=True,\n      hint_num_workers=_Mutate._DEFAULT_HINT_NUM_WORKERS):\n    \"\"\"Initialize the `WriteToDatastore` transform.\n\n    Args:\n      project: (:class:`str`) The ID of the project to write entities to.\n      throttle_rampup: Whether to enforce a gradual ramp-up.\n      hint_num_workers: A hint for the expected number of workers, used to\n                        estimate appropriate limits during ramp-up throttling.\n    \"\"\"\n    mutate_fn = WriteToDatastore._DatastoreWriteFn(project)\n    super().__init__(mutate_fn, throttle_rampup, hint_num_workers)\n\n  class _DatastoreWriteFn(_Mutate.DatastoreMutateFn):\n    def element_to_client_batch_item(self, element):\n      if not isinstance(element, types.Entity):\n        raise ValueError(\n            'apache_beam.io.gcp.datastore.v1new.datastoreio.Entity'\n            ' expected, got: %s' % type(element))\n      if not element.key.project:\n        element.key.project = self._project\n      client_entity = element.to_client_entity()\n      if client_entity.key.is_partial:\n        raise ValueError(\n            'Entities to be written to Cloud Datastore must '\n            'have complete keys:\\n%s' % client_entity)\n      return client_entity\n\n    def add_to_batch(self, client_entity):\n      self._batch.put(client_entity)\n\n    def display_data(self):\n      return {\n          'mutation': 'Write (upsert)',\n          'project': self._project,","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/gcp/datastore/v1new/datastoreio.py#L520-L556","documentation":"WriteToDatastore's _DatastoreWriteFn only accepts apache_beam.io.gcp.datastore.v1new.types.Entity elements; any other element type raises ValueError naming the actual type received. This is an eager type check before converting the element to a client entity.","triggerScenarios":"Piping non-Entity elements (dicts, google.cloud.datastore Entity objects, strings) into WriteToDatastore; using the older v1 types.Entity with the v1new sink (or vice versa); PCollection produced by a source that yields dicts.","commonSituations":"Migrating from apache_beam.io.gcp.datastore.v1 to v1new without converting element types; building Entities manually as dicts; mixing outputs of ReadFromDatastore (which yields v1new Entities) with hand-constructed elements of the wrong library.","solutions":["Wrap/convert each element to apache_beam.io.gcp.datastore.v1new.types.Entity before writing (Entity(key=Key(...), properties=...)).","If elements are google.cloud.datastore entities, convert their key and properties into v1new types.Entity.","Check the upstream PCollection's element type; add an explicit Map step that constructs the correct Entity.","Make sure you did not import types from datastore.v1 instead of datastore.v1new."],"exampleFix":"// before\n| 'write' >> WriteToDatastore(project)   # elements are dicts\n// after\n| 'to_entity' >> beam.Map(lambda d: types.Entity(\n      key=types.Key(d['kind'], d['id'], project=project), properties=d['props'])) \\\n| 'write' >> WriteToDatastore(project)","handlingStrategy":"type-guard","validationCode":"from apache_beam.io.gcp.datastore.v1new import types\nassert all(isinstance(e, types.Entity) for e in elements), 'non-Entity element in PCollection'","typeGuard":"def is_writable_entity(e):\n    from apache_beam.io.gcp.datastore.v1new import types\n    return isinstance(e, types.Entity) and e.key is not None","tryCatchPattern":"try:\n    _ = write_result = (pcoll | WriteToDatastore(project))\nexcept ValueError as e:\n    if 'Entity expected' in str(e):\n        pcoll = pcoll | beam.Map(to_v1new_entity)\n        write_result = pcoll | WriteToDatastore(project)","preventionTips":["Import Entity/Key from datastore.v1new.types, not the legacy v1 package","Add a beam.Map conversion step between your source and WriteToDatastore","Check PCollection element types with type hints (beam.PCollection[types.Entity])","Never feed raw dicts or google.cloud.datastore entities to v1new sinks"],"tags":["python","type-mismatch","beam-io","datastore"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}