xtekky/gpt4free · error · RuntimeError

Could not discover project ID. Ensure authentication or set

Error message

Could not discover project ID. Ensure authentication or set GEMINI_PROJECT_ID.

What it means

Catch-all wrapper around project discovery: any exception other than MissingAuthError raised while loading or onboarding the project is logged via debug.error and re-raised as this RuntimeError. The original cause is in the debug log rather than the exception chain, which makes diagnosis harder.

Source

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

                    "metadata": {"duetProject": "default-project"},
                },
            )
            project = load_response.get("cloudaicompanionProject")
            if project:
                self._project_id = project
                return project
            project = await self.onboard_managed_project(
                access_token=self.auth_manager.get_access_token(), tier_id="free-tier"
            )
            if project:
                self._project_id = project
                return project
            raise RuntimeError("No project information found in API response.")
        except MissingAuthError:
            raise
        except Exception as e:
            debug.error(f"Failed to discover project ID: {e}")
            raise RuntimeError(
                "Could not discover project ID. Ensure authentication or set GEMINI_PROJECT_ID."
            )

    async def onboard_managed_project(
        self,
        access_token: str,
        tier_id: str,
        project_id: Optional[str] = "default-project",
        attempts: int = 10,
        delay_ms: int = 5000,
    ) -> Optional[str]:
        """
        Onboard a managed project for the user, optionally retrying until completion.

        Args:
            access_token (str): Bearer token for authorization.
            tier_id (str): Tier ID to use for onboarding.
            project_id (Optional[str]): Optional project ID to onboard.

View on GitHub (pinned to 973504e177)

Solutions

  1. Run with g4f debug logging enabled to see the logged original error 'Failed to discover project ID: ...'
  2. Set GEMINI_PROJECT_ID to skip discovery entirely
  3. Check network and proxy access to cloudcode-pa.googleapis.com
  4. Retry after transient failures; re-login if the underlying error is auth-related
Defensive patterns

Strategy: fallback

Validate before calling

import socket

def can_reach_code_assist(host="cloudcode-pa.googleapis.com", port=443, timeout=5) -> bool:
    try:
        socket.create_connection((host, port), timeout=timeout).close()
        return True
    except OSError:
        return False

Try / catch

try:
    await provider.discover_project_id()
except RuntimeError as e:
    if "Could not discover project ID" in str(e):
        # original cause is in the debug log; fall back to explicit project
        provider._project_id = os.environ["GEMINI_PROJECT_ID"]

Prevention

When it happens

Trigger: Network timeouts, JSON decode errors, unexpected response shapes, or non-403 HTTP errors inside discover_project_id that are not MissingAuthError.

Common situations: Egress blocked to cloudcode-pa.googleapis.com; proxy interference; transient 5xx during discovery; response schema drift after a Google-side change.

Understand the failure class

Related errors


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