Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Error using openai api for Gemini AI

I am trying to use the experimental OpenAI library with Gemini (google/gemini-1.5-flash-001) for some translation tasks. These are translation of various type of articles (e.g., news), which I receive from a data pipeline. In general it works great, but about 10-15% of time, I get one specific error, where the response from the chat.completion API is mostly none. Here is a sample:

2024-06-27 11:33:29 - routes.utils.llm_utils - ERROR - OpenAI API error: ['NoneType' object is not subscriptable], messages: [[{'role': 'user', 'content': 'Translate verbatim to English, and only return the English translation: \n ...... <text for translation> ....'}]], response: [ChatCompletion(id=None, choices=None, created=None, model='google/gemini-1.5-flash-001', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=0, prompt_tokens=212, total_tokens=212))]

The "['NoneType' object is not subscriptable]" comes becuase I am trying to extract

response.choices[0] from nonexistent response values.

Has anyone else noticed this error? Any ideas about resolving this?

Thanks!

2 1 1,494
1 REPLY 1

Sometime back, when I was printing a streaming response for a Gemini Model, I encountered an error: `'NoneType' object is not subscriptable`.

In my experiment, I sent a prompt `ABC` and the AI model replied with message chunks like `('123', '456', '789', '')`. The last chunk in this message stream was an empty string `` or a trailing line break (`\n`). When I was printing `print(response.choices[0])`, the last response chunk's - `response.choices` would be an empty list, and `response.choices[0]`  gives an error.

To resolve this issue, I simply checked if there were any messages in `response.choices`. Instead of `print(response.choices[0])`, I used `if response.choices: print(response.choices[0])`.