xtekky/gpt4free · error · ValueError
Invalid input audio
Error message
Invalid input audio
What it means
Thrown by g4f.image.to_input_audio() when audio is passed as a non-str object (bytes, Path, file-like) together with a filename, but get_extension(filename) returns None — i.e. the filename has no dot or its extension is not in EXTENSIONS_MAP. The library needs a known audio extension to fill the 'format' field of the input-audio dict, so it refuses the call.
Source
Thrown at g4f/image/__init__.py:514
except (AttributeError, io.UnsupportedOperation):
pass
return image.read()
def to_data_uri(image: ImageType, filename: str = None) -> str:
if not isinstance(image, str):
data = to_bytes(image)
data_base64 = base64.b64encode(data).decode()
return f"data:{is_data_an_media(data, filename)};base64,{data_base64}"
return image
def to_input_audio(audio: ImageType, filename: str = None) -> str:
if not isinstance(audio, str):
if filename is not None:
format = get_extension(filename)
if format is None:
raise ValueError("Invalid input audio")
return {
"data": base64.b64encode(to_bytes(audio)).decode(),
"format": format,
}
raise ValueError("Invalid input audio")
audio = re.match(r"^data:audio/(\w+);base64,(.+?)", audio)
if audio:
return {"data": audio.group(2), "format": audio.group(1).replace("mpeg", "mp3")}
raise ValueError("Invalid input audio")
def use_aspect_ratio(extra_body: dict, aspect_ratio: str) -> Image:
extra_body = {key: value for key, value in extra_body.items() if value is not None}
if extra_body.get("width") is None or extra_body.get("height") is None:
width, height = get_width_height(
aspect_ratio, extra_body.get("width"), extra_body.get("height")
)
extra_body = {"width": width, "height": height, **extra_body}View on GitHub (pinned to 973504e177)
Solutions
- Give the filename a recognized audio extension: use .mp3, .wav, .ogg, .webm, .flac, .aac, or another key present in g4f.image.EXTENSIONS_MAP.
- Check beforehand: from g4f.image import get_extension; assert get_extension(filename) is not None.
- If the format genuinely is unsupported, convert the audio to mp3/wav (e.g. with pydub) before calling.
Example fix
// before
payload = to_input_audio(open('voicenote','rb').read(), filename='voicenote')
// after
payload = to_input_audio(open('voicenote.mp3','rb').read(), filename='voicenote.mp3') Defensive patterns
Strategy: validation
Validate before calling
from g4f.image import get_extension
def valid_audio_filename(filename: str) -> bool:
return get_extension(filename) is not None
assert valid_audio_filename("clip.mp3") Prevention
- Always include a known audio extension (.mp3, .wav, .ogg, .webm, ...) in the filename argument.
- Reuse get_extension() as the pre-check — it is the exact predicate the error uses.
When it happens
Trigger: Calling to_input_audio(audio_bytes, filename="recording") with no extension, or filename="track.opus" / "audio.m4a" where the extension is not registered in g4f's EXTENSIONS_MAP.
Common situations: Generating temp filenames like tempfile.mkstemp() suffix-less names; using rarer audio containers the map does not cover; upstream code deriving the filename from a URL query string that lost its extension.
Related errors
- No input provided
- Invalid image format or file not found. Expected bytes, str,
- Response must be a dict or have headers
- url must be a non-empty string
- url must start with http:// or https://
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/59cce051b01e4276.
Report an issue: GitHub.