{"record":{"id":"93868ee97c9c01f6","repo":"xtekky/gpt4free","slug":"add-a-api-key-93868e","errorCode":null,"errorMessage":"Add a \"api_key\"","messagePattern":"Add a \"api_key\"","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/hf/HuggingFaceMedia.py","lineNumber":141,"sourceCode":"        model: str,\n        messages: Messages,\n        api_key: str = None,\n        extra_body: dict = None,\n        prompt: str = None,\n        proxy: str = None,\n        timeout: int = 0,\n        # Video & Image Generation\n        n: int = 1,\n        aspect_ratio: str = None,\n        # Only for Image Generation\n        height: int = None,\n        width: int = None,\n        # Video Generation\n        resolution: str = \"480p\",\n        **kwargs,\n    ):\n        if not api_key:\n            raise MissingAuthError('Add a \"api_key\"')\n        if extra_body is None:\n            extra_body = {}\n        selected_provider = None\n        if model and \":\" in model:\n            model, selected_provider = model.split(\":\", 1)\n        elif not model:\n            model = cls.get_models()[0]\n        prompt = format_media_prompt(messages, prompt)\n        provider_mapping = await cls.get_mapping(model, api_key)\n        headers = {\n            \"Accept-Encoding\": \"gzip, deflate\",\n            \"Content-Type\": \"application/json\",\n            \"Prefer\": \"wait\",\n        }\n        new_mapping = {\n            \"hf-free\" if key == \"hf-inference\" else key: value\n            for key, value in provider_mapping.items()\n            if key in [\"replicate\", \"together\", \"hf-inference\"]","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/hf/HuggingFaceMedia.py#L123-L159","documentation":"Raised by HuggingFaceMedia.create_async_generator when no api_key argument was supplied. HuggingFaceMedia routes media (image/video) generation through the HuggingFace router API which requires a bearer token, so the provider fails immediately with MissingAuthError rather than making a doomed request.","triggerScenarios":"Calling a HuggingFaceMedia model (image or video generation) without passing api_key (via kwargs, provider settings, or env-based auth).","commonSituations":"Assuming hf-free-style access works for media models; forgetting to configure auth for a needs_auth provider; passing the key under the wrong kwarg name.","solutions":["Create a HuggingFace access token at huggingface.co/settings/tokens","Pass it as api_key when calling the provider, or configure it in g4f provider settings","Verify the token has the required scope for inference/router calls","If you wanted keyless access, use the hf-free text providers instead of HuggingFaceMedia"],"exampleFix":"# before\nresponse = await HuggingFaceMedia.create_async_generator(model='...', messages=messages)\n# after\nresponse = await HuggingFaceMedia.create_async_generator(model='...', messages=messages, api_key='hf_xxx')","handlingStrategy":"validation","validationCode":"if not api_key:\n    api_key = os.environ.get('HF_API_KEY')\nif not api_key:\n    raise SystemExit('Set HF_API_KEY or pass api_key= for HuggingFaceMedia')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Store the HuggingFace token in an env var and inject it at call time","Check api_key presence before invoking any needs_auth provider","Keep the token scoped and rotated from huggingface.co/settings/tokens"],"tags":["huggingface","authentication","api-key","media-generation"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}