xtekky/gpt4free · error · ValueError

Invalid or disallowed media URL

Error message

Invalid or disallowed media URL

What it means

Error "Invalid or disallowed media URL" thrown in xtekky/gpt4free.

Source

Thrown at g4f/image/__init__.py:456

def to_bytes(image: ImageType) -> bytes:
    """
    Converts the given image to bytes.

    Args:
        image (ImageType): The image to convert.

    Returns:
        bytes: The image as bytes.
    """
    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

View on GitHub (pinned to 973504e177)

Solutions

  1. Use an http(s) URL pointing directly to an image.
  2. Remove disallowed schemes or private addresses from the URL.

When it happens

Trigger: Thrown at g4f/image/__init__.py:456 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/d91cb72b9be91b11. Report an issue: GitHub.