xtekky/gpt4free · error · RuntimeError
Failed to read OAuth credentials from {path}: {e}
Error message
Failed to read OAuth credentials from {path}: {e} What it means
RuntimeError raised while loading Antigravity OAuth credentials from disk: a credentials file exists (either the auth-manager cache or the default path from get_antigravity_oauth_creds_path()), but opening/parsing it raised — json.load failing on malformed JSON is the usual cause, hence the {e} chaining json.JSONDecodeError. The {path} in the message names the exact file that failed.
Source
Thrown at g4f/Provider/needs_auth/Antigravity.py:408
now = time.time()
if cached:
expires_at = cached["expiry_date"] / 1000 # ms to seconds
if expires_at - now > self.TOKEN_BUFFER_TIME:
self._access_token = cached["access_token"]
self._expiry = expires_at
return # Use cached token if valid
# Try loading from cache file or default path
path = AntigravityAuthManager.get_cache_file()
if not path.exists():
path = get_antigravity_oauth_creds_path()
if path.exists():
try:
with path.open("r") as f:
creds = json.load(f)
except Exception as e:
raise RuntimeError(f"Failed to read OAuth credentials from {path}: {e}")
else:
# Parse credentials from environment
if "ANTIGRAVITY_SERVICE_ACCOUNT" not in self.env:
raise RuntimeError(
"ANTIGRAVITY_SERVICE_ACCOUNT environment variable not set. "
f"Please set it or create credentials at {get_antigravity_oauth_creds_path()}"
)
creds = json.loads(self.env["ANTIGRAVITY_SERVICE_ACCOUNT"])
# Store project_id from credentials if available
if creds.get("project_id"):
self._project_id = creds["project_id"]
refresh_token = creds.get("refresh_token")
access_token = creds.get("access_token")
expiry_date = creds.get("expiry_date") # milliseconds since epoch
# Use original access token if still validView on GitHub (pinned to 973504e177)
Solutions
- Validate the file named in the message: python -m json.tool <path> — it must be valid JSON.
- If corrupt, delete (or move aside) the cache file and the default creds file; the flow will fall back to the service-account env var or a fresh interactive login.
- If the JSON is valid but the error persists, check {e} for permission/encoding issues (file readable, UTF-8).
Example fix
# before (truncated/corrupt cache keeps failing)
# ~/.cache/antigravity/oauth_creds.json -> {"refresh_token": "ya29."
# after
rm ~/.cache/antigravity/oauth_creds.json # then re-run auth to regenerate Defensive patterns
Strategy: validation
Validate before calling
import json, pathlib
path = pathlib.Path(creds_path)
if path.exists():
try:
json.loads(path.read_text())
except json.JSONDecodeError as e:
raise SystemExit(f'Corrupt Antigravity creds at {path}: delete it and re-auth ({e})') Try / catch
try:
...
except RuntimeError as e:
if 'Failed to read OAuth credentials' in str(e):
creds_path.unlink(missing_ok=True) # drop corrupt cache, re-run auth flow
... Prevention
- Validate credential JSON with json.tool after any manual edit.
- Write credential caches atomically (tmp file + rename) in your own code.
- Delete and regenerate rather than hand-repairing corrupt token files.
When it happens
Trigger: The cached/default credentials JSON is truncated (process killed mid-write), hand-edited and made invalid (trailing commas, comments, smart quotes), empty (0 bytes), or not JSON at all (e.g. a HTML error page saved there by mistake); unreadable encoding can also raise here.
Common situations: Crash during token cache write leaving a partial file; users pasting credentials with formatting damage; tools writing logs into the creds path; the cache file being from an incompatible older schema that no longer parses.
Related errors
- No refresh token found in credentials.
- OAuth error: {OAuthCallbackHandler.callback_error}
- ANTIGRAVITY_SERVICE_ACCOUNT environment variable not set. Pl
- Token refresh failed: {text}
- No access_token in refresh response.
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/90f956edb69036fc.
Report an issue: GitHub.