Skip to content

_to_openai_image_url raises TypeError for local files without extension #1336

@kingofpowers

Description

@kingofpowers

AgentScope Version: 1.0.16dev

Description:

When a local image file has no extension (e.g., /app/downloads/download), _to_openai_image_url raises:

TypeError: "/app/downloads/download" should end with (.png, .jpg, .jpeg, .gif, .webp).

This happens when:

  1. Downloading images from URLs without file extension in the path (e.g., QQ multimedia URLs like https://multimedia.nt.qq.com.cn/download?appid=...&rkey=...)
  2. The downloaded file has no extension

Reproduction:

# Download an image from a URL without extension
import urllib.request
urllib.request.urlretrieve("https://example.com/download?token=xxx", "/tmp/download")
# File saved as /tmp/download (no extension)

# Then try to use it in a message
# _to_openai_image_url("/tmp/download") raises TypeError

Suggested Fix:

Use imghdr to detect file type when extension is missing:

import imghdr

# In _to_openai_image_url, when local file has no extension:
detected_type = imghdr.what(url)
if detected_type in ("png", "jpeg", "gif", "webp"):
    with open(url, "rb") as f:
        base64_image = base64.b64encode(f.read()).decode("utf-8")
    mime_type = f"image/{detected_type}"
    return f"data:{mime_type};base64,{base64_image}"

Related: PR #1066 (URLs with query parameters), PR #1284 (file:// URI handling)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions