{"record":{"id":"0a063ba303372652","repo":"binary-husky/gpt_academic","slug":"api-key-llm-model-api-key","errorCode":null,"errorMessage":"您提供的api-key不满足要求，不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源（左上角更换模型菜单中可切换openai,azure,claude,cohere等请求源）。","messagePattern":"您提供的api-key不满足要求，不包含任何可用于(.+?)的api-key。您可能选择了错误的模型或请求源（左上角更换模型菜单中可切换openai,azure,claude,cohere等请求源）。","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"shared_utils/key_pattern_manager.py","lineNumber":118,"sourceCode":"\n    if llm_model.startswith('api2d-'):\n        for k in key_list:\n            if is_api2d_key(k): avail_key_list.append(k)\n\n    if llm_model.startswith('azure-'):\n        for k in key_list:\n            if is_azure_api_key(k): avail_key_list.append(k)\n\n    if llm_model.startswith('cohere-'):\n        for k in key_list:\n            if is_cohere_api_key(k): avail_key_list.append(k)\n\n    if llm_model.startswith('openrouter-'):\n        for k in key_list:\n            if is_openroute_api_key(k): avail_key_list.append(k)\n\n    if len(avail_key_list) == 0:\n        raise RuntimeError(f\"您提供的api-key不满足要求，不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源（左上角更换模型菜单中可切换openai,azure,claude,cohere等请求源）。\")\n\n    api_key = random.choice(avail_key_list) # 随机负载均衡\n    return api_key\n\n\ndef select_api_key_for_embed_models(keys, llm_model):\n    import random\n    avail_key_list = []\n    key_list = keys.split(',')\n\n    if llm_model.startswith('text-embedding-'):\n        for k in key_list:\n            if is_openai_api_key(k): avail_key_list.append(k)\n\n    if len(avail_key_list) == 0:\n        raise RuntimeError(f\"您提供的api-key不满足要求，不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源。\")\n\n    api_key = random.choice(avail_key_list) # 随机负载均衡","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/shared_utils/key_pattern_manager.py#L100-L136","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","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.","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.","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)."],"exampleFix":"# before (config_private.py)\nAPI_KEY = \"0123456789abcdef0123456789abcdef\"  # an Azure key\n# model selected: gpt-4-turbo -> no sk- key found -> RuntimeError\n\n# after: use an azure-prefixed model so the Azure key is picked\n# in the model menu choose: azure-gpt-4-turbo\n# or add an OpenAI key:\nAPI_KEY = \"sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"","handlingStrategy":"validation","validationCode":"from shared_utils.key_pattern_manager import (\n    is_azure_api_key, is_cohere_api_key, is_openroute_api_key, is_openai_api_key,\n)\n\ndef has_key_for_chat_model(keys: str, llm_model: str) -> bool:\n    key_list = [k.strip() for k in keys.split(',') if k.strip()]\n    if llm_model.startswith('azure-'):\n        return any(is_azure_api_key(k) for k in key_list)\n    if llm_model.startswith('cohere-'):\n        return any(is_cohere_api_key(k) for k in key_list)\n    if llm_model.startswith('openrouter-'):\n        return any(is_openroute_api_key(k) for k in key_list)\n    return any(is_openai_api_key(k) for k in key_list)\n\n# before selecting:\n# assert has_key_for_chat_model(API_KEY, llm_model), f'no usable key for {llm_model}'","typeGuard":null,"tryCatchPattern":"try:\n    api_key = select_api_key_for_chat_models(keys, llm_model)\nexcept RuntimeError as e:\n    # config error, not transient: report and abort, do not retry\n    raise ConfigError(f'key/model mismatch for {llm_model}: {e}') from e","preventionTips":["Keep one provider's keys per config slot and make the selected model prefix (azure-/cohere-/openrouter-/gpt-) match that provider.","Normalize the key string at load time: strip whitespace, drop empty comma segments.","Run has_key_for_chat_model once at startup after loading config so bad key/model combos fail fast with a clear message."],"tags":["api-key","configuration","model-selection","provider-mismatch"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}