Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'datacontenttype' #187

Open
edump72 opened this issue Mar 31, 2024 · 4 comments
Open

KeyError: 'datacontenttype' #187

edump72 opened this issue Mar 31, 2024 · 4 comments
Assignees
Labels
bug Something isn't working waiting for customer response Issue is on hold awaiting more information from OP

Comments

@edump72
Copy link

edump72 commented Mar 31, 2024

I am creating a Google Cloud Function using a Python script, in order to handle the creation of a new document in my Firestore database. When the data of the new document is fetched, I create an SVG with it using Spacy. I have used the documentation in order to handle this event : https://firebase.google.com/docs/functions/firestore-events?hl=es-419&gen=2nd#python-preview_1

This is my Python script :

from firebase_functions import firestore_fn, https_fn
from firebase_admin import initialize_app, firestore
from spacy import displacy
import google.cloud.firestore
#Spacy
import spacy


app = initialize_app()

 @firestore_fn.on_document_created(document="Solicitud desde Android Studio/Entidades")
 def sentenceToImageFunction(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:

   firestore_client: google.cloud.firestore.Client = firestore.client()
   nlp = spacy.load("es_dep_news_trf")
   # Get the value of "Oración" if it exists.
   if event.data is None:
       return
   try:
       sentence = event.data.get("Oración")
   except KeyError:
       # No "Oración" field, so do nothing.
       return

   nlp = spacy.load("es_dep_news_trf")
   doc = nlp(sentence)
   options = {"compact": True, "bg": "#FFFFFF","color": "black", "font": "Arial", 
   "collapse_punct": True, "arrow_stroke": 5, "arrow_spacing": 10, "distance": 200}
   svg = str(displacy.render(doc, style="ent", options=options))

   firestore_client.collection("Respuesta desde Python").document("Entidades analizadas").set({"Entidad analizada" : svg})`

I have deployed the function using Google Cloud Console, setting everything correctly. The function is accepted, but when I enter a trigger event:
CKm83
,my Firestore database continues as it was and I get this error :

   [7:04:17 PM] - [2024-03-31 17:04:17,815] ERROR in app: Exception on / [POST]
   Traceback (most recent call last):
   File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1463, 
   in wsgi_app
   response = self.full_dispatch_request()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 872, in 
   full_dispatch_request
   rv = self.handle_user_exception(e)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 870, in 
   full_dispatch_request
   rv = self.dispatch_request()
     ^^^^^^^^^^^^^^^^^^^^^^^
   File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 855, in 
   dispatch_request
   return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no- 
   any-return]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/layers/google.python.pip/pip/lib/python3.11/site- 
   packages/functions_framework/__init__.py", line 178, in view_func
   function(event)
   File "/layers/google.python.pip/pip/lib/python3.11/site- 
   packages/firebase_functions/firestore_fn.py", line 303, in on_document_created_wrapped
   return _firestore_endpoint_handler(
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/layers/google.python.pip/pip/lib/python3.11/site- 
   packages/firebase_functions/firestore_fn.py", line 95, in _firestore_endpoint_handler
   content_type: str = event_attributes["datacontenttype"]
                    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
   KeyError: 'datacontenttype'

Anyone knows what is the problem? Thank you in advance!

@jauntybrain jauntybrain added the bug Something isn't working label Apr 3, 2024
@jauntybrain jauntybrain self-assigned this Apr 4, 2024
@gregfenton
Copy link

Which version of the library are you using?

@exaby73
Copy link
Contributor

exaby73 commented Apr 18, 2024

Hey @edump72. Trying something similar, I cannot reproduce this issue. I get an error with the field name you're using:

ValueError: Path Oración not consumed, residue: ón

Here's the code example I used:

from firebase_functions import firestore_fn


@firestore_fn.on_document_created(document='Solicitud desde Android Studio/Entidades')
def on_post_created(event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
    print(event.data)
    if event.data is None:
        return

    try:
        event.data.get("Oración")
    except KeyError:
        return

Edit: This is the fill stack trace I get on emulators. Same is applicable for deployed functions as well:

Logs
>  <google.cloud.firestore_v1.base_document.DocumentSnapshot object at 0x1080efc50>
>  [2024-04-18 15:37:02,500] ERROR in app: Exception on /functions/projects/ [POST]
>  Traceback (most recent call last):
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
>      response = self.full_dispatch_request()
>                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
>      rv = self.handle_user_exception(e)
>           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
>      rv = self.dispatch_request()
>           ^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
>      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/functions_framework/__init__.py", line 178, in view_func
>      function(event)
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/firebase_functions/firestore_fn.py", line 446, in on_document_created_wrapped
>      return _firestore_endpoint_handler(
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/firebase_functions/firestore_fn.py", line 217, in _firestore_endpoint_handler
>      _typing.cast(_C1 | _C2, func)(database_event)
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/firebase_functions/core.py", line 125, in wrapper
>      return fn(*args, **kwargs)
>             ^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/main.py", line 13, in on_post_created
>      event.data.get("Oración")
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/base_document.py", line 487, in get
>      nested_data = field_path_module.get_nested_value(field_path, self._data)
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/field_path.py", line 229, in get_nested_value
>      field_names = parse_field_path(field_path)
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/field_path.py", line 121, in parse_field_path
>      for field_name in split_field_path(api_repr):
>                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/field_path.py", line 84, in split_field_path
>      for element in _tokenize_field_path(path):
>    File "/Users/nabeelparkar/dev/invertase/testing/python-functions/functions/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/field_path.py", line 64, in _tokenize_field_path
>      raise ValueError("Path {} not consumed, residue: {}".format(path, path[pos:]))
>  ValueError: Path Oración not consumed, residue: ón

@exaby73 exaby73 added the waiting for customer response Issue is on hold awaiting more information from OP label Apr 18, 2024
@gregfenton
Copy link

Oh....is this an issue of Python and "non-ASCII" characters in field names? Firestore is documented as supporting valid UTF-8 characters in field names, but maybe there's an issue with how Python is dealing with those strings?

[I haven't done a lot of Python coding in a long time, but I did suffer somewhat through the non-UTF8 wars of Python 2 vs. 3 ...]

@exaby73
Copy link
Contributor

exaby73 commented Apr 18, 2024

I believe that is a separate issue and not one with this SDK but rather upstream with the Google Cloud SDK we use internally. But what I don't get is how this error with datacontenttype occurred. My current speculation is that OP is using an older version of Google Cloud SDK which didn't have this issue but got another one by other means. This would mean that Google Cloud SDK has a regression. I'll raise this Unicode issue with the Firebase team, and keep this issue open for OP to respond if they can provide us with a reproducible sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting for customer response Issue is on hold awaiting more information from OP
Projects
None yet
Development

No branches or pull requests

4 participants