google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.18k stars 223 forks source link

TypeError Could not create `Blob`, expected `Blob`, `dict` or an `Image` type(`PIL.Image.Image` or `IPython.display.Image`) #380

Open petrgazarov opened 4 weeks ago

petrgazarov commented 4 weeks ago

Description of the bug:

Getting this error sometimes. What does it mean? If I rerun the script, it is not raised every time, but only sometimes.

REQUEST_TIMEOUT = 90

# model is `gemini-1.5-flash`    
client = genai.GenerativeModel(model, system_instruction=system_instruction)

google_completion = await asyncio.wait_for(
    client.generate_content_async(
        messages,
        safety_settings={
            HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
            HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
            HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
            HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
        },
    ),
    REQUEST_TIMEOUT,
)

Actual vs expected behavior:

Actual:

TypeError Could not create `Blob`, expected `Blob`, `dict` or an `Image` type(`PIL.Image.Image` or `IPython.display.Image`).
Got a: <class 'NoneType'>
Value: None

Expected there to be no error

Any other information you'd like to share?

No response

MarkDaoust commented 3 weeks ago

Right, that's not the best error.

This is at the step where it's trying to convert your input into the classes the api actually uses.

And you have a None in your list of contents or one of their lists of parts.

I should make these errors clearer. You need to track down where that None is coming from.