[Python] Can't decode event data in Cloud Run Function with a Firestore trigger

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have deployed a 2nd Gen Cloud Run Function with a Firestore trigger, on the google.cloud.datastore.entity.v1.written event. I have used the sample code shown in Google Cloud documentation:

    from cloudevents.http import CloudEvent
    import functions_framework
    from google.events.cloud import firestore


    @functions_framework.cloud_event
    def hello_firestore(cloud_event: CloudEvent) -> None:
    """Triggers by a change to a Firestore document.

    Args:
    cloud_event: cloud event with information on the firestore event trigger
    """
    firestore_payload = firestore.DocumentEventData()
    firestore_payload._pb.ParseFromString(cloud_event.data)

    print(f"Function triggered by change to: {cloud_event['source']}")

    print("\nOld value:")
    print(firestore_payload.old_value)

    print("\nNew value:")
    print(firestore_payload.value)



    And here's the list of the dependencies residing in the requirements.txt file:

    functions-framework==3.3.0
    google-events==0.13.0
    google-api-core==2.6.0
    protobuf==5.28.3
    cloudevents==1.9.0


    However, every time a Firestore event comes in, the Cloud Run Function fails with the following error:

    Traceback (most recent call last):
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py", line 174, in view_func
    function(event)
    File "/layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py", line 70, in wrapper
    return func(*args, **kwargs)
    File "/workspace/main.py", line 13, in hello_firestore
    firestore_payload._pb.ParseFromString(cloud_event.data)
    google.protobuf.message.DecodeError: Error parsing message with type 'google.events.cloud.firestore.v1.DocumentEventData'


    Documentation also suggests including the proto dependencies in the source, which is something I've done but this doesn't seem to solve the issue, either. I'm sharing a screenshot of the structure, in case I have placed the proto dependencies, incorrectly:

    [​IMG]

    I'm running out of ideas so would appreciate any help in case anyone has experienced similar issues and managed to resolve them.

    Continue reading...

Compartilhe esta Página