binary-husky/gpt_academic · critical · RuntimeError

没有设置ANTHROPIC_API_KEY选项

Error message

没有设置ANTHROPIC_API_KEY选项

What it means

RuntimeError raised at the start of Claude predict_no_ui_long_connection when the module-level ANTHROPIC_API_KEY (loaded from config at import time) is an empty string. The bridge needs the key to build the x-api-key headers in generate_payload, so it refuses before issuing the POST to the Anthropic messages endpoint. Unlike OpenAI keys, there is no type-it-in-the-chat-box fallback here — configuration is the only route.

Source

Thrown at request_llms/bridge_claude.py:88


def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_silence=False):
    """
    发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。
    inputs:
        是本次问询的输入
    sys_prompt:
        系统静默prompt
    llm_kwargs:
        chatGPT的内部调优参数
    history:
        是之前的对话列表
    observe_window = None:
        用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗
    """
    watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可
    if len(ANTHROPIC_API_KEY) == 0:
        raise RuntimeError("没有设置ANTHROPIC_API_KEY选项")
    if inputs == "":     inputs = "空空如也的输入栏"
    headers, message = generate_payload(inputs, llm_kwargs, history, sys_prompt, image_paths=None)
    retry = 0


    while True:
        try:
            # make a POST request to the API endpoint, stream=False
            from .bridge_all import model_info
            endpoint = model_info[llm_kwargs['llm_model']]['endpoint']
            response = requests.post(endpoint, headers=headers, json=message,
                                     proxies=proxies, stream=True, timeout=TIMEOUT_SECONDS);break
        except requests.exceptions.ReadTimeout as e:
            retry += 1
            traceback.print_exc()
            if retry > MAX_RETRY: raise TimeoutError
            if MAX_RETRY!=0: logger.error(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
    stream_response = response.iter_lines()

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Set ANTHROPIC_API_KEY = "sk-ant-..." in config_private.py at the repo root and restart gpt_academic.
  2. Verify config_private.py is in the project root so it shadows config.py and its value is actually loaded.
  3. Confirm the key is active in the Anthropic Console (console.anthropic.com) and has credit.
  4. If behind a proxy, also check the anthropic endpoint reachability so the subsequent request doesn't fail on network.

Example fix

# config_private.py
ANTHROPIC_API_KEY = "sk-ant-api03-your-real-key"
Defensive patterns

Strategy: validation

Validate before calling

from toolbox import get_conf
assert get_conf('ANTHROPIC_API_KEY') != '', "set ANTHROPIC_API_KEY in config_private.py before using Claude models"

Try / catch

try:
    result = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)
except RuntimeError as e:
    if 'ANTHROPIC_API_KEY' in str(e):
        block_claude_models_until_configured()
    else:
        raise

Prevention

When it happens

Trigger: ANTHROPIC_API_KEY missing or left as '' in config.py/config_private.py while selecting a Claude model; config_private.py created but the variable omitted; environment-specific config not loaded because the file is in the wrong location.

Common situations: Fresh installs trying the newly added Claude bridge without editing config; users who configured only OpenAI keys; docker deployments where config_private.py isn't mounted.

Related errors


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