binary-husky/gpt_academic · error · RuntimeError

没有配置BAIDU_CLOUD_API_KEY

Error message

没有配置BAIDU_CLOUD_API_KEY

What it means

RuntimeError from get_access_token in bridge_qianfan: BAIDU_CLOUD_API_KEY is empty. Together with the secret-key check it guards the Baidu OAuth token request; both must be non-empty before the aip.baidubce.com/oauth/2.0/token POST is made. This is a pure configuration error raised before any network activity.

Source

Thrown at request_llms/bridge_qianfan.py:40

            # Call the function and cache the result
            result = func(*args, **kwargs)
            cache[key] = (result, datetime.now())
            return result
        return wrapper
    return decorator

@cache_decorator(timeout=3600)
def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :return: access_token,或是None(如果错误)
    """
    # if (access_token_cache is None) or (time.time() - last_access_token_obtain_time > 3600):
    BAIDU_CLOUD_API_KEY, BAIDU_CLOUD_SECRET_KEY = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')

    if len(BAIDU_CLOUD_SECRET_KEY) == 0: raise RuntimeError("没有配置BAIDU_CLOUD_SECRET_KEY")
    if len(BAIDU_CLOUD_API_KEY) == 0: raise RuntimeError("没有配置BAIDU_CLOUD_API_KEY")

    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": BAIDU_CLOUD_API_KEY, "client_secret": BAIDU_CLOUD_SECRET_KEY}
    access_token_cache = str(requests.post(url, params=params).json().get("access_token"))
    return access_token_cache
    # else:
    #     return access_token_cache


def generate_message_payload(inputs, llm_kwargs, history, system_prompt):
    conversation_cnt = len(history) // 2
    if system_prompt == "": system_prompt = "Hello"
    messages = [{"role": "user", "content": system_prompt}]
    messages.append({"role": "assistant", "content": 'Certainly!'})
    if conversation_cnt:
        for index in range(0, 2*conversation_cnt, 2):
            what_i_have_asked = {}
            what_i_have_asked["role"] = "user"

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Set BAIDU_CLOUD_API_KEY in config_private.py to the Qianfan app's API Key
  2. Confirm BAIDU_CLOUD_SECRET_KEY is also set (the sibling check will fire next otherwise)
  3. Prefer config_private.py so updates to config.py do not clobber your keys

Example fix

# config_private.py
# before
BAIDU_CLOUD_API_KEY = ""

# after
BAIDU_CLOUD_API_KEY = "your-qianfan-api-key"
Defensive patterns

Strategy: validation

Validate before calling

api_key, secret = get_conf('BAIDU_CLOUD_API_KEY', 'BAIDU_CLOUD_SECRET_KEY')
assert len(api_key) > 0, 'BAIDU_CLOUD_API_KEY empty - get it from Qianfan console > 应用详情'

Type guard

def baidu_api_key_present() -> bool:
    return len(get_conf('BAIDU_CLOUD_API_KEY', '')[0] if isinstance(get_conf('BAIDU_CLOUD_API_KEY'), tuple) else get_conf('BAIDU_CLOUD_API_KEY')) > 0

Try / catch

try:
    token = get_access_token()
except RuntimeError as e:
    if 'BAIDU_CLOUD_API_KEY' in str(e):
        raise SystemExit('Set BAIDU_CLOUD_API_KEY in config_private.py') from e

Prevention

When it happens

Trigger: Qianfan model selected with BAIDU_CLOUD_API_KEY unset; only the secret key configured; wrong config file edited (config.py instead of config_private.py being overwritten by updates).

Common situations: Partial credential setup; editing the wrong config file; CI without Baidu env vars running qianfan tests.

Related errors


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