binary-husky/gpt_academic · error · RuntimeError

对话错误

Error message

对话错误

What it means

Raised at the end of the Gemini predict() generator when error_match found a '"message":' field in the streamed payload. Before raising, the code pops the failed exchange from history (history = history[-2]), renders the error into the chatbot UI with the captured message in a code fence, and then raises RuntimeError('对话错误') to abort the request.

Source

Thrown at request_llms/bridge_google_gemini.py:118

        results = response.decode("utf-8")    # 被这个解码给耍了。。
        gpt_security_policy += results
        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消息出错。")
            gpt_replying_buffer += paraphrase['text']    # 使用 json 解析库进行处理
            chatbot[-1] = (inputs, gpt_replying_buffer)
            history[-1] = gpt_replying_buffer
            log_chat(llm_model=llm_kwargs["llm_model"], input_str=inputs, output_str=gpt_replying_buffer)
            yield from update_ui(chatbot=chatbot, history=history)
        if error_match:
            history = history[-2]  # 错误的不纳入对话
            chatbot[-1] = (inputs, gpt_replying_buffer + f"对话错误,请查看message\n\n```\n{error_match.group(1)}\n```")
            yield from update_ui(chatbot=chatbot, history=history)
            raise RuntimeError('对话错误')
    if not gpt_replying_buffer:
        history = history[-2]  # 错误的不纳入对话
        chatbot[-1] = (inputs, gpt_replying_buffer + f"触发了Google的安全访问策略,没有回答\n\n```\n{gpt_security_policy}\n```")
        yield from update_ui(chatbot=chatbot, history=history)


if __name__ == '__main__':
    import sys
    llm_kwargs = {'llm_model': 'gemini-pro'}
    result = predict('Write long a story about a magic backpack.', llm_kwargs, llm_kwargs, [])
    for i in result:
        print(i)

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Read the rendered message in the chatbot pane / logs — it contains error_match.group(1), the actual API reason; fix that (key, quota, safety, request size).
  2. Validate GEMINI_API_KEY and quota with a minimal non-streaming call before long sessions.
  3. Add retry with exponential backoff for transient 429/503 messages.
  4. If safety-related, restructure the prompt; do not retry unchanged.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    for chunk in predict(...):
        yield chunk
except RuntimeError as e:
    if '对话错误' in str(e):
        # chatbot already rendered error_match.group(1); surface it and stop
        chatbot.append(('system', 'Gemini request failed, see message above.'))

Prevention

When it happens

Trigger: Any Gemini API error object present in the stream during a UI predict() call: invalid API key, 429 rate limit, safety refusal-as-error, or request too large — after the chatbot was updated with the error details.

Common situations: Free-tier quota exhausted mid-session, key rotated/expired, proxy injecting an error page that contains a "message" JSON field, prompt tripping safety policy.

Related errors


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