Firestore Python DocumentSnapshot.get ValueError: Field Name/Path Constraint

Jun 26, 2019
ValueError: {} not consumed, residue: {}

Calling DocumentSnapshot.get with the following field name/path will raise ValueError.

  • contain dash: d0b97305-85a8-4e02-943c-9021454d618f-0
  • contain =: ZDBiOTczMDUtODVhOC00ZTAyLTk0M2MtOTAyMTQ1NGQ2MThmLTA=
  • starts with number: 0b9730585a84e02943c9021454d618f0

As per this source

Field name constraint

  • Must be valid UTF-8 characters
  • 1,500 bytes

Field path constraint (google/cloud/firestore_v1/field_path.py)

  • Must separate field names with a single period (.)
  • Must enclose each field name in backticks unless the field name meets the following requirements:
  • The field name contains only the characters a-z, A-Z, 0-9, and underscore (_)
  • The field name does not start with 0-9
from firebase_admin import firestorefirestore_client = firestore.client()ref = firestore_client.collection('test').document('DOC_ID')doc = ref.get()try:    # use backtick    field_name = '`69f63bdefc5345b5881bd8c02b2018a50`'    value = doc.get(field_name)except KeyError as e: # if field_name not found    print(e)

NOTE: To be safe, always put backtick around field path (`FIELD_PATH`); ValueError is thrown if FIELD_PATH value is invalid.

Else, you can convert the doc to dict to access by the field name without such limitation.

data = doc.to_dict()value = data[field_name]

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.