{"record":{"id":"3957852c47e6e502","repo":"binary-husky/gpt_academic","slug":"baidu-cloud-secret-key","errorCode":null,"errorMessage":"没有配置BAIDU_CLOUD_SECRET_KEY","messagePattern":"没有配置BAIDU_CLOUD_SECRET_KEY","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"request_llms/bridge_qianfan.py","lineNumber":39,"sourceCode":"                    return result\n\n            # Call the function and cache the result\n            result = func(*args, **kwargs)\n            cache[key] = (result, datetime.now())\n            return result\n        return wrapper\n    return decorator\n\n@cache_decorator(timeout=3600)\ndef get_access_token():\n    \"\"\"\n    使用 AK，SK 生成鉴权签名（Access Token）\n    :return: access_token，或是None(如果错误)\n    \"\"\"\n    # if (access_token_cache is None) or (time.time() - last_access_token_obtain_time > 3600):\n    BAIDU_CLOUD_API_KEY, BAIDU_CLOUD_SECRET_KEY = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')\n\n    if len(BAIDU_CLOUD_SECRET_KEY) == 0: raise RuntimeError(\"没有配置BAIDU_CLOUD_SECRET_KEY\")\n    if len(BAIDU_CLOUD_API_KEY) == 0: raise RuntimeError(\"没有配置BAIDU_CLOUD_API_KEY\")\n\n    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n    params = {\"grant_type\": \"client_credentials\", \"client_id\": BAIDU_CLOUD_API_KEY, \"client_secret\": BAIDU_CLOUD_SECRET_KEY}\n    access_token_cache = str(requests.post(url, params=params).json().get(\"access_token\"))\n    return access_token_cache\n    # else:\n    #     return access_token_cache\n\n\ndef generate_message_payload(inputs, llm_kwargs, history, system_prompt):\n    conversation_cnt = len(history) // 2\n    if system_prompt == \"\": system_prompt = \"Hello\"\n    messages = [{\"role\": \"user\", \"content\": system_prompt}]\n    messages.append({\"role\": \"assistant\", \"content\": 'Certainly!'})\n    if conversation_cnt:\n        for index in range(0, 2*conversation_cnt, 2):\n            what_i_have_asked = {}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_qianfan.py#L21-L57","documentation":"RuntimeError from get_access_token in bridge_qianfan: the BAIDU_CLOUD_SECRET_KEY read via get_conf is an empty string, so the OAuth client_credentials flow cannot be attempted. It fails before any network call; the companion check for BAIDU_CLOUD_API_KEY sits directly below. get_access_token is cached (timeout=3600), but a raise is not cached so fixing config takes effect on next call after restart.","triggerScenarios":"Using a qianfan (Baidu Qianfan/ERNIE) model without setting BAIDU_CLOUD_SECRET_KEY in config_private.py; key defined under the wrong variable name; Docker/env deployment missing the variable.","commonSituations":"Selecting an ERNIE Bot model in model dropdown before configuring Baidu credentials; copying config template and skipping Baidu section.","solutions":["Set BAIDU_CLOUD_SECRET_KEY in config_private.py (from Baidu Qianfan console > application credentials)","Also set BAIDU_CLOUD_API_KEY - both are required","Restart the process after config changes","If you did not intend to use Qianfan, switch the model back to an OpenAI-compatible one"],"exampleFix":"# config_private.py\n# before\nBAIDU_CLOUD_SECRET_KEY = \"\"\n\n# after\nBAIDU_CLOUD_SECRET_KEY = \"your-qianfan-secret-key\"","handlingStrategy":"validation","validationCode":"from shared_utils.config_loader import get_conf\napi_key, secret = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')\nassert api_key and secret, 'Baidu Qianfan credentials missing - set BAIDU_CLOUD_API_KEY and BAIDU_CLOUD_SECRET_KEY'","typeGuard":"def qianfan_configured() -> bool:\n    api_key, secret = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')\n    return len(api_key) > 0 and len(secret) > 0","tryCatchPattern":"try:\n    token = get_access_token()\nexcept RuntimeError as e:\n    if 'BAIDU_CLOUD_SECRET_KEY' in str(e):\n        raise SystemExit('Set BAIDU_CLOUD_SECRET_KEY in config_private.py') from e","preventionTips":["Gate qianfan model selection on both Baidu keys being non-empty","Add credential checks to a startup lint so misconfiguration surfaces at boot","Store keys in config_private.py, never config.py which updates overwrite"],"tags":["qianfan","baidu","configuration","credentials","api-key"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}