Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

ValueError: Protocol message Document has no "type" field. #43

Closed
Arthur-Cassarin-Grand opened this issue Oct 22, 2020 · 6 comments
Closed
Assignees
Labels
api: language Issues related to the googleapis/python-language API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@Arthur-Cassarin-Grand
Copy link

Arthur-Cassarin-Grand commented Oct 22, 2020

Hello,

I've got an issue since I've passed my project to google-cloud-language 2.0.0.
I tried on the Cloud Shell Protocol offered by the official documentation. This bug appears everytime.

Environment details

  • OS type and version: MacOS - Catalina
  • Python version: Python 3.7.0
  • pip version: pip 20.2.4
  • google-cloud-language version: 2.0.0

pip freeze

asgiref==3.2.10
cachetools==4.0.0
certifi==2019.11.28
chardet==3.0.4
colorama==0.4.4
cygrpc==1.0.4.post6
google-api-core==1.23.0
google-api-python-client==1.12.4
google-auth==1.22.1
google-auth-httplib2==0.0.4
google-cloud==0.34.0
google-cloud-language==2.0.0
googleapis-common-protos==1.6.0
grpcio==1.31.0
grpcio-tools==1.23.0
idna==2.8
libcst==0.3.13
oauth2client==4.1.3
packaging==20.4
pkginfo==1.6.0
proto-plus==1.11.0
protobuf==3.13.0

Steps to reproduce

  1. I use the sample code of the library here : samples/v1/language_syntax_text.py
  2. python3 samples/v1/language_syntax_text.py

Code example

def gcl_analyze_syntax(sentence):
    client = language_v1.LanguageServiceClient()
    type_ = language_v1.Document.Type.PLAIN_TEXT
    language = "fr"
    document = {"content": sentence, "type": type_, "language": language}
    encoding_type = language_v1.EncodingType.UTF8
    tokens = client.analyze_syntax(request = {'document': document, 'encoding_type': encoding_type}).tokens
    return tokens

or

def sample_analyze_syntax(text_content):
    """
    Analyzing Syntax in a String
    Args:
      text_content The text content to analyze
    """

    client = language_v1.LanguageServiceClient()

    # text_content = 'This is a short sentence.'

    # Available types: PLAIN_TEXT, HTML
    type_ = language_v1.Document.Type.PLAIN_TEXT

    # Optional. If not specified, the language is automatically detected.
    # For list of supported languages:
    # https://cloud.google.com/natural-language/docs/languages
    language = "en"
    document = {"content": text_content, "type": type_, "language": language}

    # Available values: NONE, UTF8, UTF16, UTF32
    encoding_type = language_v1.EncodingType.UTF8

    response = client.analyze_syntax(request = {'document': document, 'encoding_type': encoding_type})

Stack trace

Traceback (most recent call last):
  File "samples/v1/language_syntax_text.py", line 110, in <module>
    main()
  File "samples/v1/language_syntax_text.py", line 106, in main
    sample_analyze_syntax(args.text_content)
  File "samples/v1/language_syntax_text.py", line 54, in sample_analyze_syntax
    response = client.analyze_syntax(request = {'document': document, 'encoding_type': encoding_type})
  File "/usr/local/lib/python3.7/dist-packages/google/cloud/language_v1/services/language_service/client.py", line 533, in analyze_syntax
    request = language_service.AnalyzeSyntaxRequest(request)
  File "/usr/local/lib/python3.7/dist-packages/proto/message.py", line 461, in __init__
    pb_value = marshal.to_proto(pb_type, value)
  File "/usr/local/lib/python3.7/dist-packages/proto/marshal/marshal.py", line 205, in to_proto
    pb_value = rule.to_proto(value)
  File "/usr/local/lib/python3.7/dist-packages/proto/marshal/rules/message.py", line 32, in to_proto
    return self._descriptor(**value)
ValueError: Protocol message Document has no "type" field.

Thanks,
Arthur

@product-auto-label product-auto-label bot added the api: language Issues related to the googleapis/python-language API. label Oct 22, 2020
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Oct 23, 2020
@santma
Copy link

santma commented Oct 26, 2020

I am having the same problem. Let me know if you salved this somehow. Thanks!

@paukiun
Copy link

paukiun commented Oct 27, 2020

i have the same question. And when i downgrade my google.cloud.language to 1.3.0, python still went wrong saying "do not have the attribute document"

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Oct 27, 2020
@busunkim96
Copy link
Contributor

Hi,

The attribute is renamed to type_ to avoid conflicts with builtins. Unfortunately it looks like the handwritten bits of the documentation are incorrectly showing Document.type instead of Document.type_.

https://googleapis.dev/python/language/latest/language_v1/types.html#google.cloud.language_v1.types.Document

@Arthur-Cassarin-Grand
Copy link
Author

Arthur-Cassarin-Grand commented Oct 27, 2020

Okay thank you @busunkim96.

For those who didn't have understand, here's the code fixed.

def gcl_analyze_syntax(sentence):
    client = language_v1.LanguageServiceClient()
    type_ = language_v1.Document.Type.PLAIN_TEXT
    language = "fr"
    document = {"content": sentence, "type_": type_, "language": language}
    encoding_type = language_v1.EncodingType.UTF8
    tokens = client.analyze_syntax(request = {'document': document, 'encoding_type': encoding_type}).tokens
    return tokens

@munkhuushmgl munkhuushmgl self-assigned this Oct 27, 2020
@busunkim96 busunkim96 reopened this Oct 27, 2020
@busunkim96
Copy link
Contributor

The samples and RST files need to be updated with this change.

@busunkim96 busunkim96 added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed 🚨 This issue needs some love. api: language Issues related to the googleapis/python-language API. triage me I really want to be triaged. labels Oct 27, 2020
@yoshi-automation yoshi-automation added 🚨 This issue needs some love. triage me I really want to be triaged. labels Oct 27, 2020
@busunkim96 busunkim96 added priority: p2 Moderately-important priority. Fix may not be included in next release. api: language Issues related to the googleapis/python-language API. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Oct 28, 2020
@telpirion
Copy link
Contributor

Fixed in #49 .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: language Issues related to the googleapis/python-language API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

7 participants