binary-husky/gpt_academic · error · KeyError

No available key found.

Error message

No available key found.

What it means

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.

Source

Thrown at request_llms/key_manager.py:27

        return _instance[cls]

    return _singleton


@Singleton
class OpenAI_ApiKeyManager():
    def __init__(self, mode='blacklist') -> None:
        # self.key_avail_list = []
        self.key_black_list = []

    def add_key_to_blacklist(self, key):
        self.key_black_list.append(key)

    def select_avail_key(self, key_list):
        # select key from key_list, but avoid keys also in self.key_black_list, raise error if no key can be found
        available_keys = [key for key in key_list if key not in self.key_black_list]
        if not available_keys:
            raise KeyError("No available key found.")
        selected_key = random.choice(available_keys)
        return selected_key

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Supply valid OpenAI API keys in config.py (or the corresponding environment variable) and restart.
  2. Inspect key_black_list state (add logging) to confirm every configured key was blacklisted and why.
  3. If keys are valid but rate-limited, wait for quota reset or lower request rate so keys stop being blacklisted.
  4. Deduplicate the key list before selection so one bad key does not consume multiple slots.

Example fix

# before
available_keys = [key for key in key_list if key not in self.key_black_list]
if not available_keys:
    raise KeyError("No available key found.")

# after: report which pool was exhausted
available_keys = [key for key in set(key_list) if key not in self.key_black_list]
if not available_keys:
    raise KeyError(f"No available key found. pool={len(key_list)} blacklisted={len(self.key_black_list)}")
Defensive patterns

Strategy: validation

Validate before calling

def has_available_key(mgr, key_list) -> bool:
    return any(k not in mgr.key_black_list for k in set(key_list))

if not has_available_key(mgr, key_list):
    # surface a config error instead of KeyError mid-request
    raise ConfigError('All API keys blacklisted; update config.py')

Try / catch

try:
    key = mgr.select_avail_key(key_list)
except KeyError:
    notify_user('All API keys exhausted — check keys/quota'); abort_run()

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/f8a2b314f3b73c3f. Report an issue: GitHub.