{"record":{"id":"c56c01a8e0ad0658","repo":"binary-husky/gpt_academic","slug":"api-key-1-api-key","errorCode":null,"errorMessage":"你提供了错误的API_KEY。\n\n1. 临时解决方案：直接在输入区键入api_key，然后回车提交。\n\n2. 长效解决方案：在config.py中配置。","messagePattern":"你提供了错误的API_KEY。\n\n1\\. 临时解决方案：直接在输入区键入api_key，然后回车提交。\n\n2\\. 长效解决方案：在config\\.py中配置。","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"critical","filePath":"request_llms/bridge_chatgpt.py","lineNumber":456,"sourceCode":"        chatbot[-1] = (chatbot[-1][0], \"[Local Message] API key has been deactivated. OpenAI以账户失效为由, 拒绝服务.\" + openai_website)\n    elif \"bad forward key\" in error_msg:\n        chatbot[-1] = (chatbot[-1][0], \"[Local Message] Bad forward key.\")\n    elif \"Not enough point\" in error_msg:\n        chatbot[-1] = (chatbot[-1][0], \"[Local Message] Not enough point.\")\n    else:\n        from toolbox import regular_txt_to_markdown\n        tb_str = '```\\n' + trimmed_format_exc() + '```'\n        chatbot[-1] = (chatbot[-1][0], f\"[Local Message] 异常 \\n\\n{tb_str} \\n\\n{regular_txt_to_markdown(chunk_decoded)}\")\n    return chatbot, history\n\ndef generate_payload(inputs:str, llm_kwargs:dict, history:list, system_prompt:str, image_base64_array:list=[], has_multimodal_capacity:bool=False, stream:bool=True):\n    \"\"\"\n    整合所有信息，选择LLM模型，生成http请求，为发送请求做准备\n    \"\"\"\n    from request_llms.bridge_all import model_info\n\n    if not is_any_api_key(llm_kwargs['api_key']):\n        raise AssertionError(\"你提供了错误的API_KEY。\\n\\n1. 临时解决方案：直接在输入区键入api_key，然后回车提交。\\n\\n2. 长效解决方案：在config.py中配置。\")\n\n    if llm_kwargs['llm_model'].startswith('vllm-'):\n        api_key = 'no-api-key'\n    else:\n        api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])\n\n    headers = {\n        \"Content-Type\": \"application/json\",\n        \"Authorization\": f\"Bearer {api_key}\"\n    }\n    if API_ORG.startswith('org-'): headers.update({\"OpenAI-Organization\": API_ORG})\n    if llm_kwargs['llm_model'].startswith('azure-'):\n        headers.update({\"api-key\": api_key})\n        if llm_kwargs['llm_model'] in AZURE_CFG_ARRAY.keys():\n            azure_api_key_unshared = AZURE_CFG_ARRAY[llm_kwargs['llm_model']][\"AZURE_API_KEY\"]\n            headers.update({\"api-key\": azure_api_key_unshared})\n\n    if has_multimodal_capacity:","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_chatgpt.py#L438-L474","documentation":"AssertionError raised at the top of generate_payload in the main ChatGPT bridge: is_any_api_key(llm_kwargs['api_key']) returned False, meaning the key string is empty or contains no usable key (it checks for non-placeholder, non-'sk-'-missing values). The bridge builds Authorization: Bearer headers from this key, so it aborts before any request. The message offers both a quick fix (type the key in the chat box) and a durable one (config.py).","triggerScenarios":"API_KEY/API_KEY_EXPOSE left empty or as the default placeholder in config.py and config_private.py; user selected an OpenAI-family model but never configured a key; env var override not picked up because config was already loaded; key typed into the UI but the model branch reads the config value instead.","commonSituations":"Fresh install where only config.py (template) exists; user filled config_private.py but misspelled the variable name; docker deployments missing the mounted config; selecting gpt models before any key was ever provided.","solutions":["Temporary: type the API key directly into the gpt_academic input box and press Enter — it is picked up for the session.","Permanent: set API_KEY = \"sk-...\" (and if sharing, API_KEY_EXPOSE) in config_private.py at the repo root and restart.","Verify the variable spelling and that config_private.py is in the project root (it shadows config.py).","For vllm- models no key is needed — the code path assigns 'no-api-key' — so the error means you are on an OpenAI/Azure model that requires one."],"exampleFix":"# config_private.py\nAPI_KEY = \"sk-your-real-key-here\"\nAPI_KEY_EXPOSE = 0","handlingStrategy":"validation","validationCode":"from toolbox import is_any_api_key\nassert is_any_api_key(API_KEY), \"API_KEY empty/placeholder — set it in config_private.py before starting\"","typeGuard":null,"tryCatchPattern":"try:\n    payload = generate_payload(inputs, llm_kwargs, history, system_prompt)\nexcept AssertionError as e:\n    if 'API_KEY' in str(e):\n        prompt_user_for_key_in_chat()  # the documented temporary fix\n    else:\n        raise","preventionTips":["Validate keys at startup with is_any_api_key and fail fast with a clear message.","Keep config_private.py in the repo root and double-check variable names against config.py.","For docker installs, mount config_private.py into the container.","Remember vllm- models need no key; openai/azure models always do."],"tags":["openai","api-key","configuration","authentication"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}