{"record":{"id":"59cce051b01e4276","repo":"xtekky/gpt4free","slug":"invalid-input-audio","errorCode":null,"errorMessage":"Invalid input audio","messagePattern":"Invalid input audio","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/image/__init__.py","lineNumber":514,"sourceCode":"        except (AttributeError, io.UnsupportedOperation):\n            pass\n        return image.read()\n\n\ndef to_data_uri(image: ImageType, filename: str = None) -> str:\n    if not isinstance(image, str):\n        data = to_bytes(image)\n        data_base64 = base64.b64encode(data).decode()\n        return f\"data:{is_data_an_media(data, filename)};base64,{data_base64}\"\n    return image\n\n\ndef to_input_audio(audio: ImageType, filename: str = None) -> str:\n    if not isinstance(audio, str):\n        if filename is not None:\n            format = get_extension(filename)\n            if format is None:\n                raise ValueError(\"Invalid input audio\")\n            return {\n                \"data\": base64.b64encode(to_bytes(audio)).decode(),\n                \"format\": format,\n            }\n        raise ValueError(\"Invalid input audio\")\n    audio = re.match(r\"^data:audio/(\\w+);base64,(.+?)\", audio)\n    if audio:\n        return {\"data\": audio.group(2), \"format\": audio.group(1).replace(\"mpeg\", \"mp3\")}\n    raise ValueError(\"Invalid input audio\")\n\n\ndef use_aspect_ratio(extra_body: dict, aspect_ratio: str) -> Image:\n    extra_body = {key: value for key, value in extra_body.items() if value is not None}\n    if extra_body.get(\"width\") is None or extra_body.get(\"height\") is None:\n        width, height = get_width_height(\n            aspect_ratio, extra_body.get(\"width\"), extra_body.get(\"height\")\n        )\n        extra_body = {\"width\": width, \"height\": height, **extra_body}","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/image/__init__.py#L496-L532","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npayload = to_input_audio(open('voicenote','rb').read(), filename='voicenote')\n\n// after\npayload = to_input_audio(open('voicenote.mp3','rb').read(), filename='voicenote.mp3')","handlingStrategy":"validation","validationCode":"from g4f.image import get_extension\n\ndef valid_audio_filename(filename: str) -> bool:\n    return get_extension(filename) is not None\n\nassert valid_audio_filename(\"clip.mp3\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["audio","input-validation","extension"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}