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

June 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 firestore

firestore_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]
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.