{"record":{"id":"715646770e2bd948","repo":"binary-husky/gpt_academic","slug":"failed-to-generate-image-please-try-again-later","errorCode":null,"errorMessage":"Failed to generate image, please try again later: {str(e)}","messagePattern":"Failed to generate image, please try again later: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"crazy_functions/Image_Generate.py","lineNumber":279,"sourceCode":"        logger.info(f'Generated image.')\n        yield from update_ui_latest_msg(lastmsg=f\"Downloading image\", chatbot=chatbot, history=history, delay=0)\n\n        if ';base64,' in image_url:\n            base64_string = image_url.split('base64,')[-1]\n            image_data = base64.b64decode(base64_string)\n            file_path = f'{get_log_folder()}/image_gen/'\n            os.makedirs(file_path, exist_ok=True)\n            file_name = 'Image' + time.strftime(\"%Y-%m-%d-%H-%M-%S\", time.localtime()) + '.png'\n            fp = file_path+file_name\n            with open(fp, 'wb+') as f: f.write(image_data)\n        else:\n            raise ValueError(\"Invalid image URL format.\")\n\n        return image_url, fp\n\n    except Exception as e:\n        yield from update_ui_latest_msg(lastmsg=f\"Generate failed, please try again later.\", chatbot=chatbot, history=history, delay=0)\n        raise RuntimeError(f\"Failed to generate image, please try again later: {str(e)}\")\n\n\n\n\n\n\n\n\n\n@CatchException\ndef 图片生成_NanoBanana(prompt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):\n    history = []    # 清空历史,以免输入溢出\n\n    if prompt.strip() == \"\":\n        chatbot.append((prompt, \"[Local Message] 图像生成提示为空白\"))\n        yield from update_ui(chatbot=chatbot, history=history)\n        return\n    chatbot.append((","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/Image_Generate.py#L261-L297","documentation":"This is the catch-all wrapper around gen_image_banana(). Every exception in the try block is shown as a generic failure message and re-raised as RuntimeError with only str(e). It hides whether the real failure was error 7, error 8, requests.post, response.json(), base64 decoding, or writing the output file.","triggerScenarios":"Any network/request exception; missing or malformed JSON; missing choices/message/images/image_url fields; a non-base64 URL; invalid resolution/aspect ratio; quota/authentication failure; inability to create gpt_log/image_gen or write the PNG.","commonSituations":"Wrong GEMINI_BASE_URL/GEMINI_API_KEY or ONE_API_URL/ONE_API_KEY; model not enabled for images; provider returns https rather than base64; disk permission issues; transient network failure.","solutions":["Capture the full exception chain and log response details instead of relying on the generic message.","Verify GEMINI/ONE_API configuration and test the endpoint with the same payload.","Handle both https and base64 image URLs.","Retry only transient network/5xx failures with backoff.","Ensure get_log_folder()/image_gen is writable."],"exampleFix":"# before\nexcept Exception as e:\n    yield from update_ui_latest_msg(...)\n    raise RuntimeError(f\"Failed to generate image, please try again later: {str(e)}\")\n\n# after\nexcept Exception as e:\n    logger.exception(\"NanoBanana image generation failed\")\n    yield from update_ui_latest_msg(...)\n    raise RuntimeError(\n        f\"Failed to generate image: {type(e).__name__}: {e}\"\n    ) from e\n","handlingStrategy":"try-catch","validationCode":"api_key = get_conf(\"GEMINI_API_KEY\") if not get_conf(\"REROUTE_ALL_TO_ONE_API\") else get_conf(\"ONE_API_KEY\")\nif not api_key:\n    raise ValueError(\"Image API key is not configured\")\nif not get_conf(\"REROUTE_ALL_TO_ONE_API\") and not get_conf(\"GEMINI_BASE_URL\"):\n    raise ValueError(\"GEMINI_BASE_URL is not configured\")\n","typeGuard":null,"tryCatchPattern":"try:\n    yield from gen_image_banana(...)\nexcept RuntimeError as e:\n    logger.exception(\"Image plugin failed\")\n    chatbot.append([\"图像生成失败\", str(e)])\n    yield from update_ui(chatbot=chatbot, history=history)\n","preventionTips":["Treat the generic wrapper as a symptom and inspect the chained exception.","Smoke-test the image endpoint and key before a long session.","Make the log folder writable.","Retry transient network errors separately from schema errors."],"tags":["image-generation","error-handling","wrapper","api-response"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}