'ParseError' for all examples in Jupyter playbook related to call Google API

Hello,

First at all thank you authors for your efforts to build this course.

I faced with an issue during work with some lessons - all exaples in Jupyter playbooks with google API iteraction (see call_LMM() function) fails with following error:

---------------------------------------------------------------------------
ParseError                                Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py:529, in _Parser._ConvertFieldValuePair(self, js, message, path)
    528     continue
--> 529   raise ParseError(
    530       ('Message type "{0}" has no field named "{1}" at "{2}".\n'
    531        ' Available Fields(except extensions): "{3}"').format(
    532            message_descriptor.full_name, name, path,
    533            [f.json_name for f in message_descriptor.fields]))
    534 if name in names:

ParseError: Message type "google.ai.generativelanguage.v1beta.GenerateContentResponse" has no field named "usageMetadata" at "GenerateContentResponse".
 Available Fields(except extensions): "['candidates', 'promptFeedback']"

The above exception was the direct cause of the following exception:

ParseError                                Traceback (most recent call last)
File /usr/local/lib/python3.11/site-packages/google/generativeai/types/generation_types.py:408, in rewrite_stream_error()
    407 try:
--> 408     yield
    409 except (google.protobuf.json_format.ParseError, AttributeError) as e:

File /usr/local/lib/python3.11/site-packages/google/generativeai/types/generation_types.py:451, in GenerateContentResponse.from_iterator(cls, iterator)
    450 with rewrite_stream_error():
--> 451     response = next(iterator)
    453 return cls(
    454     done=False,
    455     iterator=iterator,
    456     result=response,
    457 )

File /usr/local/lib/python3.11/site-packages/google/api_core/rest_streaming.py:116, in ResponseIterator.__next__(self)
    115         raise e
--> 116 return self._grab()

File /usr/local/lib/python3.11/site-packages/google/api_core/rest_streaming.py:121, in ResponseIterator._grab(self)
    120 if issubclass(self._response_message_cls, proto.Message):
--> 121     return self._response_message_cls.from_json(self._ready_objs.popleft())
    122 elif issubclass(self._response_message_cls, google.protobuf.message.Message):

File /usr/local/lib/python3.11/site-packages/proto/message.py:428, in MessageMeta.from_json(cls, payload, ignore_unknown_fields)
    427 instance = cls()
--> 428 Parse(payload, instance._pb, ignore_unknown_fields=ignore_unknown_fields)
    429 return instance

File /usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py:421, in Parse(text, message, ignore_unknown_fields, descriptor_pool, max_recursion_depth)
    420   raise ParseError('Failed to load JSON: {0}.'.format(str(e))) from e
--> 421 return ParseDict(js, message, ignore_unknown_fields, descriptor_pool,
    422                  max_recursion_depth)

File /usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py:446, in ParseDict(js_dict, message, ignore_unknown_fields, descriptor_pool, max_recursion_depth)
    445 parser = _Parser(ignore_unknown_fields, descriptor_pool, max_recursion_depth)
--> 446 parser.ConvertMessage(js_dict, message, '')
    447 return message

File /usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py:487, in _Parser.ConvertMessage(self, value, message, path)
    486 else:
--> 487   self._ConvertFieldValuePair(value, message, path)
    488 self.recursion_depth -= 1

File /usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py:616, in _Parser._ConvertFieldValuePair(self, js, message, path)
    615   else:
--> 616     raise ParseError(str(e)) from e
    617 except ValueError as e:

ParseError: Message type "google.ai.generativelanguage.v1beta.GenerateContentResponse" has no field named "usageMetadata" at "GenerateContentResponse".
 Available Fields(except extensions): "['candidates', 'promptFeedback']"

During handling of the above exception, another exception occurred:

BadRequest                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 call_LMM("invoice.png",
      2     """Identify items on the invoice, Make sure you output 
      3     JSON with quantity, description, unit price and ammount.""")

Cell In[3], line 13, in call_LMM(image_path, prompt, plain_text)
     10 img = PIL.Image.open(image_path)
     12 model = genai.GenerativeModel("gemini-1.5-flash")
---> 13 response = model.generate_content([prompt, img], stream=True)
     14 response.resolve()
     16 if(plain_text):

File /usr/local/lib/python3.11/site-packages/google/generativeai/generative_models.py:260, in GenerativeModel.generate_content(self, contents, generation_config, safety_settings, stream, tools, tool_config, request_options)
    255     with generation_types.rewrite_stream_error():
    256         iterator = self._client.stream_generate_content(
    257             request,
    258             **request_options,
    259         )
--> 260     return generation_types.GenerateContentResponse.from_iterator(iterator)
    261 else:
    262     response = self._client.generate_content(
    263         request,
    264         **request_options,
    265     )

File /usr/local/lib/python3.11/site-packages/google/generativeai/types/generation_types.py:450, in GenerateContentResponse.from_iterator(cls, iterator)
    447 @classmethod
    448 def from_iterator(cls, iterator: Iterable[glm.GenerateContentResponse]):
    449     iterator = iter(iterator)
--> 450     with rewrite_stream_error():
    451         response = next(iterator)
    453     return cls(
    454         done=False,
    455         iterator=iterator,
    456         result=response,
    457     )

File /usr/local/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    156     value = typ()
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.
    163     return exc is not value

File /usr/local/lib/python3.11/site-packages/google/generativeai/types/generation_types.py:410, in rewrite_stream_error()
    408     yield
    409 except (google.protobuf.json_format.ParseError, AttributeError) as e:
--> 410     raise google.api_core.exceptions.BadRequest(
    411         "Unknown error trying to retrieve streaming response. "
    412         "Please retry with `stream=False` for more details."
    413     )

BadRequest: 400 Unknown error trying to retrieve streaming response. Please retry with `stream=False` for more details.

Can someone fix this?

Thanks,

hi @aleht

can you explain which lesson of the selected short course threw this error?

Also a screenshot of code which threw this aforementioned error.

please kindly confirm, if you were running the codes in deeplearning.ai beta learning environment or running the lessons locally.

if you were running codes locally, did you download all the metadata from lesson assignment and also complied with requirements.txt file provided in the File==>Open section.

Regards
DP

Hello @Deepti_Prasad,

Steps to reproduce:

  1. Navigate to Large Multimodal Models (LLMs) part
  2. Run each line from the begginging of file
  3. See error after calling call_LLM() helper function:

This error also raised in lessons:

  1. Multimodal RAG (MM-RAG)

  2. Industry Applications

Hope it helps,

1 Like

???

@aleht

if you are running the lessons in course provided environment and encountering

BadRequest: 400 Unknown error trying to retrieve streaming response.

Then try clearing your cache and browsing history, log out of the beta learning id and then re login.

when next you start the lessons again you make sure once clear kernal output and restart kernel again, and then try running the codes.

Let me know if you still encountered this error.

I am running the codes in deeplearning.ai beta learning environment.

2 Likes

please try once the above approach

I tried to clear cache and restart kerner playbooks, but it doesn’t help((

See video
Screen Recording 2024-12-07 at 11.27.13.zip (17.3 MB)

@Mubsi

can you please look at this query, seemed there could be a metadata error with

these two points in error probably raises some server error.

Regards
DP

1 Like

Thank you for reaching out! We’re aware of the updates Google is making to the Gemini API that we use for this course, and we’re currently working to reflect these changes on the platform. In the meantime, I recommend reviewing the call_LMM method—it might need some adjustments. Check out this here “Explore vision capabilities with the Gemini API” for helpful guidance. Give it a try, and feel free to experiment! If you run into issues or need assistance, don’t hesitate to come back. I’ll keep you updated as soon as the platform is updated. :blush:

1 Like