{"record":{"id":"f8a2b314f3b73c3f","repo":"binary-husky/gpt_academic","slug":"no-available-key-found","errorCode":null,"errorMessage":"No available key found.","messagePattern":"No available key found\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"request_llms/key_manager.py","lineNumber":27,"sourceCode":"        return _instance[cls]\n\n    return _singleton\n\n\n@Singleton\nclass OpenAI_ApiKeyManager():\n    def __init__(self, mode='blacklist') -> None:\n        # self.key_avail_list = []\n        self.key_black_list = []\n\n    def add_key_to_blacklist(self, key):\n        self.key_black_list.append(key)\n\n    def select_avail_key(self, key_list):\n        # select key from key_list, but avoid keys also in self.key_black_list, raise error if no key can be found\n        available_keys = [key for key in key_list if key not in self.key_black_list]\n        if not available_keys:\n            raise KeyError(\"No available key found.\")\n        selected_key = random.choice(available_keys)\n        return selected_key\n","sourceCodeStart":9,"sourceCodeEnd":30,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/key_manager.py#L9-L30","documentation":"OpenAI_ApiKeyManager.select_avail_key raises KeyError('No available key found.') when every key in the caller-supplied key_list is also in key_black_list (keys previously blacklisted after failures). It is an explicit depletion signal, not an index bug: the pool has been exhausted by blacklisting.","triggerScenarios":"Multiple consecutive API failures each call add_key_to_blacklist(key); once the last remaining key of APIKEY_LAYOUT/key_list is blacklisted, the next select_avail_key call finds available_keys empty and raises.","commonSituations":"All configured OpenAI keys are invalid/expired or quota-exhausted; a single bad key tested repeatedly until blacklisted; key_list containing duplicates so one bad key poisons the whole list; misconfigured APIKEY_LAYOUT in config.py.","solutions":["Supply valid OpenAI API keys in config.py (or the corresponding environment variable) and restart.","Inspect key_black_list state (add logging) to confirm every configured key was blacklisted and why.","If keys are valid but rate-limited, wait for quota reset or lower request rate so keys stop being blacklisted.","Deduplicate the key list before selection so one bad key does not consume multiple slots."],"exampleFix":"# before\navailable_keys = [key for key in key_list if key not in self.key_black_list]\nif not available_keys:\n    raise KeyError(\"No available key found.\")\n\n# after: report which pool was exhausted\navailable_keys = [key for key in set(key_list) if key not in self.key_black_list]\nif not available_keys:\n    raise KeyError(f\"No available key found. pool={len(key_list)} blacklisted={len(self.key_black_list)}\")","handlingStrategy":"validation","validationCode":"def has_available_key(mgr, key_list) -> bool:\n    return any(k not in mgr.key_black_list for k in set(key_list))\n\nif not has_available_key(mgr, key_list):\n    # surface a config error instead of KeyError mid-request\n    raise ConfigError('All API keys blacklisted; update config.py')","typeGuard":null,"tryCatchPattern":"try:\n    key = mgr.select_avail_key(key_list)\nexcept KeyError:\n    notify_user('All API keys exhausted — check keys/quota'); abort_run()","preventionTips":["Validate at startup that at least one configured key passes a cheap API ping.","Blacklist keys only after N confirmed failures, not on first error.","Deduplicate the key pool so one bad key can't exhaust multiple slots."],"tags":["api-key","key-management","blacklist","quota"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}