Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

ValueError: ('Unknown protobuf attr type', <class 'google.cloud.ndb.key.Key'>) when storing Key as value #134

@JustasCe

Description

@JustasCe

I am trying to store an entity Key to datastore in order to be able to easily reference and query it later on. However I ran into an issues when trying to store it.

The most simplified example for this issue is :

from google.cloud.ndb import (
    Client,
    Key,
    KeyProperty,
    Model,
    StringProperty,
)


class UserGroup(Model):
    user_group_name = StringProperty()


class User(Model):
    first_name = StringProperty()
    user_group = KeyProperty(indexed=True, kind=UserGroup)


ndb_client = Client(project="project-name")

with ndb_client.context():
  User(
        first_name="Justas",
        user_group=Key('UserGroup', 5139943911325696),
  ).put()

The error I get:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/_options.py", line 93, in wrapper
    return wrapped(*pass_args, _options=_options, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4691, in _put
    return self._put_async(_options=_options).result()
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 190, in result
    self.check_success()
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 137, in check_success
    raise self._exception
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 291, in _advance_tasklet
    yielded = self.generator.send(send_value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4748, in put
    entity_pb = _entity_to_protobuf(self)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 602, in _entity_to_protobuf
    return helpers.entity_to_protobuf(ds_entity)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 221, in entity_to_protobuf
    _set_protobuf_value(value_pb, value)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 451, in _set_protobuf_value
    attr, val = _pb_attr_value(val)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 366, in _pb_attr_value
    raise ValueError("Unknown protobuf attr type", type(val))
ValueError: ('Unknown protobuf attr type', <class 'google.cloud.ndb.key.Key'>)

It looks to me like the Key is being built properly, however when executing put() , the library google.cloud.datastore receives the value as <class 'google.cloud.ndb.key.Key'> which it doesn't recognise.

Am I doing something wrong here?

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions