xtekky/gpt4free · error · ValueError

Unsupported media type: {content_type}

Error message

Unsupported media type: {content_type}

What it means

Thrown by g4f.image.copy_media() when the resolved content_type (from a dict's 'mimeType', the response's content-type header, or the content_type parameter) has no entry in MEDIA_TYPE_MAP, the extension lookup built from EXTENSIONS_MAP (plus audio/webm). The function refuses to save a file whose extension it cannot determine.

Source

Thrown at g4f/image/copy_images.py:90

    transcript: str = None,
    content_type: str = None,
) -> AsyncIterator:
    """Save media from response to local file and return a response object."""
    if isinstance(response, dict):
        content_type = response.get("mimeType", content_type or "audio/mpeg")
        transcript = response.get("transcript")
        response = response.get("data")
    elif hasattr(response, "headers"):
        content_type = response.headers.get("content-type", content_type)
    elif not content_type:
        raise ValueError("Response must be a dict or have headers")

    if isinstance(response, str):
        response = base64.b64decode(response)

    extension = MEDIA_TYPE_MAP.get(content_type)
    if extension is None:
        raise ValueError(f"Unsupported media type: {content_type}")

    filename = get_filename(tags, prompt, f".{extension}", prompt)
    if hasattr(response, "headers"):
        filename = update_filename(response, filename)
    target_path = os.path.join(get_media_dir(), filename)
    ensure_media_dir()

    with open(target_path, "wb") as f:
        if isinstance(response, bytes):
            f.write(response)
        else:
            if hasattr(response, "iter_content"):
                iter_response = response.iter_content()
            else:
                iter_response = response.content.iter_any()
            async for chunk in iter_response:
                f.write(chunk)

View on GitHub (pinned to 973504e177)

Solutions

  1. Normalize the type before calling: strip parameters via content_type.split(';')[0].strip() and check membership in g4f.image.MEDIA_TYPE_MAP.
  2. Use one of the mapped types: audio/mpeg, audio/wav, audio/ogg, audio/webm, image/png, image/jpeg, image/webp, video/mp4, etc. (keys of EXTENSIONS_MAP reversed).
  3. If the format is genuinely needed, convert the media to a mapped container first (e.g. pydub for audio).

Example fix

// before
await copy_media(data, content_type=resp.headers['content-type'])  # 'audio/mpeg; charset=binary'

// after
mime = resp.headers['content-type'].split(';')[0].strip()
await copy_media(data, content_type=mime)
Defensive patterns

Strategy: validation

Validate before calling

from g4f.image.copy_images import MEDIA_TYPE_MAP

def supported_content_type(ct: str) -> bool:
    return ct is not None and ct.split(";")[0].strip() in MEDIA_TYPE_MAP

Try / catch

try:
    await copy_media(data, content_type=ct, prompt=p, tags=t)
except ValueError as e:
    if "Unsupported media type" in str(e):
        ct = "application/octet-stream"  # only if copy_media callers tolerate magic-detection paths
    raise

Prevention

When it happens

Trigger: content_type='audio/mp4' or 'video/x-matroska' or an image type with parameters like 'image/png; charset=utf-8' — any string not exactly a key in g4f.image.MEDIA_TYPE_MAP.

Common situations: Servers sending unusual or parameterized Content-Type headers; providers returning mimeTypes like 'audio/aac' or 'audio/ogg' that the map does not include; copy-pasting a type with trailing whitespace or quotes.

Related errors


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