{"record":{"id":"9d634bd7436d7a5f","repo":"AUTOMATIC1111/stable-diffusion-webui","slug":"invalid-image-format","errorCode":null,"errorMessage":"Invalid image format","messagePattern":"Invalid image format","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"modules/api/api.py","lineNumber":128,"sourceCode":"                if isinstance(key, str) and isinstance(value, str):\n                    metadata.add_text(key, value)\n                    use_metadata = True\n            image.save(output_bytes, format=\"PNG\", pnginfo=(metadata if use_metadata else None), quality=opts.jpeg_quality)\n\n        elif opts.samples_format.lower() in (\"jpg\", \"jpeg\", \"webp\"):\n            if image.mode in (\"RGBA\", \"P\"):\n                image = image.convert(\"RGB\")\n            parameters = image.info.get('parameters', None)\n            exif_bytes = piexif.dump({\n                \"Exif\": { piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump(parameters or \"\", encoding=\"unicode\") }\n            })\n            if opts.samples_format.lower() in (\"jpg\", \"jpeg\"):\n                image.save(output_bytes, format=\"JPEG\", exif = exif_bytes, quality=opts.jpeg_quality)\n            else:\n                image.save(output_bytes, format=\"WEBP\", exif = exif_bytes, quality=opts.jpeg_quality)\n\n        else:\n            raise HTTPException(status_code=500, detail=\"Invalid image format\")\n\n        bytes_data = output_bytes.getvalue()\n\n    return base64.b64encode(bytes_data)\n\n\ndef api_middleware(app: FastAPI):\n    rich_available = False\n    try:\n        if os.environ.get('WEBUI_RICH_EXCEPTIONS', None) is not None:\n            import anyio  # importing just so it can be placed on silent list\n            import starlette  # importing just so it can be placed on silent list\n            from rich.console import Console\n            console = Console()\n            rich_available = True\n    except Exception:\n        pass\n","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/82a973c04367123ae98bd9abdf80d9eda9b910e2/modules/api/api.py#L110-L146","documentation":"HTTP 500 raised by encode_pil_to_base64 when returning generated images: the server serializes output images according to opts.samples_format, and if that setting is a value other than png/jpg/jpeg/webp (e.g. 'avif', a typo, or an unset/empty string) no save branch matches and the response cannot be built. It reflects an invalid server-side output format configuration, not a bad request.","triggerScenarios":"Any successful /sdapi/v1/txt2img, img2img, or extras call when Settings -> Saving to a directory -> 'File format for images' (samples_format) has been set to an unsupported extension; default is 'png', so this only fires after configuration changes or config-file hand-edits.","commonSituations":"Users setting samples_format to 'avif' or 'jpeg2000' which the encoder branch doesn't handle; hand-edited config.json with a typo; stale settings after webui versions that renamed the option.","solutions":["Set Settings -> Saving to a directory -> File format for images to one of png, jpg, jpeg, or webp, or remove the key from config.json to restore the 'png' default","Restart the server after fixing config.json if editing by hand","If you need avif, convert client-side from the png base64 the API returns"],"exampleFix":"# before: config.json\n\"samples_format\": \"avif\"\n\n# after\n\"samples_format\": \"png\"","handlingStrategy":"validation","validationCode":"opts = requests.get(f'{base}/sdapi/v1/options', auth=auth).json()\nassert opts.get('samples_format','png') in ('png','jpg','jpeg','webp'), f\"server samples_format invalid: {opts.get('samples_format')}\"","typeGuard":"def samples_format_ok(fmt: str | None) -> bool:\n    return (fmt or 'png') in ('png','jpg','jpeg','webp')","tryCatchPattern":null,"preventionTips":["Never hand-edit samples_format in config.json to exotic formats","Post-install sanity check: hit /sdapi/v1/txt2img with a tiny prompt","Convert formats client-side from PNG output"],"tags":["api","configuration","image-encoding","http-500","stable-diffusion-webui"],"backgroundTag":null,"analyzedSha":"82a973c04367123ae98bd9abdf80d9eda9b910e2","analyzedAt":"2026-08-14T16:46:43.225Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}