xtekky/gpt4free · error · ValueError

Invalid or unsafe image url

Error message

Invalid or unsafe image url

What it means

Error "Invalid or unsafe image url" thrown in xtekky/gpt4free.

Source

Thrown at g4f/image/__init__.py:466

    if isinstance(image, bytes):
        return image
    elif isinstance(image, str):
        if image.startswith("data:"):
            is_data_uri_an_image(image)
            return extract_data_uri(image)
        elif image.startswith("http://") or image.startswith("https://"):
            if not is_safe_url(image):
                raise ValueError("Invalid or disallowed media URL")
            path: str = urlparse(image).path
            if path.startswith("/files/"):
                path = get_bucket_dir(*path.split("/")[2:])
                if os.path.exists(path):
                    return Path(path).read_bytes()
                else:
                    raise FileNotFoundError(f"File not found: {path}")
            else:
                if not is_safe_url(image):
                    raise ValueError("Invalid or unsafe image url")
                resp = requests.get(
                    image,
                    headers={
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0",
                    },
                )
                if resp.ok and is_accepted_format(resp.content):
                    return resp.content
                raise ValueError(
                    "Invalid image url. Expected bytes, str, or PIL Image."
                )
        elif os.path.exists(image):
            return Path(image).read_bytes()
        else:
            raise ValueError(
                f"Invalid image format or file not found. Expected bytes, str, or PIL Image. Got: {image[:100]}"
            )
    elif isinstance(image, Image.Image):

View on GitHub (pinned to 973504e177)

Solutions

  1. Use a valid http(s) image URL; local paths and unsafe URLs are rejected.

When it happens

Trigger: Thrown at g4f/image/__init__.py:466 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/8c4831c9c36a982f. Report an issue: GitHub.