Skip to content

feat: add GEMINI_API_KEY and Pollinations.ai fallbacks for image generation#11

Open
nadimtuhin wants to merge 2 commits into
AndyShaman:mainfrom
nadimtuhin:upstream-pr/auto-image-gen
Open

feat: add GEMINI_API_KEY and Pollinations.ai fallbacks for image generation#11
nadimtuhin wants to merge 2 commits into
AndyShaman:mainfrom
nadimtuhin:upstream-pr/auto-image-gen

Conversation

@nadimtuhin
Copy link
Copy Markdown

Problem

Users hit ImageGenerationBlocked when the image-gen SIDCC rotates out of Chrome's SQLite store between uses. Currently there's no way to generate images in that window without a manual cookie refresh.

Solution

Two optional fallback backends, both opt-in via env vars so existing users are completely unaffected:

1. Google AI Studio API key (GEMINI_API_KEY)

export GEMINI_API_KEY=AIzaSy...   # from aistudio.google.com/apikey

Uses the official generativelanguage.googleapis.com endpoint. Tried first when the env var is present; falls back to the cookie session on 429 or error. Supports an optional model override:

export GEMINI_IMAGE_API_MODEL=gemini-2.5-flash-image   # default

2. Pollinations.ai (GEMINI_USE_POLLINATIONS=1)

export GEMINI_USE_POLLINATIONS=1

Zero-auth, no-account-required image generation via Pollinations.ai (free, MIT-licenced service). Uses curl_cffi Chrome impersonation — plain urllib gets a 403. Generates images using the Flux model.

Behaviour

Both paths are attempted only for simple text-to-image requests (prompt only, no files, no conversation_id). Editing, chat, and file-upload flows are completely unchanged.

Priority order when both are configured:

  1. GEMINI_API_KEY — official API, highest quality
  2. GEMINI_USE_POLLINATIONS=1 — zero-auth fallback
  3. Browser cookie session — existing behaviour

Testing

# Test API key path
export GEMINI_API_KEY=<your-key>
# Ask Claude: "generate an image of a red circle"

# Test Pollinations path
export GEMINI_USE_POLLINATIONS=1
# Ask Claude: "generate an image of a red circle"
# Works even without GEMINI_PSID set

Checklist

  • Opt-in only — no behaviour change for existing users
  • Both paths short-circuit before the library session is initialised
  • PermissionError handled gracefully in path checks
  • No new required dependencies (curl_cffi already in the dependency tree)
  • Env vars documented in commit message

🤖 Generated with Claude Code

nadimtuhin and others added 2 commits May 31, 2026 06:18
…ration

Adds two optional image generation backends that activate before the
browser-cookie session, giving users more flexibility and resilience:

**1. Google Generative Language API (`GEMINI_API_KEY`)**
Set `GEMINI_API_KEY` to a Google AI Studio key to use the official API
for text-to-image generation. Tried first when the env var is present.
Supports `GEMINI_IMAGE_API_MODEL` override (default: `gemini-2.5-flash-image`).

**2. Pollinations.ai fallback (`GEMINI_USE_POLLINATIONS=1`)**
Zero-auth, no-key-required fallback via https://pollinations.ai (MIT
licence, free tier). Uses `curl_cffi` Chrome impersonation — required
because Pollinations blocks plain HTTP clients. Opt-in via env var so
existing users are unaffected.

Both paths are tried only for simple text-to-image requests (no `files`,
no `conversation_id`) and short-circuit before the library session is
initialised, so cookie-based editing/chat flows are unchanged.

**Why Pollinations?**
Users occasionally hit `ImageGenerationBlocked` when image-gen SIDCC
has rotated out of Chrome's SQLite store. A zero-auth fallback lets the
tool remain useful in those windows without requiring a manual cookie
refresh.

**New env vars:**
- `GEMINI_API_KEY` — Google AI Studio API key
- `GEMINI_IMAGE_API_MODEL` — model override for the API path (default: `gemini-2.5-flash-image`)
- `GEMINI_USE_POLLINATIONS` — set to `1` to enable Pollinations fallback

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers URL encoding, Chrome impersonation, and error handling for
_pollinations_generate_image without requiring network, an API key,
or browser cookies — runs in CI as-is.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant