binary-husky/gpt_academic · error · RuntimeError
程序终止。
Error message
程序终止。
What it means
Raised inside the Gemini streaming loop (predict_no_ui_long_connection) by a watchdog: if the time stored in observe_window[1] is more than watch_dog_patience seconds (5s) behind time.time(), the stream is considered stalled and the generator aborts with RuntimeError('程序终止。'). It is a client-side liveness check on the SSE stream, not a Gemini API error.
Source
Thrown at request_llms/bridge_google_gemini.py:42
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
if len(observe_window) >= 2:
if (time.time() - observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。")
if error_match:
raise RuntimeError(f'{gpt_replying_buffer} 对话错误')
return gpt_replying_buffer
def make_media_input(inputs, image_paths):
image_base64_array = []
for image_path in image_paths:
path = os.path.abspath(image_path)
inputs = inputs + f'<br/><br/><div align="center"><img src="file={path}"></div>'
base64 = encode_image(path)
image_base64_array.append(base64)
return inputs, image_base64_array
def predict(inputs:str, llm_kwargs:dict, plugin_kwargs:dict, chatbot:ChatBotWithCookies,
history:list=[], system_prompt:str='', stream:bool=True, additional_fn:str=None):
from .bridge_all import model_info
View on GitHub (pinned to d6bde0fa54)
Solutions
- Check network/proxy reachability to the Gemini endpoint (curl a minimal streaming request) and retry once.
- Raise watch_dog_patience (e.g. to 30s) or have the caller update observe_window[1] on each yield so only true stalls trip the dog.
- If the stream legitimately produces no text for long periods (safety refusal), handle the empty-buffer path at the end of the loop instead of relying on the watchdog.
- Upgrade/patch the bridge to use the official google-generativeai SDK instead of regex-scraping raw SSE bytes.
Example fix
// before
watch_dog_patience = 5
if (time.time() - observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。")
# after
watch_dog_patience = 30
if (time.time() - observe_window[1]) > watch_dog_patience:
raise RuntimeError("Gemini stream stalled for >30s; check proxy/network.") Defensive patterns
Strategy: retry
Validate before calling
import time
start = time.time()
# caller refreshes window[1] each iteration
window = ['', time.time()]
try:
out = predict_no_ui_long_connection(..., observe_window=window)
except RuntimeError as e:
if '程序终止' in str(e) and time.time()-start < 120:
window[1] = time.time(); retry = True # one retry with refreshed watchdog Try / catch
try:
result = predict_no_ui_long_connection(...)
except RuntimeError as e:
if '程序终止' in str(e):
# watchdog stall: refresh observe_window[1] and retry once
... Prevention
- Pass a 2+ element observe_window and refresh window[1] on every poll
- Keep watch_dog_patience >= worst-case Gemini silence (30s)
- Test proxy reachability to generativelanguage.googleapis.com before long jobs
When it happens
Trigger: Calling predict_no_ui_long_connection with an observe_window list of length >= 2 where observe_window[1] was set to a start timestamp, and no successfully decoded '"text":' chunk arrives within 5 seconds (slow network, proxy stall, or the API stopped sending deltas).
Common situations: Flaky proxy/VPN to generativelanguage.googleapis.com, Gemini rate-limiting or hanging the connection, large prompt causing long silent thinking time, or the caller forgetting to refresh observe_window[1] between chunks.
Related errors
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/4d72091315a10e47.
Report an issue: GitHub.