{"record":{"id":"ad708e6d94322782","repo":"xtekky/gpt4free","slug":"api-call-failed-with-status-resp-status-text","errorCode":null,"errorMessage":"API call failed with status {resp.status}: {text}","messagePattern":"API call failed with status (.+?): (.+?)","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/GeminiCLI.py","lineNumber":503,"sourceCode":"        headers = {\n            \"Content-Type\": \"application/json\",\n            \"Authorization\": f\"Bearer {self.get_access_token()}\",\n        }\n        if user_agent:\n            headers[\"User-Agent\"] = user_agent\n\n        async with aiohttp.ClientSession() as session:\n            async with session.post(url, headers=headers, json=body) as resp:\n                if resp.status == 401 and not is_retry:\n                    # Token likely expired, clear and retry once\n                    await self.clear_token_cache()\n                    await self.initialize_auth()\n                    return await self.call_endpoint(\n                        method, body, is_retry=True, user_agent=user_agent\n                    )\n                elif not resp.ok:\n                    text = await resp.text()\n                    raise RuntimeError(\n                        f\"API call failed with status {resp.status}: {text}\"\n                    )\n\n                return await resp.json()\n\n\nclass GeminiCLIProvider:\n    url = \"https://cloud.google.com/code-assist\"\n    base_url = \"https://cloudcode-pa.googleapis.com/v1internal\"\n\n    # Required for authentication and token management; Expects a compatible AuthManager instance\n    auth_manager: AuthManager\n    env: dict\n\n    def __init__(self, env: dict, auth_manager: AuthManager):\n        self.env = env\n        self.auth_manager = auth_manager\n","sourceCodeStart":485,"sourceCodeEnd":521,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/GeminiCLI.py#L485-L521","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the embedded status and body: 429 -> wait and retry later; 403 -> check account/project eligibility; 400 -> check model name and request payload","Re-login to refresh credentials if the body indicates an auth problem despite the automatic retry","Set GEMINI_PROJECT_ID explicitly to rule out discovery picking an ineligible project","Upgrade g4f; the v1internal API surface changes frequently"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"import re\ntry:\n    data = await auth_manager.call_endpoint(method, body)\nexcept RuntimeError as e:\n    m = re.search(r\"status (\\d+)\", str(e))\n    status = int(m.group(1)) if m else 0\n    if status in (429, 500, 503):\n        await asyncio.sleep(30)\n        data = await auth_manager.call_endpoint(method, body)  # retry once\n    else:\n        raise","preventionTips":["Branch on the embedded status code instead of treating all failures alike","Respect RetryInfo delays on 429 responses","Confirm model names against the provider's supported list before sending"],"tags":["gemini","http","api","quota","error-handling"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}