xtekky/gpt4free · error · NoValidHarFileError
Access token is not valid: {cls.request_config.access_token}
Error message
Access token is not valid: {cls.request_config.access_token} What it means
Raised as NoValidHarFileError by OpenaiChat providers when the access token loaded from a HAR file (or cached request config) fails provider-side validation via cls._set_api_key(). The library tried to authenticate the provider automatically from captured browser traffic, but the token was rejected. If the nodriver package is installed, the provider attempts an automated browser login (RequestLogin + nodriver_auth) instead of failing; otherwise the error propagates.
Source
Thrown at g4f/Provider/needs_auth/OpenaiChat.py:1384
if cookies is not None:
cls.request_config.cookies = cookies
if api_key is not None:
cls._create_request_args(
cls.request_config.cookies, cls.request_config.headers
)
cls._set_api_key(api_key)
else:
try:
cls.request_config = await get_request_config(cls.request_config, proxy)
if cls.request_config is None:
cls.request_config = RequestConfig()
cls._create_request_args(
cls.request_config.cookies, cls.request_config.headers
)
if cls.needs_auth and cls.request_config.access_token is None:
raise NoValidHarFileError(f"Missing access token")
if not cls._set_api_key(cls.request_config.access_token):
raise NoValidHarFileError(
f"Access token is not valid: {cls.request_config.access_token}"
)
except NoValidHarFileError:
if has_nodriver:
if cls.request_config.access_token is None:
yield RequestLogin(
cls.label, os.environ.get("G4F_LOGIN_URL", "")
)
await cls.nodriver_auth(proxy)
else:
raise
@classmethod
async def nodriver_auth(cls, proxy: str = None):
async with get_nodriver_session(proxy=proxy) as browser:
page = await browser.get(cls.url)
def on_request(event: nodriver.cdp.network.RequestWillBeSent, page=None):View on GitHub (pinned to 973504e177)
Solutions
- Re-capture a fresh HAR file from the provider's web UI and place it where g4f looks for it (or clear the cached request config so it is reloaded)
- Pass a valid api_key directly to the provider call to bypass HAR-based auth
- Install nodriver (pip install nodriver) so the provider can trigger an automated browser login when the token is rejected
- Delete the cached RequestConfig/cookies for the provider so the next call re-authenticates from scratch
Example fix
# before
response = client.chat.completions.create(model='gpt-4o', messages=[...]) # NoValidHarFileError
# after
response = client.chat.completions.create(
model='gpt-4o',
messages=[...],
provider=g4f.Provider.OpenaiChat,
api_key='<valid-token>', # bypass stale HAR token
) Defensive patterns
Strategy: fallback
Validate before calling
from g4f.requests import get_cookies
cookies = get_cookies('chatgpt.com')
if not cookies:
raise SystemExit('No HAR/cookies: log in or run nodriver auth first') Type guard
def has_valid_token(cfg) -> bool:
return cfg is not None and isinstance(getattr(cfg, 'access_token', None), str) and len(cfg.access_token) > 20 Try / catch
try:
result = client.chat.completions.create(...)
except NoValidHarFileError:
result = client.chat.completions.create(..., api_key=explicit_key) # or trigger nodriver login Prevention
- Keep HAR files fresh; re-capture after provider password changes
- Install nodriver so the provider can self-heal via browser login
- Pass an explicit api_key for needs_auth providers in production instead of relying on HAR auth
When it happens
Trigger: Calling a needs_auth OpenAI-compatible provider (e.g. via g4f.Client or g4f.ChatCompletion.create) when the stored HAR/cookie token is expired, revoked, or belongs to a different account, and cls._set_api_key(access_token) returns False.
Common situations: Stale HAR files collected weeks earlier, provider changed its auth backend and old tokens no longer validate, running in CI/headless where nodriver is not installed so the auto-login fallback is unavailable.
Related errors
- No valid Blackbox session found. Please log in to Blackbox A
- DeepSeekAuth: No authentication found.
- DeepSeekAuth: No authentication found. Please add a DeepSeek
- DeepSeekAuth: Authorization token required. Please ensure HA
- API key is required for Puter.js API
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/19569a611ae16add.
Report an issue: GitHub.