Skip to content

Client library should not fail when encountering unexpected response bodies #2930

@gguuss

Description

@gguuss
  1. OS type and version
    OSX 10.11

  2. Python version and virtual environment information python --version
    Python 2.7.12

  3. google-cloud-python version pip show google-cloud, pip show google-<service> or pip freeze
    google-cloud-vision==0.22.0

  4. Stacktrace if available


cls = <class 'google.cloud.vision.annotations.Annotations'>
response = {'fullTextAnnotation': {'pages': [{'blocks': [{'blockType': 'TEXT', 'boundingBox': {...}, 'paragraphs': [...], 'proper... [{'x': 317, 'y': 506}, {'x': 337, 'y': 507}, {'x': 335, 'y': 536}, {'x': 315, 'y': 535}]}, 'description': 'to'}, ...]}

    @classmethod
    def from_api_repr(cls, response):
        """Factory: construct an instance of ``Annotations`` from a response.

            :type response: dict
            :param response: Vision API response object.

            :rtype: :class:`~google.cloud.vision.annotations.Annotations`
            :returns: An instance of ``Annotations`` with detection types loaded.
            """
        annotations = {}
        for feature_type, annotation in response.items():
>           curr_feature = annotations.setdefault(_KEY_MAP[feature_type], [])
E           KeyError: u'fullTextAnnotation'

env/lib/python2.7/site-packages/google/cloud/vision/annotations.py:91: KeyError
  1. Steps to reproduce
  • Enable API backend with extra data in response to detect_text
  • Call detect_text
  • Extra content in response body crashes the client library
  1. Code example

The following code exercises the crash:

def detect_text(path):
    """Detects text in the file."""
    vision_client = vision.Client()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision_client.image(content=content)

    texts = image.detect_text()
    print('Texts:')
    for text in texts:
        print(text.description)

Metadata

Metadata

Assignees

Labels

api: visionIssues related to the Cloud Vision API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions