binary-husky/gpt_academic · error · RuntimeError

您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源

Error message

您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源(左上角更换模型菜单中可切换openai,azure,claude,cohere等请求源)。

What it means

Raised by select_api_key_for_chat_models in shared_utils/key_pattern_manager.py:118 when none of the comma-separated keys in the API_KEY pool matches the pattern required by the requested chat model. The function filters the key list through per-provider detectors (is_azure_api_key, is_cohere_api_key, is_openroute_api_key, etc.) based on the model-name prefix, and the error fires when the filtered list is empty. In practice it means the key you supplied belongs to a different provider than the model/request source you selected in the model-switch menu.

Source

Thrown at shared_utils/key_pattern_manager.py:118

    if llm_model.startswith('api2d-'):
        for k in key_list:
            if is_api2d_key(k): avail_key_list.append(k)

    if llm_model.startswith('azure-'):
        for k in key_list:
            if is_azure_api_key(k): avail_key_list.append(k)

    if llm_model.startswith('cohere-'):
        for k in key_list:
            if is_cohere_api_key(k): avail_key_list.append(k)

    if llm_model.startswith('openrouter-'):
        for k in key_list:
            if is_openroute_api_key(k): avail_key_list.append(k)

    if len(avail_key_list) == 0:
        raise RuntimeError(f"您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源(左上角更换模型菜单中可切换openai,azure,claude,cohere等请求源)。")

    api_key = random.choice(avail_key_list) # 随机负载均衡
    return api_key


def select_api_key_for_embed_models(keys, llm_model):
    import random
    avail_key_list = []
    key_list = keys.split(',')

    if llm_model.startswith('text-embedding-'):
        for k in key_list:
            if is_openai_api_key(k): avail_key_list.append(k)

    if len(avail_key_list) == 0:
        raise RuntimeError(f"您提供的api-key不满足要求,不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源。")

    api_key = random.choice(avail_key_list) # 随机负载均衡

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Switch the request source/model in the top-left model menu so the model prefix matches the provider of the key you actually have (e.g. choose an azure-* model if you only have an Azure key).
  2. Check your API_KEY / AZURE_API_KEY / COHERE_API_KEY entries in config_private.py: strip quotes, spaces, and empty segments from the comma-separated list.
  3. Add at least one key of the matching provider: sk-... style for openai/gpt models, Azure deployment key for azure-* models, a Cohere key for cohere-* models.
  4. If you intended to use Azure OpenAI, configure AZURE_CFG_ARRAY with model names prefixed 'azure' so the key gets routed correctly (see toolbox.py read_single_conf_via_LLM).

Example fix

# before (config_private.py)
API_KEY = "0123456789abcdef0123456789abcdef"  # an Azure key
# model selected: gpt-4-turbo -> no sk- key found -> RuntimeError

# after: use an azure-prefixed model so the Azure key is picked
# in the model menu choose: azure-gpt-4-turbo
# or add an OpenAI key:
API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Defensive patterns

Strategy: validation

Validate before calling

from shared_utils.key_pattern_manager import (
    is_azure_api_key, is_cohere_api_key, is_openroute_api_key, is_openai_api_key,
)

def has_key_for_chat_model(keys: str, llm_model: str) -> bool:
    key_list = [k.strip() for k in keys.split(',') if k.strip()]
    if llm_model.startswith('azure-'):
        return any(is_azure_api_key(k) for k in key_list)
    if llm_model.startswith('cohere-'):
        return any(is_cohere_api_key(k) for k in key_list)
    if llm_model.startswith('openrouter-'):
        return any(is_openroute_api_key(k) for k in key_list)
    return any(is_openai_api_key(k) for k in key_list)

# before selecting:
# assert has_key_for_chat_model(API_KEY, llm_model), f'no usable key for {llm_model}'

Try / catch

try:
    api_key = select_api_key_for_chat_models(keys, llm_model)
except RuntimeError as e:
    # config error, not transient: report and abort, do not retry
    raise ConfigError(f'key/model mismatch for {llm_model}: {e}') from e

Prevention

When it happens

Trigger: Calling select_api_key_for_chat_models(keys, llm_model) where llm_model starts with 'azure-', 'cohere-', 'openrouter-', 'gpt-', 'claude-', etc. but no key in keys (split on ',') passes the corresponding is_*_api_key check. Typical concrete cases: selecting a cohere-* model while only OpenAI sk- keys are configured; pasting an Azure key (32-char hex) while the model is a plain gpt-* model that expects an sk- key; trailing whitespace or an empty entry from 'key1,,key2' so no detector matches.

Common situations: Wrong request source selected in the top-left model menu (openai vs azure vs claude vs cohere). Copying only the AZURE_API_KEY into API_KEY (or vice versa) in shared_utils/config.py or config_private.py. Mixing keys from multiple providers in one comma-separated string but choosing a model whose provider has no key present. Keys pasted with quotes/spaces so the regex detectors fail.

Related errors


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