binary-husky/gpt_academic · error · ValueError
Invalid image URL format.
Error message
Invalid image URL format.
What it means
gen_image_banana() supports only inline data URIs: it requires ';base64,' in image_url. If the provider returns an ordinary https URL or a malformed URL, this ValueError is raised. Like error 7, it is caught immediately and normally surfaces wrapped as error 9.
Source
Thrown at crazy_functions/Image_Generate.py:273
yield from update_ui_latest_msg(lastmsg=f"Generate Failed\n\n{generated_content}\n\nStatus Code: {response.status_code}", chatbot=chatbot, history=history, delay=0)
return
if image_url is None:
raise RuntimeError("No image URL found in the response.")
logger.info(f'Generated image.')
yield from update_ui_latest_msg(lastmsg=f"Downloading image", chatbot=chatbot, history=history, delay=0)
if ';base64,' in image_url:
base64_string = image_url.split('base64,')[-1]
image_data = base64.b64decode(base64_string)
file_path = f'{get_log_folder()}/image_gen/'
os.makedirs(file_path, exist_ok=True)
file_name = 'Image' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.png'
fp = file_path+file_name
with open(fp, 'wb+') as f: f.write(image_data)
else:
raise ValueError("Invalid image URL format.")
return image_url, fp
except Exception as e:
yield from update_ui_latest_msg(lastmsg=f"Generate failed, please try again later.", chatbot=chatbot, history=history, delay=0)
raise RuntimeError(f"Failed to generate image, please try again later: {str(e)}")
@CatchException
def 图片生成_NanoBanana(prompt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):
history = [] # 清空历史,以免输入溢出View on GitHub (pinned to d6bde0fa54)
Solutions
- Log image_url to see whether it is an https URL or malformed data.
- Add an else branch that downloads the remote URL, as gen_image() already does.
- Configure the provider/relay to return base64 data URIs if local download is undesirable.
- Validate the URL scheme before decoding base64.
- Preserve the original ValueError in the outer error message.
Example fix
# before
if ';base64,' in image_url:
...
else:
raise ValueError("Invalid image URL format.")
# after
if image_url.startswith(("http://", "https://")):
r = requests.get(image_url, proxies=proxies, timeout=120)
r.raise_for_status()
image_data = r.content
elif ";base64," in image_url:
image_data = base64.b64decode(image_url.split("base64,", 1)[1])
else:
raise ValueError(f"Invalid image URL format: {image_url[:100]}")
Defensive patterns
Strategy: validation
Validate before calling
from urllib.parse import urlparse
parsed = urlparse(image_url or "")
is_data_uri = (image_url or "").startswith("data:") and ";base64," in image_url
is_http_url = parsed.scheme in {"http", "https"} and bool(parsed.netloc)
assert is_data_uri or is_http_url
Type guard
def classify_image_url(url) -> str:
if not isinstance(url, str) or not url:
return "invalid"
if url.startswith("data:") and ";base64," in url:
return "base64"
if urlparse(url).scheme in {"http", "https"} and urlparse(url).netloc:
return "remote"
return "invalid"
Try / catch
try:
image_data = decode_or_download_image(image_url)
except ValueError as e:
report_unsupported_image_url(image_url)
raise
Prevention
- Do not assume every image API returns inline base64.
- Support both data URIs and temporary https URLs.
- Validate URL scheme before base64 decoding.
- Log a truncated URL for schema debugging.
When it happens
Trigger: The image endpoint returns https://.../image.png instead of data:image/png;base64,..., or the nested image_url.url field has an unexpected format.
Common situations: Using an OpenAI-compatible relay that returns temporary download URLs; a NanoBanana proxy configured not to inline base64; partial URL truncation; provider schema changes.
Related errors
- response.content.decode()
- No image URL found in the response.
- Failed to generate image, please try again later: {str(e)}
- Invalid URL: {url}
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/a6bc9f12361740d9.
Report an issue: GitHub.