binary-husky/gpt_academic · critical · ValueError

请配置 GEMINI_API_KEY。

Error message

请配置 GEMINI_API_KEY。

What it means

ValueError raised at the top of Gemini predict_no_ui_long_connection when get_conf("GEMINI_API_KEY") returns "" — the key was never set in config.py/config_private.py. The Gemini path builds signed Google AI (GenerativeLanguage) requests from this key, so the bridge aborts before constructing GoogleChatInit-dependent work. Configuration is the only fix; there is no in-chat fallback like the OpenAI bridges.

Source

Thrown at request_llms/bridge_google_gemini.py:22

# @Descr   :
import json
import re
import os
import time
from request_llms.com_google import GoogleChatInit
from toolbox import ChatBotWithCookies
from toolbox import get_conf, update_ui, update_ui_latest_msg, have_any_recent_upload_image_files, trimmed_format_exc, log_chat, encode_image

proxies, TIMEOUT_SECONDS, MAX_RETRY = get_conf('proxies', 'TIMEOUT_SECONDS', 'MAX_RETRY')
timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \
                  '网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。'


def predict_no_ui_long_connection(inputs:str, llm_kwargs:dict, history:list=[], sys_prompt:str="", observe_window:list=[],
                                  console_silence:bool=False):
    # 检查API_KEY
    if get_conf("GEMINI_API_KEY") == "":
        raise ValueError(f"请配置 GEMINI_API_KEY。")

    genai = GoogleChatInit(llm_kwargs)
    watch_dog_patience = 5  # 看门狗的耐心, 设置5秒即可
    gpt_replying_buffer = ''
    stream_response = genai.generate_chat(inputs, llm_kwargs, history, sys_prompt)
    for response in stream_response:
        results = response.decode()
        match = re.search(r'"text":\s*"((?:[^"\\]|\\.)*)"', results, flags=re.DOTALL)
        error_match = re.search(r'\"message\":\s*\"(.*?)\"', results, flags=re.DOTALL)
        if match:
            try:
                paraphrase = json.loads('{"text": "%s"}' % match.group(1))
            except:
                raise ValueError(f"解析GEMINI消息出错。")
            buffer = paraphrase['text']
            gpt_replying_buffer += buffer
            if len(observe_window) >= 1:
                observe_window[0] = gpt_replying_buffer

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Create a key at aistudio.google.com/app/apikey and set GEMINI_API_KEY = "AIza..." in config_private.py, then restart.
  2. Confirm config_private.py is in the project root so it overrides config.py.
  3. Verify the key works with a direct curl to the GenerativeLanguage API to rule out revocation/quota.
  4. If behind restricted networks, also configure proxies — the follow-up request needs reachability of generativelanguage.googleapis.com.

Example fix

# config_private.py
GEMINI_API_KEY = "AIzaSy-your-real-key"
Defensive patterns

Strategy: validation

Validate before calling

from toolbox import get_conf
assert get_conf('GEMINI_API_KEY') != '', "set GEMINI_API_KEY in config_private.py (create at aistudio.google.com/app/apikey)"

Try / catch

try:
    result = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)
except ValueError as e:
    if 'GEMINI_API_KEY' in str(e):
        disable_gemini_models_until_configured()
    else:
        raise

Prevention

When it happens

Trigger: Selecting a Gemini model while GEMINI_API_KEY is empty in config; config_private.py exists but omits GEMINI_API_KEY; key stored only in an env var the config loader doesn't read.

Common situations: Fresh installs trying google gemini models; users who configured OpenAI/Anthropic keys and assumed Gemini shares them; Google AI Studio key never generated.

Related errors


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