{"record":{"id":"7964b07a026d95d2","repo":"binary-husky/gpt_academic","slug":"error-7964b0","errorCode":null,"errorMessage":"对话错误","messagePattern":"对话错误","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"request_llms/bridge_google_gemini.py","lineNumber":118,"sourceCode":"        results = response.decode(\"utf-8\")    # 被这个解码给耍了。。\n        gpt_security_policy += results\n        match = re.search(r'\"text\":\\s*\"((?:[^\"\\\\]|\\\\.)*)\"', results, flags=re.DOTALL)\n        error_match = re.search(r'\\\"message\\\":\\s*\\\"(.*)\\\"', results, flags=re.DOTALL)\n        if match:\n            try:\n                paraphrase = json.loads('{\"text\": \"%s\"}' % match.group(1))\n            except:\n                raise ValueError(f\"解析GEMINI消息出错。\")\n            gpt_replying_buffer += paraphrase['text']    # 使用 json 解析库进行处理\n            chatbot[-1] = (inputs, gpt_replying_buffer)\n            history[-1] = gpt_replying_buffer\n            log_chat(llm_model=llm_kwargs[\"llm_model\"], input_str=inputs, output_str=gpt_replying_buffer)\n            yield from update_ui(chatbot=chatbot, history=history)\n        if error_match:\n            history = history[-2]  # 错误的不纳入对话\n            chatbot[-1] = (inputs, gpt_replying_buffer + f\"对话错误，请查看message\\n\\n```\\n{error_match.group(1)}\\n```\")\n            yield from update_ui(chatbot=chatbot, history=history)\n            raise RuntimeError('对话错误')\n    if not gpt_replying_buffer:\n        history = history[-2]  # 错误的不纳入对话\n        chatbot[-1] = (inputs, gpt_replying_buffer + f\"触发了Google的安全访问策略，没有回答\\n\\n```\\n{gpt_security_policy}\\n```\")\n        yield from update_ui(chatbot=chatbot, history=history)\n\n\nif __name__ == '__main__':\n    import sys\n    llm_kwargs = {'llm_model': 'gemini-pro'}\n    result = predict('Write long a story about a magic backpack.', llm_kwargs, llm_kwargs, [])\n    for i in result:\n        print(i)\n","sourceCodeStart":100,"sourceCodeEnd":131,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_google_gemini.py#L100-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"Free-tier quota exhausted mid-session, key rotated/expired, proxy injecting an error page that contains a \"message\" JSON field, prompt tripping safety policy.","solutions":["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).","Validate GEMINI_API_KEY and quota with a minimal non-streaming call before long sessions.","Add retry with exponential backoff for transient 429/503 messages.","If safety-related, restructure the prompt; do not retry unchanged."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    for chunk in predict(...):\n        yield chunk\nexcept RuntimeError as e:\n    if '对话错误' in str(e):\n        # chatbot already rendered error_match.group(1); surface it and stop\n        chatbot.append(('system', 'Gemini request failed, see message above.'))","preventionTips":["Check quota/key before long chat sessions","Handle safety refusures by rephrasing rather than retrying","Catch RuntimeError around predict and keep the chatbot history fix (history[-2]) intact"],"tags":["gemini","api-error","chatbot","quota"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}