xtekky/gpt4free · error · RuntimeError

Create images failed. Code: {response.status}

Error message

Create images failed. Code: {response.status}

What it means

Raised as RuntimeError by Bing image creation when neither the rt=4 nor the retry rt=3 submission returns the expected 302 redirect. Without the redirect the provider cannot obtain the request id needed to poll results, so it fails with the last HTTP status code received.

Source

Thrown at g4f/Provider/needs_auth/bing/create_images.py:110

    async with session.post(
        url, allow_redirects=False, data=payload, timeout=timeout
    ) as response:
        response.raise_for_status()
        text = (await response.text()).lower()
        if "0 coins available" in text:
            raise RateLimitError(
                "No coins left. Log in with a different account or wait a while"
            )
        for error in ERRORS:
            if error in text:
                raise RuntimeError(f"Create images failed: {error}")
    if response.status != 302:
        url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=3&FORM=GENCRE"
        async with session.post(
            url, allow_redirects=False, timeout=timeout
        ) as response:
            if response.status != 302:
                raise RuntimeError(f"Create images failed. Code: {response.status}")

    redirect_url = response.headers["Location"].replace("&nfy=1", "")
    redirect_url = f"{BING_URL}{redirect_url}"
    request_id = redirect_url.split("id=")[-1]
    async with session.get(redirect_url) as response:
        response.raise_for_status()

    polling_url = (
        f"{BING_URL}/images/create/async/results/{request_id}?q={url_encoded_prompt}"
    )
    start_time = time.time()
    while True:
        if time.time() - start_time > timeout:
            raise RuntimeError(f"Timeout error after {timeout} sec")
        async with session.get(polling_url) as response:
            if response.status != 200:
                raise RuntimeError(f"Polling images faild. Code: {response.status}")
            text = await response.text()

View on GitHub (pinned to 973504e177)

Solutions

  1. Refresh the Bing cookies (log in again) — a 200 instead of 302 usually means unauthenticated
  2. Retry after a short delay for transient 5xx responses
  3. Check the returned status code in the message to distinguish auth (401/403) from server issues (5xx)

Example fix

# before
images = create_images(session, prompt, cookies)  # Create images failed. Code: 200

# after
# refresh cookies then retry
cookies = load_fresh_bing_cookies()
try:
    images = create_images(session, prompt, cookies)
except RuntimeError as e:
    logging.warning('Bing create failed: %s', e)
    images = fallback_image_provider(prompt)
Defensive patterns

Strategy: retry

Try / catch

try:
    images = create_images(session, prompt, cookies)
except RuntimeError as e:
    if 'Code: 5' in str(e) or 'Code: 429' in str(e):
        time.sleep(10)
        images = create_images(session, prompt, cookies)
    elif 'Code: 200' in str(e) or 'Code: 40' in str(e):
        refresh_bing_cookies()  # auth issue: re-login then retry
    else:
        raise

Prevention

When it happens

Trigger: Bing responds 200 (form re-render), 4xx (auth/CSRF cookie problems), or 5xx instead of redirecting after the create POST.

Common situations: Expired or missing _U cookie so Bing re-renders the page instead of accepting, Bing A/B changes in the create flow, temporary server errors.

Related errors


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