fishaudio/fish-speech · error · HTTPException

Failed to encode audio

Error message

Failed to encode audio

What it means

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'.

Source

Thrown at tools/server/views.py:110

        # Get the model from the app
        model_manager: ModelManager = request.app.state.model_manager
        decoder_model = model_manager.decoder_model

        # Encode the audio
        start_time = time.time()
        tokens = cached_vqgan_batch_encode(decoder_model, req.audios)
        logger.info(
            f"[EXEC] VQGAN encode time: {(time.time() - start_time) * 1000:.2f}ms"
        )

        # Return the response
        return ormsgpack.packb(
            ServeVQGANEncodeResponse(tokens=[i.tolist() for i in tokens]),
            option=ormsgpack.OPT_SERIALIZE_PYDANTIC,
        )
    except Exception as e:
        logger.error(f"Error in VQGAN encode: {e}", exc_info=True)
        raise HTTPException(
            HTTPStatus.INTERNAL_SERVER_ERROR, content="Failed to encode audio"
        )


@routes.http.post("/v1/vqgan/decode")
async def vqgan_decode(req: Annotated[ServeVQGANDecodeRequest, Body(exclusive=True)]):
    """
    Decode tokens to audio using VQGAN model.
    """
    try:
        # Get the model from the app
        model_manager: ModelManager = request.app.state.model_manager
        decoder_model = model_manager.decoder_model

        # Decode the audio
        tokens = [torch.tensor(token, dtype=torch.int) for token in req.tokens]
        start_time = time.time()
        audios = batch_vqgan_decode(decoder_model, tokens)

View on GitHub (pinned to befe400174)

Solutions

  1. Check server logs for the logged underlying exception ('Error in VQGAN encode: ...')
  2. Re-encode the audio to 16-bit PCM WAV (e.g. via ffmpeg) before uploading
  3. Verify the VQGAN model path/config used at server startup

Example fix

# shell: normalize the audio before upload
ffmpeg -i input.mp3 -ar 44100 -ac 1 -c:a pcm_s16le ref.wav
Defensive patterns

Strategy: fallback

Validate before calling

import soundfile as sf
try:
    data, sr = sf.read(audio_path)
except Exception:
    raise ValueError("audio unreadable; re-encode to 16-bit WAV")

Try / catch

if resp.status_code == 500:
    # re-encode and retry once
    ffmpeg_normalize(audio_path)
    resp = retry_encode(client, audio_path)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of fishaudio/fish-speech@befe400174 (2026-08-27). Data as JSON: /api/errors/dd5e919eb34760fa. Report an issue: GitHub.