xtekky/gpt4free · error · ImportError

pillow is required for system tray support. Install it with:

Error message

pillow is required for system tray support. Install it with: pip install pystray pillow

What it means

g4f/tray.py raises this ImportError only in the fallback path: _make_icon() returned None (no bundled icon image could be built), so the code tries to create a 1x1 transparent PNG with PIL, and 'from PIL import Image' fails because pillow is not installed. pystray alone is not enough — an icon image needs pillow.

Source

Thrown at g4f/tray.py:204

    menu = pystray.Menu(
        pystray.MenuItem("Open in Browser", on_open_browser, default=True),
        pystray.Menu.SEPARATOR,
        pystray.MenuItem(server_label, on_toggle_server, enabled=server_enabled),
        pystray.Menu.SEPARATOR,
        pystray.MenuItem("Show logs", lambda: on_open_browser("/logs")),
        pystray.Menu.SEPARATOR,
        pystray.MenuItem("Quit", on_quit),
    )

    icon_image = _make_icon()
    if icon_image is None:
        # Fallback: 1×1 transparent image (pystray still works, icon may be blank)
        try:
            from PIL import Image

            icon_image = Image.new("RGBA", (1, 1))
        except ImportError:
            raise ImportError(
                "pillow is required for system tray support. "
                "Install it with: pip install pystray pillow"
            )

    tray_icon = pystray.Icon(
        name="g4f",
        icon=icon_image,
        title="g4f AI Server",
        menu=menu,
    )

    # Auto-start the server unless opted out
    if not no_autostart:
        _start_server()

    tray_icon.run()

View on GitHub (pinned to 973504e177)

Solutions

  1. Install pillow: pip install pystray pillow (the error message asks for both)
  2. If _make_icon() returning None is itself unexpected, verify the g4f installation is complete (reinstall: pip install --force-reinstall g4f)
  3. Run without the tray icon via the plain API entry point if an icon is not needed

Example fix

# before
pip install pystray  # pillow still missing -> fallback ImportError

# after
pip install pystray pillow
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util

has_pillow = importlib.util.find_spec("PIL") is not None

Try / catch

try:
    icon_image = Image.new("RGBA", (1, 1))
except ImportError:
    # run tray with a generated icon file or skip the icon entirely
    pass

Prevention

When it happens

Trigger: Running the tray with pystray installed but pillow missing, AND the normal icon source unavailable (custom icon not found/not writable), so the 1x1 fallback is attempted.

Common situations: Installing pystray without pillow; environments where the icon asset path is broken (partial install, read-only dir) so _make_icon() returns None; CI containers with trimmed image libraries.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/384fe5623ac19997. Report an issue: GitHub.