Skip to content

AttributeError: module 'PIL' has no attribute 'PngImagePlugin' when running Gemini model on Windows 11 #178

@DHerls

Description

@DHerls

Description of the bug:

When attempting to run sample gemini text + image code on Windows, I receive the following error:

Traceback (most recent call last):
  File "C:\Users\dan\Projects\gemini_bug_report\without_import.py", line 10, in <module>
    response = model.generate_content(["Is the following image white?", image], stream=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\generative_models.py", line 234, in generate_content
    request = self._prepare_request(
              ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\generative_models.py", line 204, in _prepare_request
    contents = content_types.to_contents(contents)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 239, in to_contents
    contents = [to_content(contents)]
                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 202, in to_content
    return glm.Content(parts=[to_part(part) for part in content])
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 202, in <listcomp>
    return glm.Content(parts=[to_part(part) for part in content])
                              ^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 172, in to_part
    return glm.Part(inline_data=to_blob(part))
                                ^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 135, in to_blob
    return image_to_blob(blob)
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 63, in image_to_blob
    return pil_to_blob(image)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\dan\.virtualenvs\gemini_bug_report-gfzPZTWW\Lib\site-packages\google\generativeai\types\content_types.py", line 49, in pil_to_blob
    if isinstance(img, PIL.PngImagePlugin.PngImageFile):
                       ^^^^^^^^^^^^^^^^^^
AttributeError: module 'PIL' has no attribute 'PngImagePlugin'

Versions:

Windows 11
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32

google-generativeai==0.3.2
pillow==10.2.0

I believe this is a problem in how this library is referencing PngImagePlugin

See reproduction here: https://github.com/DHerls/google_genai_import_bug_repro

File without_import.py causes the issue for me and with_import.py runs as expected.

Actual vs expected behavior:

Actual behavior: AttributeError: module 'PIL' has no attribute 'PngImagePlugin'
Expected behavior: Code runs

Any other information you'd like to share?

Proposed solution

Change code that references PngImagePlugin like this:

import PIL
...
PIL.PngImagePlugin

to code that references PngImagePlugin like this:

from PIL import PngImagePlugin
...
PngImportPlugin

Metadata

Metadata

Assignees

No one assigned

    Labels

    component:python sdkIssue/PR related to Python SDKstatus:awaiting user responseAwaiting a response from the authorstatus:staleIssue/PR will be closed automatically if there's no further activitytype:bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions