{"record":{"id":"dd5e919eb34760fa","repo":"fishaudio/fish-speech","slug":"failed-to-encode-audio","errorCode":null,"errorMessage":"Failed to encode audio","messagePattern":"Failed to encode audio","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"tools/server/views.py","lineNumber":110,"sourceCode":"        # Get the model from the app\n        model_manager: ModelManager = request.app.state.model_manager\n        decoder_model = model_manager.decoder_model\n\n        # Encode the audio\n        start_time = time.time()\n        tokens = cached_vqgan_batch_encode(decoder_model, req.audios)\n        logger.info(\n            f\"[EXEC] VQGAN encode time: {(time.time() - start_time) * 1000:.2f}ms\"\n        )\n\n        # Return the response\n        return ormsgpack.packb(\n            ServeVQGANEncodeResponse(tokens=[i.tolist() for i in tokens]),\n            option=ormsgpack.OPT_SERIALIZE_PYDANTIC,\n        )\n    except Exception as e:\n        logger.error(f\"Error in VQGAN encode: {e}\", exc_info=True)\n        raise HTTPException(\n            HTTPStatus.INTERNAL_SERVER_ERROR, content=\"Failed to encode audio\"\n        )\n\n\n@routes.http.post(\"/v1/vqgan/decode\")\nasync def vqgan_decode(req: Annotated[ServeVQGANDecodeRequest, Body(exclusive=True)]):\n    \"\"\"\n    Decode tokens to audio using VQGAN model.\n    \"\"\"\n    try:\n        # Get the model from the app\n        model_manager: ModelManager = request.app.state.model_manager\n        decoder_model = model_manager.decoder_model\n\n        # Decode the audio\n        tokens = [torch.tensor(token, dtype=torch.int) for token in req.tokens]\n        start_time = time.time()\n        audios = batch_vqgan_decode(decoder_model, tokens)","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/fishaudio/fish-speech/blob/befe4001745417f8c42131739d862b8a6fdbd15a/tools/server/views.py#L92-L128","documentation":"The /v1/vqgan/encode endpoint wraps VQGAN encoding in try/except; any exception (decode failure of the uploaded audio, model mismatch, device errors) is logged and re-raised as a generic HTTP 500 'Failed to encode audio'.","triggerScenarios":"POSTing audio that cannot be loaded by the audio loader (wrong codec/container, corrupt bytes), or server-side model/device errors, to /v1/vqgan/encode.","commonSituations":"Uploading raw bytes with the wrong extension/content-type, 24-bit or exotic WAV formats the loader can't decode, or a server started without a VQGAN model loaded.","solutions":["Check server logs for the logged underlying exception ('Error in VQGAN encode: ...')","Re-encode the audio to 16-bit PCM WAV (e.g. via ffmpeg) before uploading","Verify the VQGAN model path/config used at server startup"],"exampleFix":"# shell: normalize the audio before upload\nffmpeg -i input.mp3 -ar 44100 -ac 1 -c:a pcm_s16le ref.wav","handlingStrategy":"fallback","validationCode":"import soundfile as sf\ntry:\n    data, sr = sf.read(audio_path)\nexcept Exception:\n    raise ValueError(\"audio unreadable; re-encode to 16-bit WAV\")","typeGuard":null,"tryCatchPattern":"if resp.status_code == 500:\n    # re-encode and retry once\n    ffmpeg_normalize(audio_path)\n    resp = retry_encode(client, audio_path)","preventionTips":["Pre-convert uploads to pcm_s16le WAV with ffmpeg","Verify server has the VQGAN model loaded (check startup logs)"],"tags":["api-server","http-500","audio-processing","vqgan"],"backgroundTag":"audio-decode-failed","analyzedSha":"befe4001745417f8c42131739d862b8a6fdbd15a","analyzedAt":"2026-08-27T21:31:45.703Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}