xtekky/gpt4free · error · RuntimeError

API call failed with status {resp.status}: {text}

Error message

API call failed with status {resp.status}: {text}

What it means

Generic non-OK response from the Code Assist API call in AuthManager.call_endpoint. The 401-with-retry case is handled separately (cache cleared, auth re-initialized, one retry), so this error means the request failed with a status other than 401, or a 401 that persisted after the retry. The status code and body are included in the message.

Source

Thrown at g4f/Provider/needs_auth/GeminiCLI.py:503

        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.get_access_token()}",
        }
        if user_agent:
            headers["User-Agent"] = user_agent

        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=headers, json=body) as resp:
                if resp.status == 401 and not is_retry:
                    # Token likely expired, clear and retry once
                    await self.clear_token_cache()
                    await self.initialize_auth()
                    return await self.call_endpoint(
                        method, body, is_retry=True, user_agent=user_agent
                    )
                elif not resp.ok:
                    text = await resp.text()
                    raise RuntimeError(
                        f"API call failed with status {resp.status}: {text}"
                    )

                return await resp.json()


class GeminiCLIProvider:
    url = "https://cloud.google.com/code-assist"
    base_url = "https://cloudcode-pa.googleapis.com/v1internal"

    # Required for authentication and token management; Expects a compatible AuthManager instance
    auth_manager: AuthManager
    env: dict

    def __init__(self, env: dict, auth_manager: AuthManager):
        self.env = env
        self.auth_manager = auth_manager

View on GitHub (pinned to 973504e177)

Solutions

  1. Inspect the embedded status and body: 429 -> wait and retry later; 403 -> check account/project eligibility; 400 -> check model name and request payload
  2. Re-login to refresh credentials if the body indicates an auth problem despite the automatic retry
  3. Set GEMINI_PROJECT_ID explicitly to rule out discovery picking an ineligible project
  4. Upgrade g4f; the v1internal API surface changes frequently
Defensive patterns

Strategy: retry

Try / catch

import re
try:
    data = await auth_manager.call_endpoint(method, body)
except RuntimeError as e:
    m = re.search(r"status (\d+)", str(e))
    status = int(m.group(1)) if m else 0
    if status in (429, 500, 503):
        await asyncio.sleep(30)
        data = await auth_manager.call_endpoint(method, body)  # retry once
    else:
        raise

Prevention

When it happens

Trigger: POST to cloudcode-pa.googleapis.com/v1internal endpoints returning 400 (malformed body), 403 (project not allowlisted or not eligible), 429 (quota), or 5xx; or a second 401 after is_retry=True.

Common situations: Model name not supported by the internal Code Assist API; project-level quota exhausted (429 with RetryInfo); account lost Gemini Code Assist entitlement; stale token that stays invalid after one refresh retry.

Related errors


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