{"record":{"id":"08f7211c310aa3f0","repo":"xtekky/gpt4free","slug":"api-key-is-missing","errorCode":null,"errorMessage":"api_key is missing","messagePattern":"api_key is missing","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Replicate.py","lineNumber":41,"sourceCode":"        messages: Messages,\n        api_key: str = None,\n        proxy: str = None,\n        timeout: int = 180,\n        system_prompt: str = None,\n        max_tokens: int = None,\n        temperature: float = None,\n        top_p: float = None,\n        top_k: float = None,\n        stop: list = None,\n        extra_body: dict = {},\n        headers: dict = {\n            \"accept\": \"application/json\",\n        },\n        **kwargs,\n    ) -> AsyncResult:\n        model = cls.get_model(model)\n        if cls.needs_auth and api_key is None:\n            raise MissingAuthError(\"api_key is missing\")\n        if api_key is not None:\n            headers[\"Authorization\"] = f\"Bearer {api_key}\"\n            base_url = \"https://api.replicate.com/v1/models/\"\n        else:\n            base_url = \"https://replicate.com/api/models/\"\n        async with StreamSession(\n            proxy=proxy, headers=headers, timeout=timeout\n        ) as session:\n            data = {\n                \"stream\": True,\n                \"input\": {\n                    \"prompt\": format_prompt(messages),\n                    **filter_none(\n                        system_prompt=system_prompt,\n                        max_new_tokens=max_tokens,\n                        temperature=temperature,\n                        top_p=top_p,\n                        top_k=top_k,","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Replicate.py#L23-L59","documentation":"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.","triggerScenarios":"Invoking a needs_auth Replicate model (i.e. an official hosted model requiring the API) without api_key in the call.","commonSituations":"Routing a model name to Replicate without configuring credentials, forgetting that only community/unauthenticated endpoints work keyless.","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"],"exampleFix":"# before\nresponse = client.chat.completions.create(model='...', provider=g4f.Provider.Replicate)  # MissingAuthError\n\n# after\nresponse = client.chat.completions.create(model='...', provider=g4f.Provider.Replicate, api_key=os.environ['REPLICATE_API_KEY'])","handlingStrategy":"validation","validationCode":"api_key = os.environ.get('REPLICATE_API_KEY')\nif not api_key:\n    raise SystemExit('Set REPLICATE_API_KEY for Replicate models')","typeGuard":"def replicate_ready(api_key: str | None, needs_auth: bool) -> bool:\n    return not needs_auth or (isinstance(api_key, str) and api_key.startswith('r8_'))","tryCatchPattern":"from g4f.errors import MissingAuthError\ntry:\n    result = ...create(provider=g4f.Provider.Replicate)\nexcept MissingAuthError:\n    result = ...create(provider=g4f.Provider.Replicate, api_key=os.environ['REPLICATE_API_KEY'])","preventionTips":["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"],"tags":["authentication","missing-api-key","replicate","g4f"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}