xtekky/gpt4free · error · ValueError

Invalid image url. Expected bytes, str, or PIL Image.

Error message

Invalid image url. Expected bytes, str, or PIL Image.

What it means

Error "Invalid image url. Expected bytes, str, or PIL Image." thrown in xtekky/gpt4free.

Source

Thrown at g4f/image/__init__.py:475

            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):
        bytes_io = BytesIO()
        image.save(bytes_io, image.format)
        image.seek(0)
        return bytes_io.getvalue()
    elif isinstance(image, os.PathLike):
        return Path(image).read_bytes()
    elif isinstance(image, Path):
        return image.read_bytes()
    else:

View on GitHub (pinned to 973504e177)

Solutions

  1. Pass the image as bytes, a path/URL string, or a PIL Image object.

When it happens

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