xtekky/gpt4free · error · MissingAuthError
api_key is missing
Error message
api_key is missing
What it means
Raised as MissingAuthError by the Replicate provider when cls.needs_auth is true and no api_key argument was supplied. Without a key the provider would fall back to the unauthenticated replicate.com/api/models endpoint, but the needs_auth flag forbids that, so it fails fast.
Source
Thrown at g4f/Provider/needs_auth/Replicate.py:41
messages: Messages,
api_key: str = None,
proxy: str = None,
timeout: int = 180,
system_prompt: str = None,
max_tokens: int = None,
temperature: float = None,
top_p: float = None,
top_k: float = None,
stop: list = None,
extra_body: dict = {},
headers: dict = {
"accept": "application/json",
},
**kwargs,
) -> AsyncResult:
model = cls.get_model(model)
if cls.needs_auth and api_key is None:
raise MissingAuthError("api_key is missing")
if api_key is not None:
headers["Authorization"] = f"Bearer {api_key}"
base_url = "https://api.replicate.com/v1/models/"
else:
base_url = "https://replicate.com/api/models/"
async with StreamSession(
proxy=proxy, headers=headers, timeout=timeout
) as session:
data = {
"stream": True,
"input": {
"prompt": format_prompt(messages),
**filter_none(
system_prompt=system_prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
top_k=top_k,View on GitHub (pinned to 973504e177)
Solutions
- Pass a Replicate API token: client.chat.completions.create(..., api_key='r8_...')
- Store the token in env (e.g. REPLICATE_API_KEY) and read it into the call
- If you intended keyless use, pick a model that does not set needs_auth
Example fix
# before response = client.chat.completions.create(model='...', provider=g4f.Provider.Replicate) # MissingAuthError # after response = client.chat.completions.create(model='...', provider=g4f.Provider.Replicate, api_key=os.environ['REPLICATE_API_KEY'])
Defensive patterns
Strategy: validation
Validate before calling
api_key = os.environ.get('REPLICATE_API_KEY')
if not api_key:
raise SystemExit('Set REPLICATE_API_KEY for Replicate models') Type guard
def replicate_ready(api_key: str | None, needs_auth: bool) -> bool:
return not needs_auth or (isinstance(api_key, str) and api_key.startswith('r8_')) Try / catch
from g4f.errors import MissingAuthError
try:
result = ...create(provider=g4f.Provider.Replicate)
except MissingAuthError:
result = ...create(provider=g4f.Provider.Replicate, api_key=os.environ['REPLICATE_API_KEY']) Prevention
- Keep a credentials registry keyed by provider and check it before dispatching requests
- Use keyless Replicate models only when needs_auth is False for that model
When it happens
Trigger: Invoking a needs_auth Replicate model (i.e. an official hosted model requiring the API) without api_key in the call.
Common situations: Routing a model name to Replicate without configuring credentials, forgetting that only community/unauthenticated endpoints work keyless.
Related errors
- API key is required for Puter.js API
- Access token is not valid: {cls.request_config.access_token}
- No cookies found for {cls.domain}
- No appSession found in cookies for {cls.domain}, log in or p
- Failed to get access token: {e}, refresh your cookies / log
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/08f7211c310aa3f0.
Report an issue: GitHub.