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

  1. Pass a Replicate API token: client.chat.completions.create(..., api_key='r8_...')
  2. Store the token in env (e.g. REPLICATE_API_KEY) and read it into the call
  3. 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

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


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